File size: 2,465 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/* @flow
 *
 * A reusable set of routes for signing in with different providers. Handles token-based authentication.
 * Usage:
 *
 * const { main, callbacks } = createSigninRoutes('facebook');
 * facebookRouter.get('/', main);
 * facebookRouter.get('/callback', ...callbacks);
 */
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 {
    // The main route takes care of storing the redirect URL in the session
    // and passing the right options
    main: (req: express$Request, ...rest: any) => {
      let url = FALLBACK_URL;
      if (typeof req.query.r === 'string' && isSpectrumUrl(req.query.r)) {
        url = req.query.r;
      }

      // Attach the redirectURL and authType to the session so we have it in the /auth/twitter/callback route
      // $FlowIssue
      req.session.redirectUrl = url;

      return passport.authenticate(strategy, strategyOptions)(req, ...rest);
    },
    // The callbacks take care of authenticating, setting the response cookies,
    // redirecting to the right place and handling tokens
    callbacks: [
      passport.authenticate(strategy, {
        failureRedirect: '/',
      }),
      (req: express$Request, res: express$Response) => {
        // $FlowIssue
        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',
            // $FlowIssue
            req.authInfo.message
          );
          redirectUrl.searchParams.append('toastType', 'error');
        }

        // Delete the redirectURL from the session again so we don't redirect
        // to the old URL the next time around
        // $FlowIssue
        req.session.redirectUrl = undefined;
        res.cookie('_now_no_cache', '1', {
          maxAge: 315569260000, // 10 years
          sameSite: 'lax',
          secure: false,
        });
        return res.redirect(redirectUrl.href);
      },
    ],
  };
};