Skip to content

Commit 0109153

Browse files
committed
Add nextjs init function
1 parent f4d8d50 commit 0109153

File tree

2 files changed

+58
-27
lines changed

2 files changed

+58
-27
lines changed

lib/ts/framework/nextjs.ts

Lines changed: 0 additions & 27 deletions
This file was deleted.

lib/ts/framework/nextjs.tsx

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import SuperTokensReact from "..";
2+
3+
import { SuperTokensWrapper } from "../components/supertokensWrapper";
4+
5+
import type { SuperTokensConfig } from "../types";
6+
7+
type NextjsRouterInfo = {
8+
pathName?: string;
9+
router: { push: (url: string) => void };
10+
};
11+
const RouterInfo: NextjsRouterInfo = {} as NextjsRouterInfo;
12+
13+
export function init(config: SuperTokensConfig) {
14+
// eslint-disable-next-line
15+
if (typeof window !== "undefined") {
16+
SuperTokensReact.init({
17+
...config,
18+
windowHandler: (original) => {
19+
if (RouterInfo.pathName === undefined) {
20+
return {
21+
...original,
22+
location: {
23+
...original.location,
24+
setHref: (url) => RouterInfo.router.push(url.toString()),
25+
},
26+
};
27+
} else {
28+
return {
29+
...original,
30+
location: {
31+
...original.location,
32+
getPathName: () => RouterInfo.pathName as string,
33+
assign: (url) => RouterInfo.router.push(url.toString()),
34+
setHref: (url) => RouterInfo.router.push(url.toString()),
35+
},
36+
};
37+
}
38+
},
39+
});
40+
}
41+
}
42+
43+
type SuperTokensProviderProps = {
44+
router: { push: (url: string) => void };
45+
pathName?: string;
46+
};
47+
48+
export const SuperTokensProvider: React.FC<
49+
PropsWithChildren<SuperTokensProviderProps>
50+
> = (props) => {
51+
52+
RouterInfo.router = props.router;
53+
if (props.pathName) {
54+
RouterInfo.pathName = props.pathName;
55+
}
56+
57+
return <SuperTokensWrapper {...props}>
58+
};

0 commit comments

Comments
 (0)