rsnext/examples/with-supertokens/pages/_app.js
Rishabh Poddar 41efb824f2
Docs update: Adds supertokens to 'Bring Your Own Database' section as well + improvements to code structure in example (#23779)
- Since SuperTokens is a self hosted solution, it can be added to 'Bring Your Own Database' as well (in https://nextjs.org/docs/authentication#supertokens)
- Improvements to code structure in the with-supertokens example
2021-04-20 17:23:32 +00:00

36 lines
1.1 KiB
JavaScript

import '../styles/globals.css'
import React from 'react'
import { useEffect } from 'react'
import SuperTokensReact from 'supertokens-auth-react'
import * as SuperTokensConfig from '../config/supertokensConfig'
import Session from 'supertokens-auth-react/recipe/session'
import SuperTokensNode from 'supertokens-node'
import { redirectToAuth } from 'supertokens-auth-react/recipe/thirdpartyemailpassword'
if (typeof window !== 'undefined') {
SuperTokensReact.init(SuperTokensConfig.frontendConfig())
} else {
SuperTokensNode.init(SuperTokensConfig.backendConfig())
}
function MyApp({ Component, pageProps }) {
useEffect(() => {
async function doRefresh() {
if (pageProps.fromSupertokens === 'needs-refresh') {
if (await Session.attemptRefreshingSession()) {
location.reload()
} else {
// user has been logged out
redirectToAuth()
}
}
}
doRefresh()
}, [pageProps.fromSupertokens])
if (pageProps.fromSupertokens === 'needs-refresh') {
return null
}
return <Component {...pageProps} />
}
export default MyApp