forked from jedireza/drywall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
passport.js
121 lines (108 loc) · 3.29 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
'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,
GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
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' });
}
app.db.models.User.validatePassword(password, user.password, function(err, isValid) {
if (err) {
return done(err);
}
if (!isValid) {
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
});
}
));
}
if (app.get('google-oauth-key')) {
passport.use(new GoogleStrategy({
clientID: app.get('google-oauth-key'),
clientSecret: app.get('google-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 && user.roles && user.roles.admin) {
user.roles.admin.populate("groups", function(err, admin) {
done(err, user);
});
}
else {
done(err, user);
}
});
});
};