|
1
|
|
|
var passport = require('passport') |
|
2
|
|
|
const passportConfig = require('../config/passport') |
|
3
|
|
|
|
|
4
|
|
|
module.exports = function (app) { |
|
5
|
|
|
// GET /auth/google |
|
6
|
|
|
// Use passport.authenticate() as route middleware to authenticate the |
|
7
|
|
|
// request. The first step in Google authentication will involve |
|
8
|
|
|
// redirecting the user to google.com. After authorization, Google |
|
9
|
|
|
// will redirect the user back to this application at /auth/google/callback |
|
10
|
|
|
app.get('/auth/google', passport.authenticate('google', { |
|
11
|
|
|
scope: [ |
|
12
|
|
|
'https://www.googleapis.com/auth/plus.login', |
|
13
|
|
|
'https://www.googleapis.com/auth/plus.profile.emails.read'] |
|
14
|
|
|
})) |
|
15
|
|
|
|
|
16
|
|
|
// GET /auth/google/callback |
|
17
|
|
|
// Use passport.authenticate() as route middleware to authenticate the |
|
18
|
|
|
// request. If authentication fails, the user will be redirected back to the |
|
19
|
|
|
// login page. Otherwise, the primary route function function will be called, |
|
20
|
|
|
// which, in this example, will redirect the user to the home page. |
|
21
|
|
|
app.get('/auth/google/callback', |
|
22
|
|
|
passport.authenticate('google', { |
|
23
|
|
|
successRedirect: '/', |
|
24
|
|
|
failureRedirect: '/auth/login' |
|
25
|
|
|
})) |
|
26
|
|
|
|
|
27
|
|
|
app.get('/account', passportConfig.ensureAuthenticated, function (req, res) { |
|
28
|
|
|
console.log(req) |
|
|
|
|
|
|
29
|
|
|
res.render('account', { user: req.user }) |
|
30
|
|
|
}) |
|
31
|
|
|
|
|
32
|
|
|
app.get('/auth/login', function (req, res) { |
|
33
|
|
|
res.render('login', { user: req.user }) |
|
34
|
|
|
}) |
|
35
|
|
|
|
|
36
|
|
|
app.get('/auth/logout', function (req, res) { |
|
37
|
|
|
req.logout() |
|
38
|
|
|
res.redirect('/') |
|
39
|
|
|
}) |
|
40
|
|
|
} |
|
41
|
|
|
|