Skip to content

Commit

Permalink
- fix supertokens redirection issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Lamarcke committed Nov 3, 2024
1 parent 6a602e3 commit e0e23c4
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 60 deletions.
22 changes: 14 additions & 8 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { ModalsProvider } from "@mantine/modals";
import { Notifications } from "@mantine/notifications";
import { theme } from "@/styles/theme";
import { AppProvider } from "./provider";
import SuperTokensProvider from "@/components/auth/SuperTokensProvider";

/**
* Basic configuration for wrapper services
Expand Down Expand Up @@ -55,14 +56,19 @@ export default function RootLayout({
/>
</head>
<body>
<DirectionProvider>
<MantineProvider theme={theme} defaultColorScheme={"dark"}>
<ModalsProvider>
<AppProvider>{children}</AppProvider>
</ModalsProvider>
<Notifications />
</MantineProvider>
</DirectionProvider>
<SuperTokensProvider>
<DirectionProvider>
<MantineProvider
theme={theme}
defaultColorScheme={"dark"}
>
<ModalsProvider>
<AppProvider>{children}</AppProvider>
</ModalsProvider>
<Notifications />
</MantineProvider>
</DirectionProvider>
</SuperTokensProvider>
</body>
</html>
);
Expand Down
5 changes: 2 additions & 3 deletions src/components/auth/SessionAuthWithRoles.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
"use client";

import React, { PropsWithChildren, useEffect, useState } from "react";
import { useUserRoles } from "@/components/auth/hooks/useUserRoles";
import { SessionAuth } from "supertokens-auth-react/recipe/session";
import { EUserRoles } from "@/components/auth/roles";
import { UserRoleClaim } from "supertokens-auth-react/recipe/userroles";
import CenteredLoading from "@/components/general/CenteredLoading";
import { Stack } from "@mantine/core";
import { AccessDeniedScreen } from "supertokens-auth-react/recipe/session/prebuiltui";
import { redirectToAuth } from "supertokens-auth-react";

interface Props extends PropsWithChildren {
roles: EUserRoles[];
Expand Down
98 changes: 49 additions & 49 deletions src/components/auth/SuperTokensProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use client";

import SuperTokensReact, { SuperTokensWrapper } from "supertokens-auth-react";
import React from "react";
import Session from "supertokens-auth-react/recipe/session";
Expand All @@ -9,68 +11,66 @@ import { usePathname, useRouter } from "next/navigation";
const IS_DEV = process.env.NODE_ENV !== "production";

const routerInfo: { router?: ReturnType<typeof useRouter>; pathName?: string } =
{};
{};

export function setRouter(
router: ReturnType<typeof useRouter>,
pathName: string
router: ReturnType<typeof useRouter>,
pathName: string,
) {
routerInfo.router = router;
routerInfo.pathName = pathName;
routerInfo.router = router;
routerInfo.pathName = pathName;
}

export const frontendConfig = (): SuperTokensConfig => {
return {
appInfo: {
appName: "GameNode Admin",
apiDomain: process.env.NEXT_PUBLIC_DOMAIN_SERVER as string,
websiteDomain: process.env.NEXT_PUBLIC_DOMAIN_WEBSITE as string,
apiBasePath: "/v1/auth",
websiteBasePath: "/auth",
},
recipeList: [
Passwordless.init({
contactMethod: "EMAIL",
}),
ThirdParty.init({
signInAndUpFeature: {
providers: [
// TODO: Enable once it's approved
// ThirdPartyPasswordlessReact.Google.init(),
ThirdParty.Discord.init(),
],
},
}),
Session.init({
sessionTokenFrontendDomain: IS_DEV
? undefined
: (process.env.NEXT_PUBLIC_SESSION_DOMAIN as string),
}),
],
getRedirectionURL: async () => {
return "/dashboard";
},
windowHandler: (original) => ({
...original,
location: {
...original.location,
getPathName: () => routerInfo.pathName!,
assign: (url) => routerInfo.router!.push(url.toString()),
setHref: (url) => routerInfo.router!.push(url.toString()),
},
}),
};
return {
appInfo: {
appName: "GameNode Admin",
apiDomain: process.env.NEXT_PUBLIC_DOMAIN_SERVER as string,
websiteDomain: process.env.NEXT_PUBLIC_DOMAIN_WEBSITE as string,
apiBasePath: "/v1/auth",
websiteBasePath: "/auth",
},
recipeList: [
Passwordless.init({
contactMethod: "EMAIL",
}),
ThirdParty.init({
signInAndUpFeature: {
providers: [
// TODO: Enable once it's approved
// ThirdPartyPasswordlessReact.Google.init(),
ThirdParty.Discord.init(),
],
},
}),
Session.init({
sessionTokenFrontendDomain: IS_DEV
? undefined
: (process.env.NEXT_PUBLIC_SESSION_DOMAIN as string),
}),
],
windowHandler: (original) => ({
...original,
location: {
...original.location,
getPathName: () => routerInfo.pathName!,
assign: (url) => routerInfo.router!.push(url.toString()),
setHref: (url) => routerInfo.router!.push(url.toString()),
},
}),
};
};

if (typeof window !== "undefined") {
// we only want to call this init function on the frontend, so we check typeof window !== 'undefined'
SuperTokensReact.init(frontendConfig());
// we only want to call this init function on the frontend, so we check typeof window !== 'undefined'
console.log("Supertokens init with routerInfo: ", routerInfo);
SuperTokensReact.init(frontendConfig());
}

const SuperTokensProvider = ({ children }: { children: React.ReactNode }) => {
setRouter(useRouter(), usePathname() || window.location.pathname);
setRouter(useRouter(), usePathname() || window.location.pathname);

return <SuperTokensWrapper>{children}</SuperTokensWrapper>;
return <SuperTokensWrapper>{children}</SuperTokensWrapper>;
};

export default SuperTokensProvider;

0 comments on commit e0e23c4

Please sign in to comment.