-
Notifications
You must be signed in to change notification settings - Fork 11
/
app.js
125 lines (100 loc) · 3.43 KB
/
app.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
var authConfig = require('./config/auth'),
express = require('express'),
passport = require('passport'),
GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
// Passport session setup.
//
// For persistent logins with sessions, Passport needs to serialize users into
// and deserialize users out of the session. Typically, this is as simple as
// storing the user ID when serializing, and finding the user by ID when
// deserializing.
passport.serializeUser(function(user, done) {
// done(null, user.id);
done(null, user);
});
passport.deserializeUser(function(obj, done) {
// Users.findById(obj, done);
done(null, obj);
});
// Use the GoogleStrategy within Passport.
// Strategies in Passport require a `verify` function, which accept
// credentials (in this case, an accessToken, refreshToken, and Google
// profile), and invoke a callback with a user object.
// See http://passportjs.org/docs/configure#verify-callback
passport.use(new GoogleStrategy(
// Use the API access settings stored in ./config/auth.json. You must create
// an OAuth 2 client ID and secret at: https://console.developers.google.com
authConfig.google,
function(accessToken, refreshToken, profile, done) {
// Typically you would query the database to find the user record
// associated with this Google profile, then pass that object to the `done`
// callback.
return done(null, profile);
}
));
// Express 4 boilerplate
var app = express();
app.set('view engine', 'hbs');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var session = require('express-session');
app.use(logger('dev'));
app.use(cookieParser());
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(__dirname + '/public'));
// Application routes
app.get('/', function(req, res) {
res.render('index', {
user: req.user
});
});
app.get('/login', function(req, res) {
res.render('login', {
user: req.user
});
});
// GET /auth/google
// Use passport.authenticate() as route middleware to authenticate the
// request. The first step in Google authentication will involve
// redirecting the user to google.com. After authorization, Google
// will redirect the user back to this application at /auth/google/callback
app.get('/auth/google',
passport.authenticate('google', { scope: ['openid email profile'] }));
// GET /auth/google/callback
// Use passport.authenticate() as route middleware to authenticate the
// request. If authentication fails, the user will be redirected back to the
// login page. Otherwise, the primary route function function will be called,
// which, in this example, will redirect the user to the home page.
app.get('/auth/google/callback',
passport.authenticate('google', {
failureRedirect: '/login'
}),
function(req, res) {
// Authenticated successfully
res.redirect('/');
});
app.get('/account', ensureAuthenticated, function(req, res) {
res.render('account', {
user: req.user
});
});
app.get('/logout', function(req, res) {
req.logout();
res.redirect('/');
});
app.listen(process.env.PORT || 3000, function() {
console.log("Listening...");
});
// Simple route middleware to ensure user is authenticated.
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
res.redirect('/login');
}