-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpassport.js
149 lines (130 loc) · 4.9 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const passport = require('passport')
const User = require('./schema/User')
var LocalStrategy = require('passport-local').Strategy;
var TwitterStrategy = require('passport-twitter').Strategy;
var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
var GithubStrategy = require('passport-github2').Strategy;
var FacebookStrategy = require('passport-facebook').Strategy;
module.exports = function(app, mongoose, config) {
// passport.use(new LocalStrategy(User.authenticate()));
passport.use(new LocalStrategy(
function(username, password, done) {
var conditions = { isActive: 'yes' };
if (username.indexOf('@') === -1) {
conditions.username = username;
}
else {
conditions.email = username.toLowerCase();
}
app.db.models.User.findOne(conditions, function(err, user) {
if (err) {
return done(err);
}
if (!user) {
return done(null, false, { message: 'Unknown user' });
}
// user is registered only using social
if (!user.password) {
let social = null
if (user.twitter) {
social = "twitter"
}
else if (user.github) {
social = 'github'
}
else if (user.facebook) {
social = 'facebook'
}
else if (user.google) {
social = 'google'
}
return done(null, false, { message: `This email has been registered using ${social}` });
}
app.db.models.User.validatePassword(password, user.password)
.then((isValid) => {
if (!isValid) {
return done(null, false, { message: 'Invalid password' });
}
return done(null, user);
})
.catch(err => done(err));
});
}
));
if (config.oauth.twitter.key) {
passport.use(new TwitterStrategy({
consumerKey: config.oauth.twitter.key,
consumerSecret: config.oauth.twitter.secret,
includeEmail: true,
},
function(token, tokenSecret, profile, done) {
done(null, false, {
accessToken: token,
refreshToken: tokenSecret,
profile: profile
})
}));
}
// passport.serializeUser(function(user, done) {
// console.log("serializing user:", user._id)
// done(null, user);
// })
// passport.deserializeUser(function(obj, done) {
// app.db.models.User.findOne({oauthID: obj.oauthID}, function(error, user) {
// done(null, user);
// })
// console.log('called deserializeUser')
// })
if (config.oauth.google.key) {
passport.use(new GoogleStrategy({
clientID: config.oauth.google.key,
clientSecret: config.oauth.google.secret,
callbackURL: config.oauth.google.callbackUrl,
},
function(token, tokenSecret, profile, done) {
done(null, false, {
accessToken: token,
refreshToken: tokenSecret,
profile: profile
})
}))
}
if (config.oauth.github.key) {
passport.use(new GithubStrategy({
clientID: config.oauth.github.key,
clientSecret: config.oauth.github.secret,
callbackURL: config.oauth.github.callbackUrl,
scope: ['user:email'],
},
function(token, tokenSecret, profile, done) {
done(null, false, {
accessToken: token,
refreshToken: tokenSecret,
profile: profile
})
}))
}
if (config.oauth.facebook.key) {
passport.use(new FacebookStrategy({
clientID: config.oauth.facebook.key,
clientSecret: config.oauth.facebook.secret,
callbackURL: config.oauth.facebook.callbackUrl,
profileFields: ['id', 'displayName', 'photos', 'email']
},
function(token, tokenSecret, profile, done) {
done(null, false, {
accessToken: token,
refreshToken: tokenSecret,
profile: profile
})
}))
}
passport.serializeUser(function(user, done) {
done(null, user._id);
});
passport.deserializeUser(function(id, done) {
app.db.models.User.getById(id)
.then(user => done(null, user))
.catch(error => done(error, null))
});
}