-
Notifications
You must be signed in to change notification settings - Fork 1
/
passport.js
185 lines (165 loc) · 5.14 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const passport = require('passport');
const JwtStrategy = require('passport-jwt').Strategy;
const { ExtractJwt } = require('passport-jwt');
const LocalStrategy = require('passport-local').Strategy;
const GooglePlusTokenStrategy = require('passport-google-plus-token');
const FacebookTokenStrategy = require('passport-facebook-token');
const config = require('./configuration');
const User = require('./models/user');
const cookieExtractor = req => {
let token = null;
if (req && req.cookies) {
token = req.cookies['access_token'];
}
return token;
}
// JSON WEB TOKENS STRATEGY
passport.use(new JwtStrategy({
jwtFromRequest: cookieExtractor,
secretOrKey: config.JWT_SECRET,
passReqToCallback: true
}, async (req, payload, done) => {
try {
// Find the user specified in token
const user = await User.findById(payload.sub);
// If user doesn't exists, handle it
if (!user) {
return done(null, false);
}
// Otherwise, return the user
req.user = user;
done(null, user);
} catch(error) {
done(error, false);
}
}));
// Google OAuth Strategy
passport.use('googleToken', new GooglePlusTokenStrategy({
clientID: config.oauth.google.clientID,
clientSecret: config.oauth.google.clientSecret,
passReqToCallback: true,
}, async (req, accessToken, refreshToken, profile, done) => {
try {
// Could get accessed in two ways:
// 1) When registering for the first time
// 2) When linking account to the existing one
// Should have full user profile over here
console.log('profile', profile);
console.log('accessToken', accessToken);
console.log('refreshToken', refreshToken);
if (req.user) {
// We're already logged in, time for linking account!
// Add Google's data to an existing account
req.user.methods.push('google')
req.user.google = {
id: profile.id,
email: profile.emails[0].value
}
await req.user.save()
return done(null, req.user);
} else {
// We're in the account creation process
let existingUser = await User.findOne({ "google.id": profile.id });
if (existingUser) {
return done(null, existingUser);
}
// Check if we have someone with the same email
existingUser = await User.findOne({ "local.email": profile.emails[0].value })
if (existingUser) {
// We want to merge google's data with local auth
existingUser.methods.push('google')
existingUser.google = {
id: profile.id,
email: profile.emails[0].value
}
await existingUser.save()
return done(null, existingUser);
}
const newUser = new User({
methods: ['google'],
google: {
id: profile.id,
email: profile.emails[0].value
}
});
await newUser.save();
done(null, newUser);
}
} catch(error) {
done(error, false, error.message);
}
}));
passport.use('facebookToken', new FacebookTokenStrategy({
clientID: config.oauth.facebook.clientID,
clientSecret: config.oauth.facebook.clientSecret,
passReqToCallback: true
}, async (req, accessToken, refreshToken, profile, done) => {
try {
console.log('profile', profile);
console.log('accessToken', accessToken);
console.log('refreshToken', refreshToken);
if (req.user) {
// We're already logged in, time for linking account!
// Add Facebook's data to an existing account
req.user.methods.push('facebook')
req.user.facebook = {
id: profile.id,
email: profile.emails[0].value
}
await req.user.save();
return done(null, req.user);
} else {
// We're in the account creation process
let existingUser = await User.findOne({ "facebook.id": profile.id });
if (existingUser) {
return done(null, existingUser);
}
// Check if we have someone with the same email
existingUser = await User.findOne({ "local.email": profile.emails[0].value })
if (existingUser) {
// We want to merge facebook's data with local auth
existingUser.methods.push('facebook')
existingUser.facebook = {
id: profile.id,
email: profile.emails[0].value
}
await existingUser.save()
return done(null, existingUser);
}
const newUser = new User({
methods: ['facebook'],
facebook: {
id: profile.id,
email: profile.emails[0].value
}
});
await newUser.save();
done(null, newUser);
}
} catch(error) {
done(error, false, error.message);
}
}));
// LOCAL STRATEGY
passport.use(new LocalStrategy({
usernameField: 'email'
}, async (email, password, done) => {
try {
// Find the user given the email
const user = await User.findOne({ "local.email": email });
// If not, handle it
if (!user) {
return done(null, false);
}
// Check if the password is correct
const isMatch = await user.isValidPassword(password);
// If not, handle it
if (!isMatch) {
return done(null, false);
}
// Otherwise, return the user
done(null, user);
} catch(error) {
done(error, false);
}
}));