-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathapp.js
More file actions
64 lines (54 loc) · 1.51 KB
/
Copy pathapp.js
File metadata and controls
64 lines (54 loc) · 1.51 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
56
57
58
59
60
61
62
63
64
import express from 'express';
import mongoose from 'mongoose';
import cors from 'cors';
import session from 'express-session';
import connectMongoDBSession from 'connect-mongodb-session';
import passport from 'passport';
import cookieParser from 'cookie-parser';
import authRoutes from './routes/auth';
import { isUser } from './middleware/auth-middleware';
import User from './models/user';
const app = express();
const MongoDBStore = connectMongoDBSession(session);
const store = new MongoDBStore({
uri: process.env.MONGODB_URI,
collection: 'sessions'
});
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser(process.env.SECRET));
app.use(
session({
secret: process.env.SECRET,
resave: false,
saveUninitialized: false,
cookie: { secure: true },
store
})
);
// passport initialization.
app.use(passport.initialize());
app.use(passport.session());
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
app.use(isUser);
app.use(authRoutes);
mongoose
.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useFindAndModify: false
})
.then(() => {
console.log(`Connected Successfully to MongoDB!`);
app.listen(process.env.PORT, () => {
console.log(`Server Running at PORT ${process.env.PORT}`);
});
})
.catch(err => console.log(`Connection Failed to MongoDB : ${err}`));