-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth-methods.js
More file actions
79 lines (65 loc) · 2.64 KB
/
auth-methods.js
File metadata and controls
79 lines (65 loc) · 2.64 KB
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
var passport = require('passport')
, GoogleStrategy = require('passport-google-oauth').OAuth2Strategy
, GitHubStrategy = require('passport-github').Strategy
, TwitterStrategy = require('passport-twitter').Strategy
, User = require('./models/user')
, config = require('./config.json');
module.exports = {};
passport.serializeUser(function (user, done) {
User.serializeUser(user, done);
});
passport.deserializeUser(function (obj, done) {
User.deserializeUser(obj, done);
});
passport.use(new GoogleStrategy({
clientID: config.authGoogle.ID,
clientSecret: config.authGoogle.SECRET,
callbackURL: getCallbackURLWithProtocol(config.authGoogle.callbackURL)
},
function (token, tokenSecret, profile, done) {
User.findOrCreateGoogleUser(token, tokenSecret, profile, done);
}
));
passport.use(new GitHubStrategy({
clientID: config.authGitHub.ID,
clientSecret: config.authGitHub.SECRET,
callbackURL: getCallbackURLWithProtocol(config.authGitHub.callbackURL)
},
function (accessToken, refreshToken, profile, done) {
User.findOrCreateGitHubUser(accessToken, refreshToken, profile, done);
}
));
passport.use(new TwitterStrategy({
consumerKey: config.authTwitter.ID,
consumerSecret: config.authTwitter.SECRET,
callbackURL: getCallbackURLWithProtocol(config.authTwitter.callbackURL)
},
function (token, tokenSecret, profile, done) {
User.findOrCreateTwitterUser(token, tokenSecret, profile, done);
}
));
function getCallbackURLWithProtocol(url) {
return (config.useHTTPS ? 'https://' : 'http://') + url;
}
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) { return next(); }
res.redirect('/');
}
function apiAuthentication(req, res, next) {
if (req.isAuthenticated()) { return next(); }
return res.json(401, { 'err': 'Authentication failed. Please log in.'});
}
module.exports.googleAuth = passport.authenticate('google', {
scope: [
'https://www.googleapis.com/auth/userinfo.profile'
, 'https://www.googleapis.com/auth/userinfo.email'
/*, 'https://www.googleapis.com/auth/plus.login'*/]
});
module.exports.googleCallback = passport.authenticate('google', { failureRedirect: '/' });
module.exports.githubAuth = passport.authenticate('github', { scope: ['user:email'] });
module.exports.githubCallback = passport.authenticate('github', { failureRedirect: '/'});
module.exports.twitterAuth = passport.authenticate('twitter');
module.exports.twitterCallback = passport.authenticate('twitter', { failureRedirect: '/'});
module.exports.passport = passport;
module.exports.ensureAuthenticated = ensureAuthenticated;
module.exports.apiAuthentication = apiAuthentication;