-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
140 lines (125 loc) · 3.51 KB
/
app.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import express from "express";
const app = express();
import { fileURLToPath } from "url";
import { dirname } from "path";
import exphbs from "express-handlebars";
import session from "express-session";
import configRoutes from "./routes/index.js";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const staticDir = express.static(__dirname + "/public");
app.use("/public", staticDir);
app.use(express.json());
app.use(
express.urlencoded({
extended: true,
})
);
const hbs = exphbs.create({
defaultLayout: "main",
helpers: {
json: function (context) {
return JSON.stringify(context);
},
},
});
app.engine("handlebars", hbs.engine);
app.set("view engine", "handlebars");
const rewriteUnsupportedBrowserMethods = (req, res, next) => {
// If the user posts to the server with a property called _method, rewrite the request's method
// To be that method; so if they post _method=PUT you can now allow browsers to POST to a route that gets
// rewritten in this middleware to a PUT route
if (req.body && req.body._method) {
req.method = req.body._method;
delete req.body._method;
}
// let the next middleware run:
next();
};
app.use(rewriteUnsupportedBrowserMethods);
// middleware -------------------------------------- v
app.use(
session({
name: "AuthCookie",
secret: "Patrick Hill",
resave: false,
saveUninitialized: true,
cookie: {
maxAge: 300000,
},
})
);
app.use("/", async (req, res, next) => {
if (req.path === "/error" || req.path === "/logout") return next();
if (req.session.user) {
//set req.session.user to a const value above to test
console.log(
`[${new Date().toUTCString()}]: ${req.method} ${
req.originalUrl
} (Authenticated User)`
);
return next();
}
if (req.path === "/user/login" || req.path == "/user/register") return next();
console.log(
`[${new Date().toUTCString()}]: ${req.method} ${
req.originalUrl
} (Non-Authenticated User)`
);
return res.redirect("/user/login");
});
app.use("/user", async (req, res, next) => {
if (!req.session.user) {
console.log(
`[${new Date().toUTCString()}]: ${req.method} ${
req.originalUrl
} (Non-Authenticated User)`
);
return next();
}
if (req.path === "/logout") return next(); //no /user bc /user is already in the path
console.log(
`[${new Date().toUTCString()}]: ${req.method} ${
req.originalUrl
} (Authenticated User)`
);
return res.redirect("/");
});
app.get("/transactions", async (req, res, next) => {
if (!req.session.user) {
console.log(
`[${new Date().toUTCString()}]: ${req.method} ${
req.originalUrl
} (Non-Authenticated User)`
);
return res.redirect("/user/login");
}
console.log(
`[${new Date().toUTCString()}]: ${req.method} ${
req.originalUrl
} (Authenticated User)`
);
return next();
});
app.use("/logout", async (req, res, next) => {
if (!req.session.user) {
console.log(
`[${new Date().toUTCString()}]: ${req.method} ${
req.originalUrl
} (Non-Authenticated User)`
);
return res.status(200).redirect("/user/login");
}
console.log(
`[${new Date().toUTCString()}]: ${req.method} ${
req.originalUrl
} (Authenticated User)`
);
next();
});
// middleware -------------------------------------- ^
configRoutes(app);
app.listen(3000, () => {
console.log("We've now got a server!");
console.log("Your routes will be running on http://localhost:3000");
});