-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
55 lines (46 loc) · 1.34 KB
/
server.js
File metadata and controls
55 lines (46 loc) · 1.34 KB
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
require("dotenv").config();
const express = require("express");
const { join } = require("path");
const passport = require("passport");
const { User } = require("./models");
const { Strategy: JWTStrategy, ExtractJwt } = require("passport-jwt");
const app = express();
if (process.env.NODE_ENV === "production") {
app.use((req, res, next) => {
if (req.headers["x-forwarded-proto"] !== "https") {
return res.redirect(`https://${req.headers.host}${req.url}`);
}
next();
});
}
app.use(express.static(join(__dirname, "public")));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(passport.initialize());
app.use(passport.session());
passport.use(User.createStrategy());
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser((id, done) => {
User.findOne({ id })
.then((user) => done(null, user))
.catch((err) => done(err, null));
});
passport.use(
new JWTStrategy(
{
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: process.env.SECRET,
},
({ id }, cb) =>
User.findOne({ where: { id } })
.then((user) => cb(null, user))
.catch((err) => cb(err))
)
);
app.use(require("./routes"));
require("./db")
.sync()
.then(() => app.listen(process.env.PORT || 3000))
.catch((err) => console.log(err));