forked from nko4/macambiras
-
Notifications
You must be signed in to change notification settings - Fork 1
/
passport.js
100 lines (89 loc) · 2.79 KB
/
passport.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
'use strict';
exports = module.exports = function(app, passport) {
var LocalStrategy = require('passport-local').Strategy,
TwitterStrategy = require('passport-twitter').Strategy,
GitHubStrategy = require('passport-github').Strategy,
FacebookStrategy = require('passport-facebook').Strategy;
passport.use(new LocalStrategy(
function(username, password, done) {
var conditions = { isActive: 'yes' };
if (username.indexOf('@') === -1) {
conditions.username = username;
}
else {
conditions.email = username;
}
app.db.models.User.findOne(conditions, function(err, user) {
if (err) {
return done(err);
}
if (!user) {
return done(null, false, { message: 'Unknown user' });
}
var encryptedPassword = app.db.models.User.encryptPassword(password);
if (user.password !== encryptedPassword) {
return done(null, false, { message: 'Invalid password' });
}
return done(null, user);
});
}
));
if (app.get('twitter-oauth-key')) {
passport.use(new TwitterStrategy({
consumerKey: app.get('twitter-oauth-key'),
consumerSecret: app.get('twitter-oauth-secret')
},
function(token, tokenSecret, profile, done) {
done(null, false, {
token: token,
tokenSecret: tokenSecret,
profile: profile
});
}
));
}
if (app.get('github-oauth-key')) {
passport.use(new GitHubStrategy({
clientID: app.get('github-oauth-key'),
clientSecret: app.get('github-oauth-secret'),
customHeaders: { "User-Agent": app.get('project-name') }
},
function(accessToken, refreshToken, profile, done) {
done(null, false, {
accessToken: accessToken,
refreshToken: refreshToken,
profile: profile
});
}
));
}
if (app.get('facebook-oauth-key')) {
passport.use(new FacebookStrategy({
clientID: app.get('facebook-oauth-key'),
clientSecret: app.get('facebook-oauth-secret')
},
function(accessToken, refreshToken, profile, done) {
done(null, false, {
accessToken: accessToken,
refreshToken: refreshToken,
profile: profile
});
}
));
}
passport.serializeUser(function(user, done) {
done(null, user._id);
});
passport.deserializeUser(function(id, done) {
app.db.models.User.findOne({ _id: id }).populate('roles.admin').populate('roles.account').exec(function(err, user) {
if (user.roles && user.roles.admin) {
user.roles.admin.populate("groups", function(err, admin) {
done(err, user);
});
}
else {
done(err, user);
}
});
});
};