-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
63 lines (55 loc) · 2.09 KB
/
auth.js
File metadata and controls
63 lines (55 loc) · 2.09 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
// TODO: make setupAuth depend on the Config service...
function setupAuth(User, Config, app) {
var passport = require('passport');
var FacebookStrategy = require('passport-facebook').Strategy;
// High level serialize/de-serialize configuration for passport
passport.serializeUser(function (user, done) {
done(null, user._id);
});
passport.deserializeUser(function (id, done) {
User.
findOne({ _id: id }).
exec(done);
});
// Facebook-specific
passport.use(new FacebookStrategy(
{
// TODO: and use the Config service here
clientID: Config.facebookClientId,
clientSecret: Config.facebookClientSecret,
callbackURL: 'http://localhost:3000/auth/facebook/callback'
},
function (accessToken, refreshToken, profile, done) {
if (!profile.emails || !profile.emails.length) {
return done('No emails associated with this account!');
}
User.findOneAndUpdate(
{ 'data.oauth': profile.id },
{
$set: {
'profile.username': profile.emails[0].value,
'profile.picture': 'http://graph.facebook.com/' +
profile.id.toString() + '/picture?type=large'
}
},
{ 'new': true, upsert: true, runValidators: true },
function (error, user) {
done(error, user);
});
}));
// Express middlewares
app.use(require('express-session')({
secret: 'this is a secret'
}));
app.use(passport.initialize());
app.use(passport.session());
// Express routes for auth
app.get('/auth/facebook',
passport.authenticate('facebook', { scope: ['email'] }));
app.get('/auth/facebook/callback',
passport.authenticate('facebook', { failureRedirect: '/fail' }),
function (req, res) {
res.send('Welcome, ' + req.user.profile.username);
});
}
module.exports = setupAuth;