| |
| |
| |
| |
| |
| |
| |
| |
| |
| import passport from 'passport'; |
| import { URL } from 'url'; |
| import isSpectrumUrl from '../../utils/is-spectrum-url'; |
|
|
| const IS_PROD = process.env.NODE_ENV === 'production'; |
| const FALLBACK_URL = IS_PROD |
| ? 'https://spectrum.chat/home' |
| : 'http://localhost:3000/home'; |
|
|
| type Strategy = 'twitter' | 'facebook' | 'github' | 'google'; |
|
|
| export const createSigninRoutes = ( |
| strategy: Strategy, |
| strategyOptions?: Object |
| ) => { |
| return { |
| |
| |
| main: (req: express$Request, ...rest: any) => { |
| let url = FALLBACK_URL; |
| if (typeof req.query.r === 'string' && isSpectrumUrl(req.query.r)) { |
| url = req.query.r; |
| } |
|
|
| |
| |
| req.session.redirectUrl = url; |
|
|
| return passport.authenticate(strategy, strategyOptions)(req, ...rest); |
| }, |
| |
| |
| callbacks: [ |
| passport.authenticate(strategy, { |
| failureRedirect: '/', |
| }), |
| (req: express$Request, res: express$Response) => { |
| |
| const redirectUrl = req.session.redirectUrl |
| ? new URL(req.session.redirectUrl) |
| : new URL(FALLBACK_URL); |
| redirectUrl.searchParams.append('authed', 'true'); |
| if (req.authInfo && req.authInfo.message) { |
| redirectUrl.searchParams.append( |
| 'toastMessage', |
| |
| req.authInfo.message |
| ); |
| redirectUrl.searchParams.append('toastType', 'error'); |
| } |
|
|
| |
| |
| |
| req.session.redirectUrl = undefined; |
| res.cookie('_now_no_cache', '1', { |
| maxAge: 315569260000, |
| sameSite: 'lax', |
| secure: false, |
| }); |
| return res.redirect(redirectUrl.href); |
| }, |
| ], |
| }; |
| }; |
|
|