-
Notifications
You must be signed in to change notification settings - Fork 1
/
passport.js
145 lines (129 loc) · 4.4 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
/**
* Node.js API Starter Kit (https://reactstarter.com/nodejs)
*
* Copyright © 2016-present Kriasoft, LLC. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE.txt file in the root directory of this source tree.
*/
/* @flow */
/* eslint-disable no-param-reassign, no-underscore-dangle, max-len */
import passport from 'passport';
import { OAuth2Strategy as GoogleStrategy } from 'passport-google-oauth';
import { Strategy as FacebookStrategy } from 'passport-facebook';
import { Strategy as TwitterStrategy } from 'passport-twitter';
import db from './db';
passport.serializeUser((user, done) => {
done(null, {
id: user.id,
displayName: user.displayName,
imageUrl: user.imageUrl,
emails: user.emails,
});
});
passport.deserializeUser((user, done) => {
done(null, user);
});
// Creates or updates the external login credentials
// and returns the currently authenticated user.
async function login(req, provider, profile, tokens) {
let user;
if (req.user) {
user = await db.table('users').where({ id: req.user.id }).first();
}
if (!user) {
user = await db.table('logins')
.innerJoin('users', 'users.id', 'logins.user_id')
.where({ 'logins.provider': provider, 'logins.id': profile.id })
.first('users.*');
if (!user && profile.emails && profile.emails.length && profile.emails[0].verified === true) {
user = await db.table('users')
.where('emails', '@>', JSON.stringify([{ email: profile.emails[0].value, verified: true }]))
.first();
}
}
if (!user) {
user = (await db.table('users')
.insert({
display_name: profile.displayName,
emails: JSON.stringify((profile.emails || []).map(x => ({
email: x.value,
verified: x.verified || false,
}))),
image_url: profile.photos && profile.photos.length ? profile.photos[0].value : null,
})
.returning('*'))[0];
}
const loginKeys = { user_id: user.id, provider, id: profile.id };
const { count } = await db.table('logins').where(loginKeys).count('id').first();
if (count === '0') {
await db.table('logins').insert({
...loginKeys,
username: profile.username,
tokens: JSON.stringify(tokens),
profile: JSON.stringify(profile._json),
});
} else {
await db.table('logins').where(loginKeys).update({
username: profile.username,
tokens: JSON.stringify(tokens),
profile: JSON.stringify(profile._json),
updated_at: db.raw('CURRENT_TIMESTAMP'),
});
}
return {
id: user.id,
displayName: user.display_name,
imageUrl: user.image_url,
emails: user.emails,
};
}
// https://github.com/jaredhanson/passport-google-oauth2
passport.use(new GoogleStrategy({
clientID: process.env.GOOGLE_ID,
clientSecret: process.env.GOOGLE_SECRET,
callbackURL: '/login/google/return',
passReqToCallback: true,
}, async (req, accessToken, refreshToken, profile, done) => {
try {
const user = await login(req, 'google', profile, { accessToken, refreshToken });
done(null, user);
} catch (err) {
done(err);
}
}));
// https://github.com/jaredhanson/passport-facebook
passport.use(new FacebookStrategy({
clientID: process.env.FACEBOOK_ID,
clientSecret: process.env.FACEBOOK_SECRET,
profileFields: ['name', 'email', 'picture', 'link', 'locale', 'timezone', 'verified'],
callbackURL: '/login/facebook/return',
passReqToCallback: true,
}, async (req, accessToken, refreshToken, profile, done) => {
try {
if (profile.emails.length) profile.emails[0].verified = !!profile._json.verified;
profile.displayName = profile.displayName || `${profile.name.givenName} ${profile.name.familyName}`;
const user = await login(req, 'facebook', profile, { accessToken, refreshToken });
done(null, user);
} catch (err) {
done(err);
}
}));
// https://github.com/jaredhanson/passport-twitter
passport.use(new TwitterStrategy({
consumerKey: process.env.TWITTER_KEY,
consumerSecret: process.env.TWITTER_SECRET,
callbackURL: '/login/twitter/return',
includeEmail: true,
includeStatus: false,
passReqToCallback: true,
}, async (req, token, tokenSecret, profile, done) => {
try {
if (profile.emails && profile.emails.length) profile.emails[0].verified = true;
const user = await login(req, 'twitter', profile, { token, tokenSecret });
done(null, user);
} catch (err) {
done(err);
}
}));
export default passport;