-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
57 lines (40 loc) · 1.16 KB
/
server.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// UTILS
var express = require("express");
var sessions = require("express-session");
var bodyParsers = require("body-parser");
var app = express();
// MUTUAL STUFF
app.use(bodyParsers.urlencoded());
app.use(sessions({
secret:"superdupersecret"
}));
// PUBLIC
var publicReqHandler = express.Router();
publicReqHandler.post("/login.html", function(req, res){
if(req.body.username == "john" && req.body.password == "B@dP@ssw0rd"){
req.session.isLoggedIn = true;
res.redirect("/home.html");
} else {
res.redirect("/login.html#invalid, try again");
}
});
publicReqHandler.use(express.static("public"));
// PRIVATE
var privateReqHandler = express.Router();
privateReqHandler.all("*", function(req, res, next){
if(req.session.isLoggedIn){
next();
} else {
res.redirect("/login.html#login first!");
}
});
publicReqHandler.get("/logout", function(req, res){
req.session.destroy();
res.redirect("/login.html#you are logged out");
});
privateReqHandler.use(express.static("private"));
// FIRE IT UP!
app.use(publicReqHandler);
app.use(privateReqHandler);
var port = process.env.PORT;
app.listen(port);