Skip to content

Commit 901df3f

Browse files
authored
fix: supertokens api url in appinfo.ts (#486)
1 parent 4081aa0 commit 901df3f

File tree

3 files changed

+52
-15
lines changed

3 files changed

+52
-15
lines changed

view/app/config/appInfo.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
export const appInfo = {
2-
// learn more about this on https://supertokens.com/docs/thirdpartyemailpassword/appinfo
3-
appName: 'Nixopus',
4-
apiDomain: process.env.NEXT_PUBLIC_API_URL?.replace('/api', '') || 'http://localhost:8080',
5-
websiteDomain: process.env.NEXT_PUBLIC_WEBSITE_DOMAIN || 'http://localhost:3000',
6-
apiBasePath: '/auth',
7-
websiteBasePath: '/auth'
1+
import { getBaseUrl } from '@/redux/conf';
2+
3+
export const getAppInfo = async () => {
4+
const baseUrl = await getBaseUrl();
5+
const apiDomain = baseUrl.replace('/api', '');
6+
7+
return {
8+
// learn more about this on https://supertokens.com/docs/thirdpartyemailpassword/appinfo
9+
appName: 'Nixopus',
10+
apiDomain,
11+
websiteDomain: process.env.NEXT_PUBLIC_WEBSITE_DOMAIN || 'http://localhost:3000',
12+
apiBasePath: '/auth',
13+
websiteBasePath: '/auth'
14+
};
815
};

view/app/config/frontend.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import EmailPasswordReact from 'supertokens-auth-react/recipe/emailpassword'
22
import PasswordlessReact from 'supertokens-auth-react/recipe/passwordless'
33
import SessionReact from 'supertokens-auth-react/recipe/session'
4-
import { appInfo } from './appInfo'
4+
import { getAppInfo } from './appInfo'
55
import { useRouter } from "next/navigation";
66
import { SuperTokensConfig } from 'supertokens-auth-react/lib/build/types'
77

@@ -16,7 +16,9 @@ export function setRouter(
1616
routerInfo.pathName = pathName;
1717
}
1818

19-
export const frontendConfig = (): SuperTokensConfig => {
19+
export const frontendConfig = async (): Promise<SuperTokensConfig> => {
20+
const appInfo = await getAppInfo();
21+
2022
return {
2123
appInfo,
2224
recipeList: [

view/components/supertokensProvider.tsx

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,51 @@
11
'use client';
2-
import React from 'react';
2+
import React, { useEffect, useState } from 'react';
33
import { SuperTokensWrapper } from 'supertokens-auth-react';
44
import SuperTokensReact from 'supertokens-auth-react';
55
import { AuthRecipeComponentsOverrideContextProvider } from 'supertokens-auth-react/ui';
66
import { frontendConfig, setRouter } from '@/app/config/frontend';
77
import { usePathname, useRouter } from 'next/navigation';
88
import { useIsAdminRegisteredQuery } from '@/redux/services/users/authApi';
99

10-
if (typeof window !== 'undefined') {
11-
// we only want to call this init function on the frontend, so we check typeof window !== 'undefined'
12-
SuperTokensReact.init(frontendConfig());
13-
}
10+
let isInitialized = false;
1411

1512
export const SuperTokensProvider: React.FC<React.PropsWithChildren<{}>> = ({
1613
children,
1714
}) => {
18-
setRouter(useRouter(), usePathname() || window.location.pathname);
15+
const [isReady, setIsReady] = useState(isInitialized);
16+
const router = useRouter();
17+
const pathname = usePathname();
18+
19+
useEffect(() => {
20+
const initializeSuperTokens = async () => {
21+
if (!isInitialized) {
22+
try {
23+
const config = await frontendConfig();
24+
SuperTokensReact.init(config);
25+
isInitialized = true;
26+
setIsReady(true);
27+
} catch (error) {
28+
console.error('Failed to initialize SuperTokens:', error);
29+
setIsReady(true);
30+
}
31+
} else {
32+
setIsReady(true);
33+
}
34+
};
35+
36+
initializeSuperTokens();
37+
}, []);
38+
39+
useEffect(() => {
40+
setRouter(router, pathname || window.location.pathname);
41+
}, [router, pathname]);
42+
1943
const { data: isAdminRegistered } = useIsAdminRegisteredQuery();
2044

45+
if (!isReady) {
46+
return <div>Loading...</div>;
47+
}
48+
2149
return (
2250
<SuperTokensWrapper>
2351
<AuthRecipeComponentsOverrideContextProvider

0 commit comments

Comments
 (0)