-
Notifications
You must be signed in to change notification settings - Fork 0
/
express-sessions.js
58 lines (52 loc) · 1.33 KB
/
express-sessions.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
const passport = require('passport')
const LocalStrategy = require('passport-local').Strategy
const session = require('express-session')
const MongoDBStore = require('connect-mongodb-session')(session)
const Users = require('./models/users')
const { DB_URL, SESSION_SECRET } = require('./db')
module.exports = (app) => {
passport.serializeUser((user, done) => {
done(null, user)
})
passport.deserializeUser((obj, done) => {
done(null, obj)
})
passport.use(
new LocalStrategy((email, password, done) => {
process.nextTick(async () => {
let user = await Users.findOne({ email: email })
if (user) {
return done(null, user)
} else {
return done(null, false, 'user not found')
}
})
})
)
const sessionStore = new MongoDBStore({
uri: DB_URL,
collection: 'sessions',
})
// added sameSite and secure to cookie for render.com
app.use(
session({
secret: SESSION_SECRET,
resave: false,
saveUninitialized: false,
rolling: true,
unset: 'destroy',
store: sessionStore,
cookie: {
maxAge: 7 * 24 * 60 * 60 * 1000, // 1 week in milliseconds
sameSite: 'none',
secure: true,
},
})
)
app.use(
passport.initialize({
userProperty: 'user',
})
)
app.use(passport.session())
}