-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthenticate.js
42 lines (38 loc) · 1.34 KB
/
authenticate.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
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var FacebookStrategy = require('passport-facebook').Strategy;
var User = require('./models/user');
var config = require('./config');
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser((User.serializeUser()));
passport.deserializeUser((User.deserializeUser()));
exports.facebook = passport.use(new FacebookStrategy({
clientID: config.facebook.clientID,
clientSecret: config.facebook.clientSecret,
callbackURL: config.facebook.callbackURL
},
function (accessToken, refreshToken, profile, done) {
User.findOne({OauthId: profile.id}, function (err, user) {
if(err) {
console.log(err);
}
if (!err && user !== null){
done(null, user);
}else{
user = new User({
username: profile.displayName
});
user.OauthId = profile.id;
user.OauthToken = accessToken;
user.save(function (err) {
if (err){
console.log(err);
}else{
console.log('saving user');
done(null, user);
}
});
}
});
}
));