-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
89 lines (76 loc) · 2.11 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
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
require("dotenv").config();
// imports
const express = require("express");
const mongoose = require("mongoose");
const exphbs = require("express-handlebars");
const expressSession = require("express-session");
const cookieParser = require("cookie-parser");
const bodyParser = require("body-parser");
const passport = require("passport");
const LocalStrategy = require("passport-local").Strategy;
const UserModel = require("./models/User");
const entryRouter = require("./controllers/entry");
const userRouter = require("./controllers/user");
const PORT = process.env.PORT || 3000;
// DB config
mongoose.connect(
process.env.MONGODB_URI,
{
useNewUrlParser: true,
useUnifiedTopology: true
},
err => {
if (err) {
console.log("Database connection error", err);
return;
}
console.log("Database connected");
}
);
// Express configs
const app = express();
// Passport & sessions
app.use(
expressSession({
secret: process.env.SESSION_SECRET,
resave: true,
saveUninitialized: true
})
);
app.use(cookieParser());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
passport.use(
new LocalStrategy(function(username, password, done) {
UserModel.findOne({ username: username }, function(err, user) {
if (err) {
return done(err);
}
if (!user) {
return done(null, false);
}
// TODO
if (!user.verifyPassword(password)) {
return done(null, false);
}
return done(null, user);
});
})
);
// use static authenticate method of model in LocalStrategy
passport.use(new LocalStrategy(UserModel.authenticate()));
// use static serialize and deserialize of model for passport session support
passport.serializeUser(UserModel.serializeUser());
passport.deserializeUser(UserModel.deserializeUser());
app.use(passport.initialize());
app.use(passport.session());
// Handlebars
app.engine("handlebars", exphbs());
app.set("view engine", "handlebars");
// Routes
app.use("/", entryRouter);
app.use("/", userRouter);
// Start server
app.listen(PORT, () => {
console.log(`Server started on port ${PORT}`);
});