-
Notifications
You must be signed in to change notification settings - Fork 5
/
login.js
47 lines (39 loc) · 1.14 KB
/
login.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
const express = require('express');
const router = express.Router();
const database = require('../lib/db');
const passport = require('../lib/passport');
router.use(passport.initialize());
router.get('/auth/google',
passport.authenticate('google', {
scope: ['https://www.googleapis.com/auth/userinfo.profile']
})
);
router.get('/auth/google/callback',
passport.authenticate('google', { failureRedirect: '/' }),
(req, res) => {
req.session.user = req.user;
res.redirect('/films');
}
);
// Add auth middleware
router.post('/login', (req, res) => {
const { email, password, remember } = req.body;
const users = database.get('users');
const user = users.find((user) => user.email === email && user.password === password);
// Set 1h to expire
if (remember) {
req.sessionOptions.maxAge = 24 * 60 * 60 * 1000 // 24 hours
}
if (user) {
req.session.user = user;
res.redirect('/films');
} else {
res.redirect('/?error=true');
}
});
// Logout endpoint
router.get('/logout', (req, res) => {
req.session = null
res.redirect('/?logout=true');
});
module.exports = router;