-
Notifications
You must be signed in to change notification settings - Fork 1
/
passport.js
56 lines (41 loc) · 1.55 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
var TwitterStrategy = require('passport-twitter').Strategy;
var User = require('./user'), //User model
config = require('./config'); //server configurations
module.exports = function(passport) {
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
passport.use(new TwitterStrategy({
consumerKey: config.consumerKey,
consumerSecret: config.consumerSecret,
callbackURL: "/auth/twitter/callback"
},
function(token, tokenSecret, profile, done) {
process.nextTick(function() {
User.findOne({ 'twitter.id' : profile.id }, function(err, user) {
if (err)
return done(err); //if error, return error
if (user) { //user exists
return done(null, user);
} else { //new user
var newUser = new User();
newUser.twitter.id = profile.id;
newUser.twitter.token = token;
newUser.twitter.username = profile.username;
newUser.twitter.displayName = profile.displayName;
//save user in database
newUser.save(function(err) {
if (err)
throw err;
return done(null, newUser);
});
}
});
});
}));
};