Unverified Commit b83107c3 authored by Rishabh Poddar's avatar Rishabh Poddar Committed by GitHub

Updates with-supertokens example (#38369)

* add-supertokens-to-authentication.md

* bump supertokens deps

* update SuperTokens Auth doc

* Update docs/authentication.md
Co-authored-by: 's avatarLee Robinson <me@leerob.io>

* Update docs/authentication.md
Co-authored-by: 's avatarLee 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: 's avatarLee 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: 's avatarLee 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: 's avatarBalázs Orbán <info@balazsorban.com>

* Update examples/with-supertokens/package.json
Co-authored-by: 's avatarBalá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
parent 69f8024a
import ThirdPartyEmailPasswordReact from 'supertokens-auth-react/recipe/thirdpartyemailpassword' import ThirdPartyEmailPasswordReact from 'supertokens-auth-react/recipe/thirdpartyemailpassword'
import SessionReact from 'supertokens-auth-react/recipe/session' import SessionReact from 'supertokens-auth-react/recipe/session'
import { appInfo } from './appInfo' import { appInfo } from './appInfo'
import Router from 'next/router'
export let frontendConfig = () => { export let frontendConfig = () => {
return { return {
...@@ -20,5 +21,17 @@ export let frontendConfig = () => { ...@@ -20,5 +21,17 @@ export let frontendConfig = () => {
}), }),
SessionReact.init(), SessionReact.init(),
], ],
// this is so that the SDK uses the next router for navigation
windowHandler: (oI) => {
return {
...oI,
location: {
...oI.location,
setHref: (href) => {
Router.push(href)
},
},
}
},
} }
} }
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
"next": "latest", "next": "latest",
"react": "^18.0.0", "react": "^18.0.0",
"react-dom": "^18.0.0", "react-dom": "^18.0.0",
"supertokens-auth-react": "^0.21.2", "supertokens-auth-react": "^0.24.0",
"supertokens-node": "^9.1.1" "supertokens-node": "^11.0.0"
} }
} }
import '../styles/globals.css' import '../styles/globals.css'
import React from 'react' import React from 'react'
import { useEffect } 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 * as SuperTokensConfig from '../config/frontendConfig'
import Session from 'supertokens-auth-react/recipe/session' import Session from 'supertokens-auth-react/recipe/session'
import { redirectToAuth } from 'supertokens-auth-react/recipe/thirdpartyemailpassword' import { redirectToAuth } from 'supertokens-auth-react/recipe/thirdpartyemailpassword'
...@@ -27,7 +27,12 @@ function MyApp({ Component, pageProps }) { ...@@ -27,7 +27,12 @@ function MyApp({ Component, pageProps }) {
if (pageProps.fromSupertokens === 'needs-refresh') { if (pageProps.fromSupertokens === 'needs-refresh') {
return null return null
} }
return <Component {...pageProps} />
return (
<SuperTokensWrapper>
<Component {...pageProps} />
</SuperTokensWrapper>
)
} }
export default MyApp export default MyApp
import React from 'react' import React from 'react'
import Head from 'next/head' import Head from 'next/head'
import styles from '../styles/Home.module.css' import styles from '../styles/Home.module.css'
import ThirdPartyEmailPassword from 'supertokens-auth-react/recipe/thirdpartyemailpassword' import ThirdPartyEmailPassword, {
import dynamic from 'next/dynamic' ThirdPartyEmailPasswordAuth,
} from 'supertokens-auth-react/recipe/thirdpartyemailpassword'
import supertokensNode from 'supertokens-node' import supertokensNode from 'supertokens-node'
import { backendConfig } from '../config/backendConfig' import { backendConfig } from '../config/backendConfig'
import Session from 'supertokens-node/recipe/session' import Session from 'supertokens-node/recipe/session'
import { useSessionContext } from 'supertokens-auth-react/recipe/session'
const ThirdPartyEmailPasswordAuthNoSSR = dynamic(
new Promise((res) =>
res(ThirdPartyEmailPassword.ThirdPartyEmailPasswordAuth)
),
{ ssr: false }
)
export async function getServerSideProps(context) { export async function getServerSideProps(context) {
// this runs on the backend, so we must call init on supertokens-node SDK // this runs on the backend, so we must call init on supertokens-node SDK
...@@ -37,13 +32,15 @@ export async function getServerSideProps(context) { ...@@ -37,13 +32,15 @@ export async function getServerSideProps(context) {
export default function Home(props) { export default function Home(props) {
return ( return (
<ThirdPartyEmailPasswordAuthNoSSR> <ThirdPartyEmailPasswordAuth>
<ProtectedPage userId={props.userId} /> <ProtectedPage userId={props.userId} />
</ThirdPartyEmailPasswordAuthNoSSR> </ThirdPartyEmailPasswordAuth>
) )
} }
function ProtectedPage({ userId }) { function ProtectedPage({ userId }) {
const session = useSessionContext()
async function logoutClicked() { async function logoutClicked() {
await ThirdPartyEmailPassword.signOut() await ThirdPartyEmailPassword.signOut()
ThirdPartyEmailPassword.redirectToAuth() ThirdPartyEmailPassword.redirectToAuth()
...@@ -57,21 +54,30 @@ function ProtectedPage({ userId }) { ...@@ -57,21 +54,30 @@ function ProtectedPage({ userId }) {
} }
} }
if (session.loading === true) {
return null
}
return ( return (
<div className={styles.container}> <div className={styles.container}>
<Head> <Head>
<title>SuperTokens 💫</title> <title>SuperTokens 💫</title>
<link rel="icon" href="/favicon.ico" /> <link rel="icon" href="/favicon.ico" />
</Head> </Head>
<main className={styles.main}> <main className={styles.main}>
<h1 className={styles.title}> <h1 className={styles.title}>
Welcome to <a href="https://nextjs.org">Next.js!</a> Welcome to <a href="https://nextjs.org">Next.js!</a>
</h1> </h1>
<p className={styles.description}> <p className={styles.description}>
You are authenticated with SuperTokens! (UserID: {userId}) You are authenticated with SuperTokens!
</p> </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 <div
style={{ style={{
display: 'flex', display: 'flex',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment