-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassport.js
107 lines (91 loc) · 3.2 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
/**
* passport.js
*/
'use strict';
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
var FacebookStrategy = require('passport-facebook').Strategy;
// load the auth variables
var configAuth = require('./config/auth');
var User = require('./models/user');
module.exports = exports = function(app) {
app.use(passport.initialize());
app.use(passport.session());
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
// passport config
passport.use(new LocalStrategy(User.authenticate()));
// =========================================================================
// GOOGLE ==================================================================
// =========================================================================
passport.use(new GoogleStrategy({
clientID : configAuth.googleAuth.clientID,
clientSecret : configAuth.googleAuth.clientSecret,
callbackURL : configAuth.googleAuth.callbackURL,
},
function(token, refreshToken, profile, done) {
// make the code asynchronous
// User.findOne won't fire until we have all our data back from Google
process.nextTick(function() {
// try to find the user based on their google id
User.findOne({ 'google.id' : profile.id }, function(err, user) {
if (err)
return done(err);
if (user) {
// if a user is found, log them in
return done(null, user);
} else {
// if the user isnt in our database, create a new user
var newUser = new User({
// set all of the relevant information
google: {
id: profile.id,
token: token,
name: profile.name,
email: profile.emails[0].value,
displayName: profile.displayName
},
provider: 'google'
});
// save the user
newUser.save(function(err) {
if (err)
throw err;
return done(null, newUser);
});
}
});
});
}));
// =========================================================================
// FACEBOOK ================================================================
// =========================================================================
passport.use(new FacebookStrategy({
clientID: configAuth.facebookAuth.clientID,
clientSecret: configAuth.facebookAuth.clientSecret,
callbackURL: configAuth.facebookAuth.callbackURL,
passReqToCallback: true,
}, function(req, accessToken, refreshToken, profile, done) {
User.findOne({ 'facebook.id' : profile.id }, function(err, user) {
if (user) { return done(err, user); }
var newUser = new User({
facebook: {
id: profile.id,
name: profile.name,
displayName: profile.displayName
},
provider: 'facebook'
});
newUser.save(function() {
return done(null, newUser);
});
});
}));
};