-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.js
63 lines (54 loc) · 1.59 KB
/
auth.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
const passport = require('passport');
const session = require('express-session');
const FileStore = require('session-file-store')(session);
const GitHubStrategy = require('passport-github').Strategy;
const config = require('./config');
// Configure GitHub strategy for use by Passport.
passport.use(
new GitHubStrategy(
{
clientID: config.githubClientId,
clientSecret: config.githubClientSecret,
callbackURL: `${config.host}/login/github/callback`,
userAgent: 'nusmods-launchpad',
},
(accessToken, refreshToken, profile, cb) => {
// Ensure only whitelisted GitHub users can log in
if (!config.githubUsernames.includes(profile.username)) {
const authError = new Error('Username not recognized');
authError.status = 403;
return cb(authError);
}
return cb(null, profile);
},
),
);
// Configure Passport authenticated session persistence.
passport.serializeUser((user, cb) => {
cb(null, user);
});
passport.deserializeUser((obj, cb) => {
cb(null, obj);
});
module.exports = function auth(app) {
app.use(
session({
secret: config.sessionSecret,
resave: true,
saveUninitialized: true,
store: new FileStore(),
}),
);
// Initialize Passport and restore authentication state, if any, from the
// session.
app.use(passport.initialize());
app.use(passport.session());
app.get('/login/github', passport.authenticate('github'));
app.get(
'/login/github/callback',
passport.authenticate('github', { failureRedirect: '/' }),
(req, res) => {
res.redirect('/');
},
);
};