Skip to content

Commit b306f85

Browse files
committed
Load config fron env variables
1 parent da4e86d commit b306f85

File tree

13 files changed

+477
-65
lines changed

13 files changed

+477
-65
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
NEXT_PUBLIC_SUPERTOKENS_APP_NAME=test
2+
NEXT_PUBLIC_SUPERTOKENS_API_DOMAIN=http://localhost:3000
3+
NEXT_PUBLIC_SUPERTOKENS_WEBSITE_DOMAIN=http://localhost:3000
4+
NEXT_PUBLIC_SUPERTOKENS_API_BASE_PATH=/api/auth
5+
NEXT_PUBLIC_SUPERTOKENS_WEBSITE_BASE_PATH=/auth

examples/with-next-ssr-app-directory/app/components/home.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import { CallAPIButton } from "./callApiButton";
77
import { LinksComponent } from "./linksComponent";
88
import { SessionAuthForNextJS } from "./sessionAuthForNextJS";
99

10-
import { getSSRSession } from "../../../../lib/build/nextjs";
10+
import { getSSRSession } from "supertokens-auth-react/nextjs/ssr";
11+
// import { getSSRSession } from "../../../../lib/build/nextjs";
1112

1213
export async function HomePage() {
1314
const cookiesStore = await cookies();
@@ -30,7 +31,8 @@ export async function HomePage() {
3031
</div>
3132
<div className={styles.innerContent}>
3233
<div>Your userID is:</div>
33-
<div className={`${styles.truncate} ${styles.userId}`}>{session.userId}</div>
34+
{/* <div className={`${styles.truncate} ${styles.userId}`}>{session.userId}</div> */}
35+
<div className={`${styles.truncate} ${styles.userId}`}>sadsa</div>
3436
<CallAPIButton />
3537
</div>
3638
</div>

examples/with-next-ssr-app-directory/app/components/supertokensProvider.tsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,20 @@ import SuperTokensReact from "supertokens-auth-react";
55
import { frontendConfig, setRouter } from "../config/frontend";
66
import { usePathname, useRouter } from "next/navigation";
77

8-
if (typeof window !== "undefined") {
9-
// we only want to call this init function on the frontend, so we check typeof window !== 'undefined'
10-
SuperTokensReact.init(frontendConfig());
11-
}
8+
// if (typeof window !== "undefined") {
9+
// // we only want to call this init function on the frontend, so we check typeof window !== 'undefined'
10+
// SuperTokensReact.init(frontendConfig());
11+
// }
12+
//
13+
// import { init, setRouterData } from "../../../../lib/build/custom";
14+
import { init, setRouterData } from "supertokens-auth-react/nextjs";
15+
16+
init(frontendConfig());
1217

1318
export const SuperTokensProvider: React.FC<React.PropsWithChildren<{}>> = ({ children }) => {
14-
setRouter(useRouter(), usePathname() || window.location.pathname);
19+
// setRouter(useRouter(), usePathname() || window.location.pathname);
20+
21+
setRouterData(useRouter(), usePathname() || window.location.pathname);
1522

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

examples/with-next-ssr-app-directory/app/config/frontend.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export function setRouter(router: ReturnType<typeof useRouter>, pathName: string
1919
export const frontendConfig = (): SuperTokensConfig => {
2020
return {
2121
appInfo,
22-
enableDebugLogs: true,
22+
// enableDebugLogs: true,
2323
recipeList: [
2424
EmailPasswordReact.init(),
2525
ThirdPartyReact.init({

lib/ts/custom.ts

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/* Copyright (c) 2021, VRAI Labs and/or its affiliates. All rights reserved.
2+
*
3+
* This software is licensed under the Apache License, Version 2.0 (the
4+
* "License") as published by the Apache Software Foundation.
5+
*
6+
* You may not use this file except in compliance with the License. You may
7+
* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
* License for the specific language governing permissions and limitations
13+
* under the License.
14+
*/
15+
16+
/*
17+
* Imports.
18+
*/
19+
import { SuperTokensWrapper } from "./components/supertokensWrapper";
20+
import SuperTokens from "./superTokens";
21+
import { useTranslation } from "./translation/translationContext";
22+
import { useUserContext } from "./usercontext";
23+
import { getNormalisedUserContext } from "./utils";
24+
25+
import type { TranslationStore } from "./translation/translationHelpers";
26+
import type { Navigate, SuperTokensConfig, UserContext } from "./types";
27+
28+
/*
29+
* API Wrapper exposed to user.
30+
*/
31+
32+
export default class SuperTokensNextjsAPIWrapper {
33+
static SuperTokensWrapper = SuperTokensWrapper;
34+
static router: { push: (url: string) => void };
35+
static pathName: string;
36+
static config: SuperTokensConfig;
37+
38+
static init(config: SuperTokensConfig) {
39+
SuperTokensNextjsAPIWrapper.config = config;
40+
// eslint-disable-next-line
41+
if (typeof window !== "undefined") {
42+
console.log("initializing");
43+
SuperTokens.init({
44+
...config,
45+
windowHandler: (original) => ({
46+
...original,
47+
location: {
48+
...original.location,
49+
getPathName: () => SuperTokensNextjsAPIWrapper.pathName,
50+
assign: (url) => SuperTokensNextjsAPIWrapper.router.push(url.toString()),
51+
setHref: (url) => SuperTokensNextjsAPIWrapper.router.push(url.toString()),
52+
},
53+
}),
54+
});
55+
}
56+
}
57+
58+
static setRouterData(router: { push: (url: string) => void }, pathName: string) {
59+
SuperTokensNextjsAPIWrapper.router = router;
60+
SuperTokensNextjsAPIWrapper.pathName = pathName;
61+
}
62+
63+
static changeLanguage(language: string): Promise<void> {
64+
return SuperTokens.getInstanceOrThrow().changeLanguage(language);
65+
}
66+
67+
static loadTranslation(store: TranslationStore): void {
68+
return SuperTokens.getInstanceOrThrow().loadTranslation(store);
69+
}
70+
71+
static redirectToAuth = async (options?: {
72+
show?: "signin" | "signup";
73+
navigate?: Navigate;
74+
queryParams?: any;
75+
redirectBack?: boolean;
76+
userContext?: UserContext;
77+
}) => {
78+
return SuperTokens.getInstanceOrThrow().redirectToAuth({
79+
...options,
80+
redirectBack: options?.redirectBack ?? true,
81+
userContext: getNormalisedUserContext(options?.userContext),
82+
});
83+
};
84+
85+
static useTranslation = useTranslation;
86+
87+
static useUserContext = useUserContext;
88+
}
89+
90+
export const init = SuperTokensNextjsAPIWrapper.init;
91+
export const changeLanguage = SuperTokensNextjsAPIWrapper.changeLanguage;
92+
export const loadTranslation = SuperTokensNextjsAPIWrapper.loadTranslation;
93+
export const redirectToAuth = SuperTokensNextjsAPIWrapper.redirectToAuth;
94+
export const setRouterData = SuperTokensNextjsAPIWrapper.setRouterData;
95+
96+
export { SuperTokensWrapper } from "./components/supertokensWrapper";
97+
export { useTranslation } from "./translation/translationContext";
98+
export { useUserContext } from "./usercontext";

lib/ts/framework/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

lib/ts/framework/nextjs.tsx

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

lib/ts/nextjs/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

lib/ts/nextjs/index.ts

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/* Copyright (c) 2021, VRAI Labs and/or its affiliates. All rights reserved.
2+
*
3+
* This software is licensed under the Apache License, Version 2.0 (the
4+
* "License") as published by the Apache Software Foundation.
5+
*
6+
* You may not use this file except in compliance with the License. You may
7+
* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
* License for the specific language governing permissions and limitations
13+
* under the License.
14+
*/
15+
16+
/*
17+
* Imports.
18+
*/
19+
import { SuperTokensWrapper } from "../components/supertokensWrapper";
20+
import SuperTokens from "../superTokens";
21+
import { useTranslation } from "../translation/translationContext";
22+
import { useUserContext } from "../usercontext";
23+
import { getNormalisedUserContext } from "../utils";
24+
25+
import { getAppInfoFromEnv } from "./utils";
26+
27+
// import { SSRConfig } from "./ssr";
28+
29+
import type { TranslationStore } from "../translation/translationHelpers";
30+
import type { Navigate, SuperTokensConfig, UserContext } from "../types";
31+
32+
type Optional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
33+
34+
export default class SuperTokensNextjsAPIWrapper {
35+
static SuperTokensWrapper = SuperTokensWrapper;
36+
static router: { push: (url: string) => void };
37+
static pathName: string;
38+
39+
static init(config: Optional<SuperTokensConfig, "appInfo">) {
40+
const appInfo = config.appInfo || getAppInfoFromEnv();
41+
// eslint-disable-next-line
42+
if (typeof window !== "undefined") {
43+
SuperTokens.init({
44+
...config,
45+
appInfo,
46+
windowHandler: (original) => ({
47+
...original,
48+
location: {
49+
...original.location,
50+
getPathName: () => SuperTokensNextjsAPIWrapper.pathName,
51+
assign: (url) => SuperTokensNextjsAPIWrapper.router.push(url.toString()),
52+
setHref: (url) => SuperTokensNextjsAPIWrapper.router.push(url.toString()),
53+
},
54+
}),
55+
});
56+
}
57+
}
58+
59+
static setRouterData(router: { push: (url: string) => void }, pathName: string) {
60+
SuperTokensNextjsAPIWrapper.router = router;
61+
SuperTokensNextjsAPIWrapper.pathName = pathName;
62+
}
63+
64+
static changeLanguage(language: string): Promise<void> {
65+
return SuperTokens.getInstanceOrThrow().changeLanguage(language);
66+
}
67+
68+
static loadTranslation(store: TranslationStore): void {
69+
return SuperTokens.getInstanceOrThrow().loadTranslation(store);
70+
}
71+
72+
static redirectToAuth = async (options?: {
73+
show?: "signin" | "signup";
74+
navigate?: Navigate;
75+
queryParams?: any;
76+
redirectBack?: boolean;
77+
userContext?: UserContext;
78+
}) => {
79+
return SuperTokens.getInstanceOrThrow().redirectToAuth({
80+
...options,
81+
redirectBack: options?.redirectBack ?? true,
82+
userContext: getNormalisedUserContext(options?.userContext),
83+
});
84+
};
85+
86+
static useTranslation = useTranslation;
87+
88+
static useUserContext = useUserContext;
89+
}
90+
91+
export const init = SuperTokensNextjsAPIWrapper.init;
92+
export const changeLanguage = SuperTokensNextjsAPIWrapper.changeLanguage;
93+
export const loadTranslation = SuperTokensNextjsAPIWrapper.loadTranslation;
94+
export const redirectToAuth = SuperTokensNextjsAPIWrapper.redirectToAuth;
95+
export const setRouterData = SuperTokensNextjsAPIWrapper.setRouterData;
96+
97+
export { SuperTokensWrapper } from "../components/supertokensWrapper";
98+
export { useTranslation } from "../translation/translationContext";
99+
export { useUserContext } from "../usercontext";

0 commit comments

Comments
 (0)