Skip to content

Commit

Permalink
Updates with-supertokens example: Fixes init race condition (#32706)
Browse files Browse the repository at this point in the history
Updates with-supertokens example to fix a race condition for initialising the supertokens-node SDK. Earlier we used to do it in _app.js in an async manner, so if `getServerSideProps` is called before the `supertokens.init` is called, the page load would fail. 

Now, we call `supertokens.init` (for the node SDK) in the `getServerSideProps` function itself.

Co-authored-by: kant01ne <5072452+kant01ne@users.noreply.github.com>
Co-authored-by: Bhumil Sarvaiya <21988812+bhumilsarvaiya@users.noreply.github.com>
Co-authored-by: Joel Coutinho <6310783+jscyo@users.noreply.github.com>
  • Loading branch information
4 people authored Dec 21, 2021
1 parent 3667eba commit f47a251
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 8 deletions.
8 changes: 0 additions & 8 deletions examples/with-supertokens/pages/_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,8 @@ import * as SuperTokensConfig from '../config/frontendConfig'
import Session from 'supertokens-auth-react/recipe/session'
import { redirectToAuth } from 'supertokens-auth-react/recipe/thirdpartyemailpassword'

async function initNode() {
const supertokensNode = await import('supertokens-node')
const { backendConfig } = await import('../config/backendConfig')
supertokensNode.init(backendConfig())
}

if (typeof window !== 'undefined') {
SuperTokensReact.init(SuperTokensConfig.frontendConfig())
} else {
initNode().catch(console.error)
}

function MyApp({ Component, pageProps }) {
Expand Down
4 changes: 4 additions & 0 deletions examples/with-supertokens/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import Head from 'next/head'
import styles from '../styles/Home.module.css'
import ThirdPartyEmailPassword from 'supertokens-auth-react/recipe/thirdpartyemailpassword'
import dynamic from 'next/dynamic'
import supertokensNode from 'supertokens-node'
import { backendConfig } from '../config/backendConfig'
import Session from 'supertokens-node/recipe/session'

const ThirdPartyEmailPasswordAuthNoSSR = dynamic(
Expand All @@ -13,6 +15,8 @@ const ThirdPartyEmailPasswordAuthNoSSR = dynamic(
)

export async function getServerSideProps(context) {
// this runs on the backend, so we must call init on supertokens-node SDK
supertokensNode.init(backendConfig())
let session
try {
session = await Session.getSession(context.req, context.res)
Expand Down

0 comments on commit f47a251

Please sign in to comment.