forked from impronunciable/hackdash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.js
107 lines (87 loc) · 2.86 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
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
/*
* Module dependencies
*/
var passport = require('passport')
, keys = require('./keys.json')
, mongoose = require('mongoose')
, gravatar = require('gravatar');
var User = mongoose.model('User');
passport.serializeUser(function(user, done) {
done(null, user._id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user){
done(err, user);
});
});
var initStrategies = function(app) {
app.set('providers', Object.keys(keys));
for(var provider in keys) {
initStrategy(app, keys, provider);
}
if(keys['persona']) {
app.post('/auth/persona',
passport.authenticate('persona', { failureRedirect: '/login' }),
function(req, res) {
res.redirect('/');
});
}
};
var initStrategy = function(app, keys, provider) {
app.get('/auth/' + provider, passport.authenticate(provider));
app.get('/auth/' + provider + '/callback',
passport.authenticate(provider, { failureRedirect: '/' }),
function(req, res){ res.redirect('/'); });
var Strategy = require('passport-' + provider).Strategy;
passport.use(new Strategy(keys[provider], findOrCreateUser(provider)));
};
var findOrCreateUser = function (provider) {
if(provider === "persona") return findOrCreatePersona;
else return function(token, tokenSecret, profile, done) {
User.findOne({provider_id: profile.id, provider: provider},
function(err, user){
if(err) return res.send(500);
if(!user) {
createUser(provider, profile, done);
} else {
done(null, user);
}
});
};
};
var findOrCreatePersona = function(email, done) {
User.findOne({email: email, provider: 'persona'},
function(err, user){
if(err) return res.send(500);
if(!user) {
createUser('persona', {emails: [{value: email}], id: 1, username: email, displayName: email},
done);
} else {
done(null, user);
}
});
};
var createUser = function(provider, profile, done) {
var user = new User();
user.provider = provider;
user.provider_id = profile.id;
if(profile.emails && profile.emails.length && profile.emails[0].value)
user.email = profile.emails[0].value;
user.picture = getProfilePicture(profile, user.email);
user.name = profile.displayName;
user.username = profile.username || profile.displayName;
user.save(done);
};
var getProfilePicture = function(profile, email) {
var picture = '/images/default_avatar.png';
if(profile.photos && profile.photos.length && profile.photos[0].value) {
picture = profile.photos[0].value.replace('_normal', '_bigger');
} else if(profile.provider == 'facebook') {
picture = "https://graph.facebook.com/" + profile.id + "/picture";
picture += "?width=73&height=73";
} else {
picture = gravatar.url(email || '', {s: '73'});
}
return picture;
};
module.exports = initStrategies;