Unverified Commit 51b728fb authored by Rishabh Poddar's avatar Rishabh Poddar Committed by GitHub

Updates with-supertokens example app (#40707)

- Updates supertokens-node and supertokens-auth-react dependencies.
- Uses separate Email verification recipe instead of using it inside the
ThirdPartyEmailPassword recipe (as per the update)
- Uses <SessionAuth> guard instead of <ThirdPartyEmailPassword> guard
(as per the lib update)
parent 244b6297
import ThirdPartyEmailPasswordNode from 'supertokens-node/recipe/thirdpartyemailpassword' import ThirdPartyEmailPasswordNode from 'supertokens-node/recipe/thirdpartyemailpassword'
import EmailVerificationNode from 'supertokens-node/recipe/emailverification'
import SessionNode from 'supertokens-node/recipe/session' import SessionNode from 'supertokens-node/recipe/session'
import { appInfo } from './appInfo' import { appInfo } from './appInfo'
import { AuthConfig } from '../interfaces' import { AuthConfig } from '../interfaces'
...@@ -11,6 +12,9 @@ export let backendConfig = (): AuthConfig => { ...@@ -11,6 +12,9 @@ export let backendConfig = (): AuthConfig => {
}, },
appInfo, appInfo,
recipeList: [ recipeList: [
EmailVerificationNode.init({
mode: 'REQUIRED',
}),
ThirdPartyEmailPasswordNode.init({ ThirdPartyEmailPasswordNode.init({
providers: [ providers: [
// We have provided you with development keys which you can use for testing. // We have provided you with development keys which you can use for testing.
......
import ThirdPartyEmailPasswordReact from 'supertokens-auth-react/recipe/thirdpartyemailpassword' import ThirdPartyEmailPasswordReact from 'supertokens-auth-react/recipe/thirdpartyemailpassword'
import EmailVerificationReact from 'supertokens-auth-react/recipe/emailverification'
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' import Router from 'next/router'
...@@ -7,10 +8,8 @@ export let frontendConfig = () => { ...@@ -7,10 +8,8 @@ export let frontendConfig = () => {
return { return {
appInfo, appInfo,
recipeList: [ recipeList: [
EmailVerificationReact.init(),
ThirdPartyEmailPasswordReact.init({ ThirdPartyEmailPasswordReact.init({
emailVerificationFeature: {
mode: 'REQUIRED',
},
signInAndUpFeature: { signInAndUpFeature: {
providers: [ providers: [
ThirdPartyEmailPasswordReact.Google.init(), ThirdPartyEmailPasswordReact.Google.init(),
......
...@@ -9,8 +9,8 @@ ...@@ -9,8 +9,8 @@
"next": "latest", "next": "latest",
"react": "^18.2.0", "react": "^18.2.0",
"react-dom": "^18.2.0", "react-dom": "^18.2.0",
"supertokens-auth-react": "^0.24.7", "supertokens-auth-react": "^0.26.0",
"supertokens-node": "^11.2.0" "supertokens-node": "^12.0.0"
}, },
"devDependencies": { "devDependencies": {
"@types/react": "18.0.17", "@types/react": "18.0.17",
......
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, { SuperTokensWrapper } from 'supertokens-auth-react' import SuperTokensReact, {
SuperTokensWrapper,
redirectToAuth,
} 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'
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
SuperTokensReact.init(SuperTokensConfig.frontendConfig()) SuperTokensReact.init(SuperTokensConfig.frontendConfig())
......
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, { import ThirdPartyEmailPassword from 'supertokens-auth-react/recipe/thirdpartyemailpassword'
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' import {
SessionAuth,
useSessionContext,
} from 'supertokens-auth-react/recipe/session'
import { redirectToAuth } from 'supertokens-auth-react'
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
...@@ -18,7 +20,10 @@ export async function getServerSideProps(context) { ...@@ -18,7 +20,10 @@ export async function getServerSideProps(context) {
} catch (err) { } catch (err) {
if (err.type === Session.Error.TRY_REFRESH_TOKEN) { if (err.type === Session.Error.TRY_REFRESH_TOKEN) {
return { props: { fromSupertokens: 'needs-refresh' } } return { props: { fromSupertokens: 'needs-refresh' } }
} else if (err.type === Session.Error.UNAUTHORISED) { } else if (
err.type === Session.Error.UNAUTHORISED ||
err.type === Session.Error.INVALID_CLAIMS
) {
return { props: {} } return { props: {} }
} else { } else {
throw err throw err
...@@ -35,7 +40,7 @@ function ProtectedPage({ userId }) { ...@@ -35,7 +40,7 @@ function ProtectedPage({ userId }) {
async function logoutClicked() { async function logoutClicked() {
await ThirdPartyEmailPassword.signOut() await ThirdPartyEmailPassword.signOut()
ThirdPartyEmailPassword.redirectToAuth() redirectToAuth()
} }
async function fetchUserData() { async function fetchUserData() {
...@@ -173,8 +178,8 @@ function ProtectedPage({ userId }) { ...@@ -173,8 +178,8 @@ function ProtectedPage({ userId }) {
export default function Home(props) { export default function Home(props) {
return ( return (
<ThirdPartyEmailPasswordAuth> <SessionAuth>
<ProtectedPage userId={props.userId} /> <ProtectedPage userId={props.userId} />
</ThirdPartyEmailPasswordAuth> </SessionAuth>
) )
} }
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