-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
37 lines (30 loc) · 1.35 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
require('dotenv').config()
const express = require('express')
const app = express()
const port = process.env.PORT || 3000;
const basicAuth = require('express-basic-auth')
// Function used in the myAuthorizer function
const myAuthorizer = (username, password) => {
// Change this later when ready, maybe use a mongoDB or sqlite3 in memory
// https://studio.zollege.com/container/block-v1:ACA+JS311+2020_Q2+type@vertical+block@7bfe32a3ac0748d89a46aa339a68fa9f
return (username === process.env.STUDENT_USERNAME && password === process.env.STUDENT_PASSWORD) || (username === process.env.INSTRUCTOR_USERNAME && password === process.env.INSTRUCTOR_PASSWORD)
}
// using the express-basic-auth package we can return a WWW challenge for the user to enter their password.
const challengeAuth = basicAuth({
authorizer: myAuthorizer,
challenge: true
})
// app.use(express.json());
// app.use(express.urlencoded());
// forces all requests to go through the challengeAuth function, see above
app.use('/', challengeAuth);
// Serve static content
app.use(express.static('./ebook-folder/site'))
// may need to include images later....
app.listen(port, () => {
if(process.env.ENVIRONMENT !== "PRODUCTION") {
console.log(`Web server is listening on port ${port}!`);
}
});
// Return to this approach later:
// https://github.com/expressjs/express/blob/master/examples/auth/index.js