-
Notifications
You must be signed in to change notification settings - Fork 8.1k
/
passport.js
661 lines (639 loc) · 25.1 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
const passport = require('passport');
const refresh = require('passport-oauth2-refresh');
const axios = require('axios');
const { Strategy: LocalStrategy } = require('passport-local');
const { Strategy: FacebookStrategy } = require('passport-facebook');
const { Strategy: SnapchatStrategy } = require('passport-snapchat');
const { Strategy: TwitterStrategy } = require('@passport-js/passport-twitter');
const { Strategy: TwitchStrategy } = require('twitch-passport');
const { Strategy: GitHubStrategy } = require('passport-github2');
const { OAuth2Strategy: GoogleStrategy } = require('passport-google-oauth');
const { Strategy: LinkedInStrategy } = require('passport-linkedin-oauth2');
const { SteamOpenIdStrategy } = require('passport-steam-openid');
const { OAuthStrategy } = require('passport-oauth');
const { OAuth2Strategy } = require('passport-oauth');
const _ = require('lodash');
const moment = require('moment');
const User = require('../models/User');
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser(async (id, done) => {
try {
return done(null, await User.findById(id));
} catch (error) {
return done(error);
}
});
/**
* Sign in using Email and Password.
*/
passport.use(new LocalStrategy({ usernameField: 'email' }, (email, password, done) => {
User.findOne({ email: email.toLowerCase() })
.then((user) => {
if (!user) {
return done(null, false, { msg: `Email ${email} not found.` });
}
if (!user.password) {
return done(null, false, { msg: 'Your account was registered using a sign-in provider. To enable password login, sign in using a provider, and then set a password under your user profile.' });
}
user.comparePassword(password, (err, isMatch) => {
if (err) { return done(err); }
if (isMatch) {
return done(null, user);
}
return done(null, false, { msg: 'Invalid email or password.' });
});
})
.catch((err) => done(err));
}));
/**
* OAuth Strategy Overview
*
* - User is already logged in.
* - Check if there is an existing account with a provider id.
* - If there is, return an error message. (Account merging not supported)
* - Else link new OAuth account with currently logged-in user.
* - User is not logged in.
* - Check if it's a returning user.
* - If returning user, sign in and we are done.
* - Else check if there is an existing account with user's email.
* - If there is, return an error message.
* - Else create a new account.
*/
/**
* Sign in with Snapchat.
*/
passport.use(new SnapchatStrategy({
clientID: process.env.SNAPCHAT_ID,
clientSecret: process.env.SNAPCHAT_SECRET,
callbackURL: '/auth/snapchat/callback',
profileFields: ['id', 'displayName', 'bitmoji'],
scope: ['user.display_name', 'user.bitmoji.avatar'],
passReqToCallback: true
}, async (req, accessToken, refreshToken, profile, done) => {
try {
if (req.user) {
const existingUser = await User.findOne({ snapchat: profile.id });
if (existingUser) {
req.flash('errors', { msg: 'There is already a Snapchat account that belongs to you. Sign in with that account or delete it, then link it with your current account.' });
return done(null, existingUser);
}
const user = await User.findById(req.user.id);
user.snapchat = profile.id;
user.tokens.push({ kind: 'snapchat', accessToken });
user.profile.name = user.profile.name || profile.displayName;
user.profile.picture = user.profile.picture || profile.bitmoji.avatarUrl;
await user.save();
req.flash('info', { msg: 'Snapchat account has been linked.' });
return done(null, user);
}
const existingUser = await User.findOne({ snapchat: profile.id });
if (existingUser) {
return done(null, existingUser);
}
const user = new User();
// Assign a temporary e-mail address
// to get on with the registration process. It can be changed later
// to a valid e-mail address in Profile Management.
user.email = `${profile.id}@snapchat.com`;
user.snapchat = profile.id;
user.tokens.push({ kind: 'snapchat', accessToken });
user.profile.name = profile.displayName;
user.profile.picture = profile.bitmoji.avatarUrl;
await user.save();
return done(null, user);
} catch (err) {
return done(err);
}
}));
/**
* Sign in with Facebook.
*/
passport.use(new FacebookStrategy({
clientID: process.env.FACEBOOK_ID,
clientSecret: process.env.FACEBOOK_SECRET,
callbackURL: `${process.env.BASE_URL}/auth/facebook/callback`,
profileFields: ['name', 'email', 'link', 'locale', 'timezone', 'gender'],
passReqToCallback: true
}, async (req, accessToken, refreshToken, profile, done) => {
try {
if (req.user) {
const existingUser = await User.findOne({ facebook: profile.id });
if (existingUser) {
req.flash('errors', { msg: 'There is already a Facebook account that belongs to you. Sign in with that account or delete it, then link it with your current account.' });
return done(null, existingUser);
}
const user = await User.findById(req.user.id);
user.facebook = profile.id;
user.tokens.push({ kind: 'facebook', accessToken });
user.profile.name = user.profile.name || `${profile.name.givenName} ${profile.name.familyName}`;
user.profile.gender = user.profile.gender || profile._json.gender;
user.profile.picture = user.profile.picture || `https://graph.facebook.com/${profile.id}/picture?type=large`;
await user.save();
req.flash('info', { msg: 'Facebook account has been linked.' });
return done(null, user);
}
const existingUser = await User.findOne({ facebook: profile.id });
if (existingUser) {
return done(null, existingUser);
}
const existingEmailUser = await User.findOne({ email: profile._json.email });
if (existingEmailUser) {
req.flash('errors', { msg: 'There is already an account using this email address. Sign in to that account and link it with Facebook manually from Account Settings.' });
return done(null, existingEmailUser);
}
const user = new User();
user.email = profile._json.email;
user.facebook = profile.id;
user.tokens.push({ kind: 'facebook', accessToken });
user.profile.name = `${profile.name.givenName} ${profile.name.familyName}`;
user.profile.gender = profile._json.gender;
user.profile.picture = `https://graph.facebook.com/${profile.id}/picture?type=large`;
user.profile.location = profile._json.location ? profile._json.location.name : '';
await user.save();
return done(null, user);
} catch (err) {
return done(err);
}
}));
/**
* Sign in with GitHub.
*/
passport.use(new GitHubStrategy({
clientID: process.env.GITHUB_ID,
clientSecret: process.env.GITHUB_SECRET,
callbackURL: `${process.env.BASE_URL}/auth/github/callback`,
passReqToCallback: true,
scope: ['user:email']
}, async (req, accessToken, refreshToken, profile, done) => {
try {
if (req.user) {
const existingUser = await User.findOne({ github: profile.id });
if (existingUser) {
req.flash('errors', { msg: 'There is already a GitHub account that belongs to you. Sign in with that account or delete it, then link it with your current account.' });
return done(null, existingUser);
}
const user = await User.findById(req.user.id);
user.github = profile.id;
user.tokens.push({ kind: 'github', accessToken });
user.profile.name = user.profile.name || profile.displayName;
user.profile.picture = user.profile.picture || profile._json.avatar_url;
user.profile.location = user.profile.location || profile._json.location;
user.profile.website = user.profile.website || profile._json.blog;
await user.save();
req.flash('info', { msg: 'GitHub account has been linked.' });
return done(null, user);
}
const existingUser = await User.findOne({ github: profile.id });
if (existingUser) {
return done(null, existingUser);
}
const emailValue = _.get(_.orderBy(profile.emails, ['primary', 'verified'], ['desc', 'desc']), [0, 'value'], null);
if (profile._json.email === null) {
const existingEmailUser = await User.findOne({ email: emailValue });
if (existingEmailUser) {
req.flash('errors', { msg: 'There is already an account using this email address. Sign in to that account and link it with GitHub manually from Account Settings.' });
return done(null, existingEmailUser);
}
} else {
const existingEmailUser = await User.findOne({ email: profile._json.email });
if (existingEmailUser) {
req.flash('errors', { msg: 'There is already an account using this email address. Sign in to that account and link it with GitHub manually from Account Settings.' });
return done(null, existingEmailUser);
}
}
const user = new User();
user.email = emailValue;
user.github = profile.id;
user.tokens.push({ kind: 'github', accessToken });
user.profile.name = profile.displayName;
user.profile.picture = profile._json.avatar_url;
user.profile.location = profile._json.location;
user.profile.website = profile._json.blog;
await user.save();
return done(null, user);
} catch (err) {
return done(err);
}
}));
/**
* Sign in with Twitter.
*/
passport.use(new TwitterStrategy({
consumerKey: process.env.TWITTER_KEY,
consumerSecret: process.env.TWITTER_SECRET,
callbackURL: `${process.env.BASE_URL}/auth/twitter/callback`,
passReqToCallback: true
}, async (req, accessToken, tokenSecret, profile, done) => {
try {
if (req.user) {
const existingUser = await User.findOne({ twitter: profile.id });
if (existingUser) {
req.flash('errors', { msg: 'There is already a Twitter account that belongs to you. Sign in with that account or delete it, then link it with your current account.' });
return done(null, existingUser);
}
const user = await User.findById(req.user.id);
user.twitter = profile.id;
user.tokens.push({ kind: 'twitter', accessToken, tokenSecret });
user.profile.name = user.profile.name || profile.displayName;
user.profile.location = user.profile.location || profile._json.location;
user.profile.picture = user.profile.picture || profile._json.profile_image_url_https;
await user.save();
req.flash('info', { msg: 'Twitter account has been linked.' });
return done(null, user);
}
const existingUser = await User.findOne({ twitter: profile.id });
if (existingUser) {
return done(null, existingUser);
}
const user = new User();
// Twitter will not provide an email address. Period.
// But a person’s twitter username is guaranteed to be unique
// so we can "fake" a twitter email address as follows:
user.email = `${profile.username}@twitter.com`;
user.twitter = profile.id;
user.tokens.push({ kind: 'twitter', accessToken, tokenSecret });
user.profile.name = profile.displayName;
user.profile.location = profile._json.location;
user.profile.picture = profile._json.profile_image_url_https;
await user.save();
return done(null, user);
} catch (err) {
return done(err);
}
}));
/**
* Sign in with Google.
*/
const googleStrategyConfig = new GoogleStrategy({
clientID: process.env.GOOGLE_ID,
clientSecret: process.env.GOOGLE_SECRET,
callbackURL: '/auth/google/callback',
passReqToCallback: true
}, async (req, accessToken, refreshToken, params, profile, done) => {
try {
if (req.user) {
const existingUser = await User.findOne({ google: profile.id });
if (existingUser && (existingUser.id !== req.user.id)) {
req.flash('errors', { msg: 'There is already a Google account that belongs to you. Sign in with that account or delete it, then link it with your current account.' });
return done(null, existingUser);
}
const user = await User.findById(req.user.id);
user.google = profile.id;
user.tokens.push({
kind: 'google',
accessToken,
accessTokenExpires: moment().add(params.expires_in, 'seconds').format(),
refreshToken,
});
user.profile.name = user.profile.name || profile.displayName;
user.profile.gender = user.profile.gender || profile._json.gender;
user.profile.picture = user.profile.picture || profile._json.picture;
await user.save();
req.flash('info', { msg: 'Google account has been linked.' });
return done(null, user);
}
const existingUser = await User.findOne({ google: profile.id });
if (existingUser) {
return done(null, existingUser);
}
const existingEmailUser = await User.findOne({ email: profile.emails[0].value });
if (existingEmailUser) {
req.flash('errors', { msg: 'There is already an account using this email address. Sign in to that account and link it with Google manually from Account Settings.' });
return done(null, existingEmailUser);
}
const user = new User();
user.email = profile.emails[0].value;
user.google = profile.id;
user.tokens.push({
kind: 'google',
accessToken,
accessTokenExpires: moment().add(params.expires_in, 'seconds').format(),
refreshToken,
});
user.profile.name = profile.displayName;
user.profile.gender = profile._json.gender;
user.profile.picture = profile._json.picture;
await user.save();
return done(null, user);
} catch (err) {
return done(err);
}
});
passport.use('google', googleStrategyConfig);
refresh.use('google', googleStrategyConfig);
/**
* Sign in with LinkedIn.
*/
passport.use(new LinkedInStrategy({
clientID: process.env.LINKEDIN_ID,
clientSecret: process.env.LINKEDIN_SECRET,
callbackURL: `${process.env.BASE_URL}/auth/linkedin/callback`,
scope: ['r_liteprofile', 'r_emailaddress'],
passReqToCallback: true
}, async (req, accessToken, refreshToken, profile, done) => {
try {
if (req.user) {
const existingUser = await User.findOne({ linkedin: profile.id });
if (existingUser) {
req.flash('errors', { msg: 'There is already a LinkedIn account that belongs to you. Sign in with that account or delete it, then link it with your current account.' });
return done(null, existingUser);
}
const user = await User.findById(req.user.id);
user.linkedin = profile.id;
user.tokens.push({ kind: 'linkedin', accessToken });
user.profile.name = user.profile.name || profile.displayName;
user.profile.picture = user.profile.picture || profile.photos[3].value;
await user.save();
req.flash('info', { msg: 'LinkedIn account has been linked.' });
return done(null, user);
}
const existingUser = await User.findOne({ linkedin: profile.id });
if (existingUser) {
return done(null, existingUser);
}
const existingEmailUser = await User.findOne({ email: profile.emails[0].value });
if (existingEmailUser) {
req.flash('errors', { msg: 'There is already an account using this email address. Sign in to that account and link it with LinkedIn manually from Account Settings.' });
return done(null, existingEmailUser);
}
const user = new User();
user.linkedin = profile.id;
user.tokens.push({ kind: 'linkedin', accessToken });
user.email = profile.emails[0].value;
user.profile.name = profile.displayName;
user.profile.picture = user.profile.picture || profile.photos[3].value;
await user.save();
return done(null, user);
} catch (err) {
return done(err);
}
}));
/**
* Twitch API OAuth.
*/
const twitchStrategyConfig = new TwitchStrategy({
clientID: process.env.TWITCH_CLIENT_ID,
clientSecret: process.env.TWITCH_CLIENT_SECRET,
callbackURL: `${process.env.BASE_URL}/auth/twitch/callback`,
scope: ['user_read', 'chat:read', 'chat:edit', 'whispers:read', 'whispers:edit', 'user:read:email'],
passReqToCallback: true
}, async (req, accessToken, refreshToken, params, profile, done) => {
try {
if (req.user) {
const existingUser = await User.findOne({ twitch: profile.id });
if (existingUser && existingUser.id !== req.user.id) {
req.flash('errors', { msg: 'There is already a Twitch account that belongs to you. Sign in with that account or delete it, then link it with your current account.' });
return done(null, existingUser);
}
const user = await User.findById(req.user.id);
user.twitch = profile.id;
user.tokens.push({
kind: 'twitch',
accessToken,
accessTokenExpires: moment().add(params.expires_in, 'seconds').format(),
refreshToken,
});
user.profile.name = user.profile.name || profile.display_name;
user.profile.email = user.profile.gender || profile.email;
user.profile.picture = user.profile.picture || profile.profile_image_url;
await user.save();
req.flash('info', { msg: 'Twitch account has been linked.' });
return done(null, user);
}
const existingUser = await User.findOne({ twitch: profile.id });
if (existingUser) {
return done(null, existingUser);
}
const existingEmailUser = await User.findOne({ email: profile.email });
if (existingEmailUser) {
req.flash('errors', { msg: 'There is already an account using this email address. Sign in to that account and link it with Twitch manually from Account Settings.' });
return done(null, existingEmailUser);
}
const user = new User();
user.email = profile.email;
user.twitch = profile.id;
user.tokens.push({
kind: 'twitch',
accessToken,
accessTokenExpires: moment().add(params.expires_in, 'seconds').format(),
refreshToken,
});
user.profile.name = profile.display_name;
user.profile.email = profile.email;
user.profile.picture = profile.profile_image_url;
await user.save();
return done(null, user);
} catch (err) {
return done(err);
}
});
passport.use('twitch', twitchStrategyConfig);
refresh.use('twitch', twitchStrategyConfig);
/**
* Tumblr API OAuth.
*/
passport.use('tumblr', new OAuthStrategy({
requestTokenURL: 'https://www.tumblr.com/oauth/request_token',
accessTokenURL: 'https://www.tumblr.com/oauth/access_token',
userAuthorizationURL: 'https://www.tumblr.com/oauth/authorize',
consumerKey: process.env.TUMBLR_KEY,
consumerSecret: process.env.TUMBLR_SECRET,
callbackURL: '/auth/tumblr/callback',
passReqToCallback: true
},
async (req, token, tokenSecret, profile, done) => {
try {
const user = await User.findById(req.user._id);
user.tokens.push({ kind: 'tumblr', accessToken: token, tokenSecret });
await user.save();
return done(null, user);
} catch (err) {
return done(err);
}
}));
/**
* Foursquare API OAuth.
*/
passport.use('foursquare', new OAuth2Strategy({
authorizationURL: 'https://foursquare.com/oauth2/authorize',
tokenURL: 'https://foursquare.com/oauth2/access_token',
clientID: process.env.FOURSQUARE_ID,
clientSecret: process.env.FOURSQUARE_SECRET,
callbackURL: `${process.env.BASE_URL}/auth/foursquare/callback`,
passReqToCallback: true
},
async (req, accessToken, refreshToken, profile, done) => {
try {
const user = await User.findById(req.user._id);
user.tokens.push({ kind: 'foursquare', accessToken });
await user.save();
return done(null, user);
} catch (err) {
return done(err);
}
}));
/**
* Steam API OpenID.
*/
passport.use(new SteamOpenIdStrategy({
apiKey: process.env.STEAM_KEY,
returnURL: `${process.env.BASE_URL}/auth/steam/callback`,
profile: true,
}, async (req, identifier, profile, done) => {
const steamId = identifier.match(/\d+$/)[0];
const profileURL = `http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=${process.env.STEAM_KEY}&steamids=${steamId}`;
try {
if (req.user) {
const existingUser = await User.findOne({ steam: steamId });
if (existingUser) {
req.flash('errors', { msg: 'There is already an account associated with the SteamID. Sign in with that account or delete it, then link it with your current account.' });
return done(null, existingUser);
}
const user = await User.findById(req.user.id);
user.steam = steamId;
user.tokens.push({ kind: 'steam', accessToken: steamId });
try {
const res = await axios.get(profileURL);
const profileData = res.data.response.players[0];
user.profile.name = user.profile.name || profileData.personaname;
user.profile.picture = user.profile.picture || profileData.avatarmedium;
await user.save();
return done(null, user);
} catch (err) {
console.log(err);
await user.save();
return done(err, user);
}
} else {
try {
const { data } = await axios.get(profileURL);
const profileData = data.response.players[0];
const user = new User();
user.steam = steamId;
user.email = `${steamId}@steam.com`; // steam does not disclose emails, prevent duplicate keys
user.tokens.push({ kind: 'steam', accessToken: steamId });
user.profile.name = profileData.personaname;
user.profile.picture = profileData.avatarmedium;
await user.save();
return done(null, user);
} catch (err) {
return done(err, null);
}
}
} catch (err) {
return done(err);
}
}));
/**
* Pinterest API OAuth.
*/
passport.use('pinterest', new OAuth2Strategy({
authorizationURL: 'https://api.pinterest.com/oauth/',
tokenURL: 'https://api.pinterest.com/v1/oauth/token',
clientID: process.env.PINTEREST_ID,
clientSecret: process.env.PINTEREST_SECRET,
callbackURL: `${process.env.BASE_URL}/auth/pinterest/callback`,
passReqToCallback: true
},
async (req, accessToken, refreshToken, profile, done) => {
try {
const user = await User.findOne(req.user._id);
user.tokens.push({ kind: 'pinterest', accessToken });
await user.save();
return done(null, user);
} catch (err) {
return done(err);
}
}));
/**
* Intuit/QuickBooks API OAuth.
*/
const quickbooksStrategyConfig = new OAuth2Strategy({
authorizationURL: 'https://appcenter.intuit.com/connect/oauth2',
tokenURL: 'https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer',
clientID: process.env.QUICKBOOKS_CLIENT_ID,
clientSecret: process.env.QUICKBOOKS_CLIENT_SECRET,
callbackURL: `${process.env.BASE_URL}/auth/quickbooks/callback`,
passReqToCallback: true,
},
async (req, accessToken, refreshToken, params, profile, done) => {
try {
const user = await User.findById(req.user._id);
user.quickbooks = req.query.realmId;
const quickbooksToken = user.tokens.find((vendor) => vendor.kind === 'quickbooks');
if (quickbooksToken) {
quickbooksToken.accessToken = accessToken;
quickbooksToken.accessTokenExpires = moment().add(params.expires_in, 'seconds').format();
quickbooksToken.refreshToken = refreshToken;
quickbooksToken.refreshTokenExpires = moment().add(params.x_refresh_token_expires_in, 'seconds').format();
if (params.expires_in) quickbooksToken.accessTokenExpires = moment().add(params.expires_in, 'seconds').format();
} else {
user.tokens.push({
kind: 'quickbooks',
accessToken,
accessTokenExpires: moment().add(params.expires_in, 'seconds').format(),
refreshToken,
refreshTokenExpires: moment().add(params.x_refresh_token_expires_in, 'seconds').format(),
});
}
user.markModified('tokens');
await user.save();
return done(null, user);
} catch (err) {
return done(err);
}
});
passport.use('quickbooks', quickbooksStrategyConfig);
refresh.use('quickbooks', quickbooksStrategyConfig);
/**
* Login Required middleware.
*/
exports.isAuthenticated = (req, res, next) => {
if (req.isAuthenticated()) {
return next();
}
res.redirect('/login');
};
/**
* Authorization Required middleware.
*/
exports.isAuthorized = async (req, res, next) => {
const provider = req.path.split('/')[2];
const token = req.user.tokens.find((token) => token.kind === provider);
if (token) {
if (token.accessTokenExpires && moment(token.accessTokenExpires).isBefore(moment().subtract(1, 'minutes'))) {
if (token.refreshToken) {
if (token.refreshTokenExpires && moment(token.refreshTokenExpires).isBefore(moment().subtract(1, 'minutes'))) {
return res.redirect(`/auth/${provider}`);
}
try {
const newTokens = await new Promise((resolve, reject) => {
refresh.requestNewAccessToken(`${provider}`, token.refreshToken, (err, accessToken, refreshToken, params) => {
if (err) reject(err);
resolve({ accessToken, refreshToken, params });
});
});
req.user.tokens.forEach((tokenObject) => {
if (tokenObject.kind === provider) {
tokenObject.accessToken = newTokens.accessToken;
if (newTokens.params.expires_in) tokenObject.accessTokenExpires = moment().add(newTokens.params.expires_in, 'seconds').format();
}
});
await req.user.save();
return next();
} catch (err) {
console.log(err);
return next();
}
} else {
return res.redirect(`/auth/${provider}`);
}
} else {
return next();
}
} else {
return res.redirect(`/auth/${provider}`);
}
};