Skip to content

Commit b83107c

Browse files
authoredJul 6, 2022
Updates with-supertokens example (#38369)
* add-supertokens-to-authentication.md * bump supertokens deps * update SuperTokens Auth doc * Update docs/authentication.md Co-authored-by: Lee Robinson <me@leerob.io> * Update docs/authentication.md Co-authored-by: Lee Robinson <me@leerob.io> * updates supertokens dependency and optimises for serverless execution * runs prettier-fix * adds supertokens to 'Bring Your Own Database' section as well * does not show home page if not logged in * extracts config into its own file and calls it in all serverless functions * removes need for backend init in app.jsx * simplifies use of dynamic * refreshes page after getServerSideProps * removes unnecessary check in API * update to docs pertaining SuperTokens * adds placeholder secrets so that the UI loads on first run * changes to readme * updates version of supertokens frontend and backend SDK, and a few other fixes * Update docs/authentication.md Co-authored-by: Lee Robinson <me@leerob.io> * updates to readme for supertokens example * updates version of dependency * updates dependency version * updates to dependencies * removes unnecessary config on frontend * changes how redirection is done post signout * update to dependency * updates examples * updates code to use for new package * updates dependencies * updates auth-react package * with-supertokens example updated to use supertokens-node v7 * updates dependency * updates supertokens-node version * Update examples/with-supertokens/package.json Co-authored-by: Lee Robinson <me@leerob.io> * updates based on check-examples.sh script * linter fix * updates supertokens-auth-react dependency version * adds development OAuth key to example * removes section from README * removes unnecessary file * updates dependency versions * with-supertokens: reduced bundle size by removing node lib from bundle Linting fix * Removed accidentally added config file * adds sign in with apple * extracted oauth keys to .env file * fixes node init issue race condition * removes unnecessary file * updates supertokens-auth-react dependency * updates superttokens-node dependency * adds a cap to react dependency * updates eslint-config-next version * removes unnecessary dev dependency * updates to latest version of supertokens-auth-react SDK * Updated nextjs in supertokens example * Update examples/with-supertokens/package.json * Update examples/with-supertokens/package.json * Update package.json * Update examples/with-supertokens/package.json Co-authored-by: Balázs Orbán <info@balazsorban.com> * Update examples/with-supertokens/package.json Co-authored-by: Balázs Orbán <info@balazsorban.com> * updates to supertokens-auth-react version * feat: update&improve ssr in with-supertokens * refactor: implement review feedback * refactor: moved everything into ProtectedPage to make Auth component usage clearer * refactor: implement review feedback * updates dependency version and uses nextjs router for navigation * removes prettier dendency in with-supertokens example app
1 parent 69f8024 commit b83107c

File tree

4 files changed

+41
-17
lines changed

4 files changed

+41
-17
lines changed
 

‎examples/with-supertokens/config/frontendConfig.js

+13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import ThirdPartyEmailPasswordReact from 'supertokens-auth-react/recipe/thirdpartyemailpassword'
22
import SessionReact from 'supertokens-auth-react/recipe/session'
33
import { appInfo } from './appInfo'
4+
import Router from 'next/router'
45

56
export let frontendConfig = () => {
67
return {
@@ -20,5 +21,17 @@ export let frontendConfig = () => {
2021
}),
2122
SessionReact.init(),
2223
],
24+
// this is so that the SDK uses the next router for navigation
25+
windowHandler: (oI) => {
26+
return {
27+
...oI,
28+
location: {
29+
...oI.location,
30+
setHref: (href) => {
31+
Router.push(href)
32+
},
33+
},
34+
}
35+
},
2336
}
2437
}

‎examples/with-supertokens/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"next": "latest",
1010
"react": "^18.0.0",
1111
"react-dom": "^18.0.0",
12-
"supertokens-auth-react": "^0.21.2",
13-
"supertokens-node": "^9.1.1"
12+
"supertokens-auth-react": "^0.24.0",
13+
"supertokens-node": "^11.0.0"
1414
}
1515
}

‎examples/with-supertokens/pages/_app.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import '../styles/globals.css'
22
import React from 'react'
33
import { useEffect } from 'react'
4-
import SuperTokensReact from 'supertokens-auth-react'
4+
import SuperTokensReact, { SuperTokensWrapper } from 'supertokens-auth-react'
55
import * as SuperTokensConfig from '../config/frontendConfig'
66
import Session from 'supertokens-auth-react/recipe/session'
77
import { redirectToAuth } from 'supertokens-auth-react/recipe/thirdpartyemailpassword'
@@ -27,7 +27,12 @@ function MyApp({ Component, pageProps }) {
2727
if (pageProps.fromSupertokens === 'needs-refresh') {
2828
return null
2929
}
30-
return <Component {...pageProps} />
30+
31+
return (
32+
<SuperTokensWrapper>
33+
<Component {...pageProps} />
34+
</SuperTokensWrapper>
35+
)
3136
}
3237

3338
export default MyApp

‎examples/with-supertokens/pages/index.js

+19-13
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
import React from 'react'
22
import Head from 'next/head'
33
import styles from '../styles/Home.module.css'
4-
import ThirdPartyEmailPassword from 'supertokens-auth-react/recipe/thirdpartyemailpassword'
5-
import dynamic from 'next/dynamic'
4+
import ThirdPartyEmailPassword, {
5+
ThirdPartyEmailPasswordAuth,
6+
} from 'supertokens-auth-react/recipe/thirdpartyemailpassword'
67
import supertokensNode from 'supertokens-node'
78
import { backendConfig } from '../config/backendConfig'
89
import Session from 'supertokens-node/recipe/session'
9-
10-
const ThirdPartyEmailPasswordAuthNoSSR = dynamic(
11-
new Promise((res) =>
12-
res(ThirdPartyEmailPassword.ThirdPartyEmailPasswordAuth)
13-
),
14-
{ ssr: false }
15-
)
10+
import { useSessionContext } from 'supertokens-auth-react/recipe/session'
1611

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

3833
export default function Home(props) {
3934
return (
40-
<ThirdPartyEmailPasswordAuthNoSSR>
35+
<ThirdPartyEmailPasswordAuth>
4136
<ProtectedPage userId={props.userId} />
42-
</ThirdPartyEmailPasswordAuthNoSSR>
37+
</ThirdPartyEmailPasswordAuth>
4338
)
4439
}
4540

4641
function ProtectedPage({ userId }) {
42+
const session = useSessionContext()
43+
4744
async function logoutClicked() {
4845
await ThirdPartyEmailPassword.signOut()
4946
ThirdPartyEmailPassword.redirectToAuth()
@@ -57,21 +54,30 @@ function ProtectedPage({ userId }) {
5754
}
5855
}
5956

57+
if (session.loading === true) {
58+
return null
59+
}
60+
6061
return (
6162
<div className={styles.container}>
6263
<Head>
6364
<title>SuperTokens 💫</title>
6465
<link rel="icon" href="/favicon.ico" />
6566
</Head>
66-
6767
<main className={styles.main}>
6868
<h1 className={styles.title}>
6969
Welcome to <a href="https://nextjs.org">Next.js!</a>
7070
</h1>
7171
<p className={styles.description}>
72-
You are authenticated with SuperTokens! (UserID: {userId})
72+
You are authenticated with SuperTokens!
7373
</p>
7474

75+
<p className={styles.description}>
76+
UserId: {session.userId} <br /> (from SSR: {userId})
77+
</p>
78+
<p className={styles.description}>
79+
Access token payload: {JSON.stringify(session.accessTokenPayload)}
80+
</p>
7581
<div
7682
style={{
7783
display: 'flex',

0 commit comments

Comments
 (0)
Please sign in to comment.