SomeBody16
03/10/2023, 1:10 AMtypescript
// _app.tsx
if (typeof window !== 'undefined') {
Auth.init()
}
export default function App(props: AppProps) {
const { Component, pageProps } = props
React.useEffect(() => {}, [])
return (
<>
<Head>
<meta name='viewport' content='minimum-scale=1, initial-scale=1, width=device-width' />
</Head>
<BaseLayout>
<Component {...pageProps} />
</BaseLayout>
</>
)
}
typescript
// BaseLayout.tsx
export const BaseLayout = ({ children }: React.PropsWithChildren<unknown>) => (
<MantineProvider withGlobalStyles withNormalizeCSS theme={theme}>
<NavigationProgress />
<Auth.Provider>
<ModalsProvider>
<ClientProvider>{children}</ClientProvider>
</ModalsProvider>
</Auth.Provider>
</MantineProvider>
)
typescript
// auth/index.tsx
import { AuthProvider } from './AuthProvider'
import { init } from './init'
export const Auth = {
init,
Provider: AuthProvider,
}
export * from './provider'
typescript
// AuthProvider.tsx
export const AuthProvider = (props: React.PropsWithChildren<{}>) => (
<SuperTokensWrapper>{props.children}</SuperTokensWrapper>
)
typescript
// init.tsx
import SuperTokensReact from 'supertokens-auth-react'
import { frontendConfig } from './config/frontendConfig'
export const init = () => SuperTokensReact.init(frontendConfig())
typescript
// frontendConfig.ts
import Router from 'next/router'
import SessionReact from 'supertokens-auth-react/recipe/session'
import ThirdPartyReact from 'supertokens-auth-react/recipe/thirdparty'
import { appInfo } from './appInfo'
export const frontendConfig = () => {
console.debug('Supertokens init!')
console.debug(appInfo, ThirdPartyReact.Google.init())
return {
appInfo,
recipeList: [
ThirdPartyReact.init({
signInAndUpFeature: {
providers: [ThirdPartyReact.Google.init()],
},
}),
SessionReact.init(),
],
windowHandler: (oI: any) => {
return {
...oI,
location: {
...oI.location,
setHref: (href: string) => {
Router.push(href)
},
},
}
},
}
}
And this code:
typescript
export async function signInWithGoogle(authorisationURL: string) {
try {
const authUrl = await getAuthorisationURLWithQueryParamsAndSetState({ // <-- THIS LINE
providerId: 'google',
// This is where Google should redirect the user back after login or error.
// This URL goes on the Google's dashboard as well.
// authorisationURL: "http://localhost:3000/auth/callback/google",
authorisationURL,
})
// we redirect the user to google for auth.
window.location.assign(authUrl)
} catch (err: any) {
if (err.isSuperTokensGeneralError === true) {
// this may be a custom error message sent from the API by you.
throw new Error(err.message)
} else {
throw err
}
}
}
Produces this error:
Error: No instance of ThirdParty found. Make sure to call the ThirdParty.init method.
How and why frontendConfig is called, config is valid (printed to console), appInfo is valid and backend is online (Nest.js + supertokens docker)rp_st
03/10/2023, 5:24 AMrp_st
03/10/2023, 5:25 AMgetAuthorisationURLWithQueryParamsAndSetState
function - is that happening for sure?SomeBody16
03/10/2023, 8:59 AMtypescript
// signInWithGoogle.tsx
export async function signInWithGoogle(authorisationURL: string) {
try {
console.log('signInWithGoogle')
const authUrl = await getAuthorisationURLWithQueryParamsAndSetState({
providerId: 'google',
authorisationURL,
})
window.location.assign(authUrl)
} catch (err: any) {
if (err.isSuperTokensGeneralError === true) {
throw new Error(err.message)
} else {
throw err
}
}
}
// frontendConfig.tsx
export const frontendConfig = () => {
console.debug('Supertokens init!')
return {
appInfo,
recipeList: [
ThirdPartyReact.init({
signInAndUpFeature: {
providers: [ThirdPartyReact.Google.init()],
},
}),
SessionReact.init(),
],
windowHandler: (oI: any) => {
return {
...oI,
location: {
...oI.location,
setHref: (href: string) => {
Router.push(href)
},
},
}
},
}
}
// init.tsx
export const init = () => {
console.log('Init!')
SuperTokensReact.init(frontendConfig())
}
SomeBody16
03/10/2023, 8:59 AMrp_st
03/10/2023, 9:03 AMSomeBody16
03/10/2023, 9:04 AMtypescript
import { getAuthorisationURLWithQueryParamsAndSetState } from 'supertokens-web-js/recipe/thirdparty'
rp_st
03/10/2023, 9:04 AMSomeBody16
03/10/2023, 9:06 AMSomeBody16
03/10/2023, 9:07 AM