Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/with-supertokens/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
},
"dependencies": {
"next": "latest",
"prettier": "^2.7.1",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"supertokens-auth-react": "^0.21.2",
"supertokens-auth-react": "github:supertokens/supertokens-auth-react#next-breaking-change",
"supertokens-node": "^9.1.1"
}
}
9 changes: 7 additions & 2 deletions examples/with-supertokens/pages/_app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import '../styles/globals.css'
import React from 'react'
import { useEffect } from 'react'
import SuperTokensReact from 'supertokens-auth-react'
import SuperTokensReact, { SuperTokensWrapper } from 'supertokens-auth-react'
import * as SuperTokensConfig from '../config/frontendConfig'
import Session from 'supertokens-auth-react/recipe/session'
import { redirectToAuth } from 'supertokens-auth-react/recipe/thirdpartyemailpassword'
Expand All @@ -27,7 +27,12 @@ function MyApp({ Component, pageProps }) {
if (pageProps.fromSupertokens === 'needs-refresh') {
return null
}
return <Component {...pageProps} />

return (
<SuperTokensWrapper>
<Component {...pageProps} />
</SuperTokensWrapper>
)
}

export default MyApp
32 changes: 19 additions & 13 deletions examples/with-supertokens/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import React from 'react'
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 ThirdPartyEmailPassword, {
ThirdPartyEmailPasswordAuth,
} from 'supertokens-auth-react/recipe/thirdpartyemailpassword'
import supertokensNode from 'supertokens-node'
import { backendConfig } from '../config/backendConfig'
import Session from 'supertokens-node/recipe/session'

const ThirdPartyEmailPasswordAuthNoSSR = dynamic(
new Promise((res) =>
res(ThirdPartyEmailPassword.ThirdPartyEmailPasswordAuth)
),
{ ssr: false }
)
import { useSessionContext } from 'supertokens-auth-react/recipe/session'

export async function getServerSideProps(context) {
// this runs on the backend, so we must call init on supertokens-node SDK
Expand All @@ -37,13 +32,15 @@ export async function getServerSideProps(context) {

export default function Home(props) {
return (
<ThirdPartyEmailPasswordAuthNoSSR>
<ThirdPartyEmailPasswordAuth>
<ProtectedPage userId={props.userId} />
</ThirdPartyEmailPasswordAuthNoSSR>
</ThirdPartyEmailPasswordAuth>
)
}

function ProtectedPage({ userId }) {
const session = useSessionContext()

async function logoutClicked() {
await ThirdPartyEmailPassword.signOut()
ThirdPartyEmailPassword.redirectToAuth()
Expand All @@ -57,21 +54,30 @@ function ProtectedPage({ userId }) {
}
}

if (session.loading === true) {
return null
}

return (
<div className={styles.container}>
<Head>
<title>SuperTokens 💫</title>
<link rel="icon" href="/favicon.ico" />
</Head>

<main className={styles.main}>
<h1 className={styles.title}>
Welcome to <a href="https://nextjs.org">Next.js!</a>
</h1>
<p className={styles.description}>
You are authenticated with SuperTokens! (UserID: {userId})
You are authenticated with SuperTokens!
</p>

<p className={styles.description}>
UserId: {session.userId} <br /> (from SSR: {userId})
</p>
<p className={styles.description}>
Access token payload: {JSON.stringify(session.accessTokenPayload)}
</p>
<div
style={{
display: 'flex',
Expand Down