-
Notifications
You must be signed in to change notification settings - Fork 0
/
passport.js
78 lines (69 loc) · 1.98 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
import passport from 'passport'
import GooglePlusTokenStrategy from 'passport-google-plus-token'
import FacebookTokenStrategy from 'passport-facebook-token'
import User from './models/User.js'
// Facebook OAuth Strategy
passport.use(
'facebookToken',
new FacebookTokenStrategy(
{
clientID: process.env.FACEBOOK_ID,
clientSecret: process.env.FACEBOOK_SECRET
},
async (accessToken, refreshToken, profile, next) => {
try {
console.log('profile', profile)
console.log('accessToken', accessToken)
console.log('refreshToken', refreshToken)
const existingUser = await User.findOne({ 'facebook.id': profile.id })
if (existingUser) {
return next(null, existingUser)
}
const newUser = new User({
method: 'facebook',
facebook: {
id: profile.id,
email: JSON.stringify(profile.emails[0])
}
})
await newUser.save()
next(null, newUser)
} catch (error) {
next(error, false, error.message)
}
}
)
)
// Google OAUTH Strategy
passport.use(
'googleToken',
new GooglePlusTokenStrategy(
{
clientID: process.env.GOOGLE_ID,
clientSecret: process.env.GOOGLE_SECRET
},
async (accessToken, refreshToken, profile, next) => {
try {
// Should have full user profile over here
console.log('profile', profile)
console.log('accessToken', accessToken)
console.log('refreshToken', refreshToken)
const existingUser = await User.findOne({ 'google.id': profile.id })
if (existingUser) {
return next(null, existingUser)
}
const newUser = new User({
method: 'google',
google: {
id: profile.id,
email: JSON.stringify(profile.emails[0])
}
})
await newUser.save()
next(null, newUser)
} catch (error) {
next(error, false, error.message)
}
}
)
)