forked from bognix/express-passport-google-oauth-exmple
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
55 lines (48 loc) · 1.24 KB
/
server.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
const express = require('express'),
app = express(),
passport = require('passport'),
auth = require('./auth'),
cookieParser = require('cookie-parser'),
cookieSession = require('cookie-session');
auth(passport);
app.use(passport.initialize());
app.use(cookieSession({
name: 'session',
keys: ['SECRECT KEY'],
maxAge: 24 * 60 * 60 * 1000
}));
app.use(cookieParser());
app.get('/', (req, res) => {
if (req.session.token) {
res.cookie('token', req.session.token);
res.json({
status: 'session cookie set'
});
} else {
res.cookie('token', '')
res.json({
status: 'session cookie not set'
});
}
});
app.get('/logout', (req, res) => {
req.logout();
req.session = null;
res.redirect('/');
});
app.get('/auth/google', passport.authenticate('google', {
scope: ['https://www.googleapis.com/auth/userinfo.profile']
}));
app.get('/auth/google/callback',
passport.authenticate('google', {
failureRedirect: '/'
}),
(req, res) => {
console.log(req.user.token);
req.session.token = req.user.token;
res.redirect('/');
}
);
app.listen(3000, () => {
console.log('Server is running on port 3000');
});