|
1 | 1 | import '../styles/globals.css'
|
2 | 2 | import React from 'react'
|
| 3 | +import { useEffect } from 'react' |
3 | 4 | import SuperTokensReact from 'supertokens-auth-react'
|
4 |
| -import ThirdPartyEmailPasswordReact from 'supertokens-auth-react/recipe/thirdpartyemailpassword' |
5 |
| -import SessionReact from 'supertokens-auth-react/recipe/session' |
| 5 | +import * as SuperTokensConfig from '../config/supertokensConfig' |
| 6 | +import Session from 'supertokens-auth-react/recipe/session' |
6 | 7 | import SuperTokensNode from 'supertokens-node'
|
7 |
| -import SessionNode from 'supertokens-node/recipe/session' |
8 |
| -import ThirdPartyEmailPasswordNode from 'supertokens-node/recipe/thirdpartyemailpassword' |
9 |
| -const port = process.env.APP_PORT || 3000 |
10 |
| -const websiteDomain = |
11 |
| - process.env.APP_URL || |
12 |
| - process.env.NEXT_PUBLIC_APP_URL || |
13 |
| - `http://localhost:${port}` |
14 |
| -const apiBasePath = '/api/auth/' |
| 8 | +import { redirectToAuth } from 'supertokens-auth-react/recipe/thirdpartyemailpassword' |
15 | 9 |
|
16 |
| -// Client Side configs. |
17 | 10 | if (typeof window !== 'undefined') {
|
18 |
| - SuperTokensReact.init({ |
19 |
| - useReactRouterDom: false, |
20 |
| - appInfo: { |
21 |
| - appName: 'SuperTokens Demo App', |
22 |
| - websiteDomain, |
23 |
| - apiDomain: websiteDomain, |
24 |
| - apiBasePath, |
25 |
| - }, |
26 |
| - recipeList: [ |
27 |
| - ThirdPartyEmailPasswordReact.init({ |
28 |
| - emailVerificationFeature: { |
29 |
| - mode: 'REQUIRED', |
30 |
| - }, |
31 |
| - signInAndUpFeature: { |
32 |
| - providers: [ |
33 |
| - ThirdPartyEmailPasswordReact.Google.init(), |
34 |
| - ThirdPartyEmailPasswordReact.Github.init(), |
35 |
| - ThirdPartyEmailPasswordReact.Facebook.init(), |
36 |
| - ], |
37 |
| - }, |
38 |
| - }), |
39 |
| - SessionReact.init(), |
40 |
| - ], |
41 |
| - }) |
| 11 | + SuperTokensReact.init(SuperTokensConfig.frontendConfig()) |
42 | 12 | } else {
|
43 |
| - // Server Side configs. |
44 |
| - SuperTokensNode.init({ |
45 |
| - supertokens: { |
46 |
| - connectionURI: 'https://try.supertokens.io', // Replace with your SuperTokens core instance. See https://supertokens.io/docs/emailpassword/quick-setup/supertokens-core/overview |
47 |
| - }, |
48 |
| - appInfo: { |
49 |
| - appName: 'SuperTokens Demo App', |
50 |
| - websiteDomain, |
51 |
| - apiDomain: websiteDomain, |
52 |
| - apiBasePath, |
53 |
| - }, |
54 |
| - recipeList: [ |
55 |
| - ThirdPartyEmailPasswordNode.init({ |
56 |
| - providers: [ |
57 |
| - ThirdPartyEmailPasswordNode.Google({ |
58 |
| - clientSecret: process.env.GOOGLE_CLIENT_SECRET, |
59 |
| - clientId: process.env.GOOGLE_CLIENT_ID, |
60 |
| - }), |
61 |
| - ThirdPartyEmailPasswordNode.Github({ |
62 |
| - clientSecret: process.env.GITHUB_CLIENT_SECRET, |
63 |
| - clientId: process.env.GITHUB_CLIENT_ID, |
64 |
| - }), |
65 |
| - ThirdPartyEmailPasswordNode.Facebook({ |
66 |
| - clientSecret: process.env.FACEBOOK_CLIENT_SECRET, |
67 |
| - clientId: process.env.FACEBOOK_CLIENT_ID, |
68 |
| - }), |
69 |
| - ], |
70 |
| - }), |
71 |
| - SessionNode.init(), |
72 |
| - ], |
73 |
| - isInServerlessEnv: true, |
74 |
| - }) |
| 13 | + SuperTokensNode.init(SuperTokensConfig.backendConfig()) |
75 | 14 | }
|
76 | 15 |
|
77 | 16 | function MyApp({ Component, pageProps }) {
|
| 17 | + useEffect(() => { |
| 18 | + async function doRefresh() { |
| 19 | + if (pageProps.fromSupertokens === 'needs-refresh') { |
| 20 | + if (await Session.attemptRefreshingSession()) { |
| 21 | + location.reload() |
| 22 | + } else { |
| 23 | + // user has been logged out |
| 24 | + redirectToAuth() |
| 25 | + } |
| 26 | + } |
| 27 | + } |
| 28 | + doRefresh() |
| 29 | + }, [pageProps.fromSupertokens]) |
| 30 | + if (pageProps.fromSupertokens === 'needs-refresh') { |
| 31 | + return null |
| 32 | + } |
78 | 33 | return <Component {...pageProps} />
|
79 | 34 | }
|
80 | 35 |
|
|
0 commit comments