-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathindex.js
45 lines (35 loc) · 1.08 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
const express = require('express');
const session = require('express-session');
const passport = require('passport');
require('./auth');
const app = express();
function isLoggedIn(req, res, next) {
req.user ? next() : res.sendStatus(401);
}
app.use(session({ secret: 'cats', resave: false, saveUninitialized: true }));
app.use(passport.initialize());
app.use(passport.session());
app.get('/', (req, res) => {
res.send('<a href="/auth/google">Authenticate with Google</a>');
});
app.get('/auth/google',
passport.authenticate('google', { scope: [ 'email', 'profile' ] }
));
app.get( '/auth/google/callback',
passport.authenticate( 'google', {
successRedirect: '/protected',
failureRedirect: '/auth/google/failure'
})
);
app.get('/protected', isLoggedIn, (req, res) => {
res.send(`Hello ${req.user.displayName}`);
});
app.get('/logout', (req, res) => {
req.logout();
req.session.destroy();
res.send('Goodbye!');
});
app.get('/auth/google/failure', (req, res) => {
res.send('Failed to authenticate..');
});
app.listen(5000, () => console.log('listening on port: 5000'));