Skip to content

Commit

Permalink
Load config fron env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
bcbogdan committed Feb 12, 2025
1 parent da4e86d commit ad89bbe
Show file tree
Hide file tree
Showing 17 changed files with 569 additions and 65 deletions.
5 changes: 5 additions & 0 deletions examples/with-next-ssr-app-directory/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
NEXT_PUBLIC_SUPERTOKENS_APP_NAME=test
NEXT_PUBLIC_SUPERTOKENS_API_DOMAIN=http://localhost:3000
NEXT_PUBLIC_SUPERTOKENS_WEBSITE_DOMAIN=http://localhost:3000
NEXT_PUBLIC_SUPERTOKENS_API_BASE_PATH=/api/auth
NEXT_PUBLIC_SUPERTOKENS_WEBSITE_BASE_PATH=/auth
6 changes: 4 additions & 2 deletions examples/with-next-ssr-app-directory/app/components/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { CallAPIButton } from "./callApiButton";
import { LinksComponent } from "./linksComponent";
import { SessionAuthForNextJS } from "./sessionAuthForNextJS";

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

export async function HomePage() {
const cookiesStore = await cookies();
Expand All @@ -30,7 +31,8 @@ export async function HomePage() {
</div>
<div className={styles.innerContent}>
<div>Your userID is:</div>
<div className={`${styles.truncate} ${styles.userId}`}>{session.userId}</div>
{/* <div className={`${styles.truncate} ${styles.userId}`}>{session.userId}</div> */}
<div className={`${styles.truncate} ${styles.userId}`}>sadsa</div>
<CallAPIButton />
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@ import SuperTokensReact from "supertokens-auth-react";
import { frontendConfig, setRouter } from "../config/frontend";
import { usePathname, useRouter } from "next/navigation";

if (typeof window !== "undefined") {
// we only want to call this init function on the frontend, so we check typeof window !== 'undefined'
SuperTokensReact.init(frontendConfig());
}
// if (typeof window !== "undefined") {
// // we only want to call this init function on the frontend, so we check typeof window !== 'undefined'
// SuperTokensReact.init(frontendConfig());
// }
//
// import { init, setRouterData } from "../../../../lib/build/custom";
import { init, setRouterData } from "supertokens-auth-react/nextjs";

init(frontendConfig());

export const SuperTokensProvider: React.FC<React.PropsWithChildren<{}>> = ({ children }) => {
setRouter(useRouter(), usePathname() || window.location.pathname);
// setRouter(useRouter(), usePathname() || window.location.pathname);

setRouterData(useRouter(), usePathname() || window.location.pathname);

return <SuperTokensWrapper>{children}</SuperTokensWrapper>;
};
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function setRouter(router: ReturnType<typeof useRouter>, pathName: string
export const frontendConfig = (): SuperTokensConfig => {
return {
appInfo,
enableDebugLogs: true,
// enableDebugLogs: true,
recipeList: [
EmailPasswordReact.init(),
ThirdPartyReact.init({
Expand Down
98 changes: 98 additions & 0 deletions lib/ts/custom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/* Copyright (c) 2021, VRAI Labs and/or its affiliates. All rights reserved.
*
* This software is licensed under the Apache License, Version 2.0 (the
* "License") as published by the Apache Software Foundation.
*
* You may not use this file except in compliance with the License. You may
* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

/*
* Imports.
*/
import { SuperTokensWrapper } from "./components/supertokensWrapper";
import SuperTokens from "./superTokens";
import { useTranslation } from "./translation/translationContext";
import { useUserContext } from "./usercontext";
import { getNormalisedUserContext } from "./utils";

import type { TranslationStore } from "./translation/translationHelpers";
import type { Navigate, SuperTokensConfig, UserContext } from "./types";

/*
* API Wrapper exposed to user.
*/

export default class SuperTokensNextjsAPIWrapper {
static SuperTokensWrapper = SuperTokensWrapper;
static router: { push: (url: string) => void };
static pathName: string;
static config: SuperTokensConfig;

static init(config: SuperTokensConfig) {
SuperTokensNextjsAPIWrapper.config = config;
// eslint-disable-next-line
if (typeof window !== "undefined") {
console.log("initializing");
SuperTokens.init({
...config,
windowHandler: (original) => ({
...original,
location: {
...original.location,
getPathName: () => SuperTokensNextjsAPIWrapper.pathName,
assign: (url) => SuperTokensNextjsAPIWrapper.router.push(url.toString()),
setHref: (url) => SuperTokensNextjsAPIWrapper.router.push(url.toString()),
},
}),
});
}
}

static setRouterData(router: { push: (url: string) => void }, pathName: string) {
SuperTokensNextjsAPIWrapper.router = router;
SuperTokensNextjsAPIWrapper.pathName = pathName;
}

static changeLanguage(language: string): Promise<void> {
return SuperTokens.getInstanceOrThrow().changeLanguage(language);
}

static loadTranslation(store: TranslationStore): void {
return SuperTokens.getInstanceOrThrow().loadTranslation(store);
}

static redirectToAuth = async (options?: {
show?: "signin" | "signup";
navigate?: Navigate;
queryParams?: any;
redirectBack?: boolean;
userContext?: UserContext;
}) => {
return SuperTokens.getInstanceOrThrow().redirectToAuth({
...options,
redirectBack: options?.redirectBack ?? true,
userContext: getNormalisedUserContext(options?.userContext),
});
};

static useTranslation = useTranslation;

static useUserContext = useUserContext;
}

export const init = SuperTokensNextjsAPIWrapper.init;
export const changeLanguage = SuperTokensNextjsAPIWrapper.changeLanguage;
export const loadTranslation = SuperTokensNextjsAPIWrapper.loadTranslation;
export const redirectToAuth = SuperTokensNextjsAPIWrapper.redirectToAuth;
export const setRouterData = SuperTokensNextjsAPIWrapper.setRouterData;

export { SuperTokensWrapper } from "./components/supertokensWrapper";
export { useTranslation } from "./translation/translationContext";
export { useUserContext } from "./usercontext";
1 change: 0 additions & 1 deletion lib/ts/framework/index.ts

This file was deleted.

55 changes: 0 additions & 55 deletions lib/ts/framework/nextjs.tsx

This file was deleted.

1 change: 1 addition & 0 deletions lib/ts/nextjs/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

99 changes: 99 additions & 0 deletions lib/ts/nextjs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/* Copyright (c) 2021, VRAI Labs and/or its affiliates. All rights reserved.
*
* This software is licensed under the Apache License, Version 2.0 (the
* "License") as published by the Apache Software Foundation.
*
* You may not use this file except in compliance with the License. You may
* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

/*
* Imports.
*/
import { SuperTokensWrapper } from "../components/supertokensWrapper";
import SuperTokens from "../superTokens";
import { useTranslation } from "../translation/translationContext";
import { useUserContext } from "../usercontext";
import { getNormalisedUserContext } from "../utils";

import { getAppInfoFromEnv } from "./utils";

// import { SSRConfig } from "./ssr";

import type { TranslationStore } from "../translation/translationHelpers";
import type { Navigate, SuperTokensConfig, UserContext } from "../types";

type Optional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;

export default class SuperTokensNextjsAPIWrapper {
static SuperTokensWrapper = SuperTokensWrapper;
static router: { push: (url: string) => void };
static pathName: string;

static init(config: Optional<SuperTokensConfig, "appInfo">) {
const appInfo = config.appInfo || getAppInfoFromEnv();
// eslint-disable-next-line
if (typeof window !== "undefined") {
SuperTokens.init({
...config,
appInfo,
windowHandler: (original) => ({
...original,
location: {
...original.location,
getPathName: () => SuperTokensNextjsAPIWrapper.pathName,
assign: (url) => SuperTokensNextjsAPIWrapper.router.push(url.toString()),
setHref: (url) => SuperTokensNextjsAPIWrapper.router.push(url.toString()),
},
}),
});
}
}

static setRouterData(router: { push: (url: string) => void }, pathName: string) {
SuperTokensNextjsAPIWrapper.router = router;
SuperTokensNextjsAPIWrapper.pathName = pathName;
}

static changeLanguage(language: string): Promise<void> {
return SuperTokens.getInstanceOrThrow().changeLanguage(language);
}

static loadTranslation(store: TranslationStore): void {
return SuperTokens.getInstanceOrThrow().loadTranslation(store);
}

static redirectToAuth = async (options?: {
show?: "signin" | "signup";
navigate?: Navigate;
queryParams?: any;
redirectBack?: boolean;
userContext?: UserContext;
}) => {
return SuperTokens.getInstanceOrThrow().redirectToAuth({
...options,
redirectBack: options?.redirectBack ?? true,
userContext: getNormalisedUserContext(options?.userContext),
});
};

static useTranslation = useTranslation;

static useUserContext = useUserContext;
}

export const init = SuperTokensNextjsAPIWrapper.init;
export const changeLanguage = SuperTokensNextjsAPIWrapper.changeLanguage;
export const loadTranslation = SuperTokensNextjsAPIWrapper.loadTranslation;
export const redirectToAuth = SuperTokensNextjsAPIWrapper.redirectToAuth;
export const setRouterData = SuperTokensNextjsAPIWrapper.setRouterData;

export { SuperTokensWrapper } from "../components/supertokensWrapper";
export { useTranslation } from "../translation/translationContext";
export { useUserContext } from "../usercontext";
Loading

0 comments on commit ad89bbe

Please sign in to comment.