-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpassport.js
76 lines (69 loc) · 2.22 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
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var FacebookStrategy = require('passport-facebook').Strategy;
var config = require('./config');
passport.serializeUser(function(user, done) {
done(null, user._id);
});
passport.deserializeUser(function(id, done) {
User.findOne({ _id: id }, function(err, user) {
done(err, user);
});
});
passport.use(new LocalStrategy({
usernameField: 'email'
},
function(username, password, done) {
User.findOne({ email: username }, function(err, user) {
if (err) return done(err);
if (!user) {
return done(null, false, {
message: 'Incorrect username or password'
});
}
if (!user.validPassword(password)) {
return done(null, false, {
message: 'Incorrect username or password'
});
}
return done(null, user);
});
}
));
passport.use(new FacebookStrategy({
clientID: config.facebook.clientID,
clientSecret: config.facebook.clientSecret,
callbackURL: config.facebook.callback,
profileFields: ['id', 'displayName', 'email']
},
function(token, refreshToken, profile, done) {
User.findOne({ facebookId: profile.id }, function(err, user) {
if (err) return done(err);
if (user) {
return done(null, user); // usr already registered
}
else {
// see if user is existing in db but have not logged in via fb
User.findOne({ email: profile.emails[0].value }, function(err, user) {
// yes. just save fb profile id to that user
if (user) {
user.facebookId = profile.id;
return user.save(function(err) {
if (err) return done(null, false, { message: 'Error saving user info' });
return done(null, user);
});
}
// not, then create a new user on db
var user = new User();
user.name = profile.displayName;
user.email = profile.emails[0].value;
user.facebookId = profile.id;
user.save(function(err) {
if (err) return done(null, false, { message: 'Error saving user info' });
return done(null, user);
});
});
}
});
}
));