Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow returning null from getRedirectionURL override #766

Merged
merged 24 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 56 additions & 3 deletions lib/build/genericComponentOverrideContext.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions lib/build/index2.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions lib/build/logger.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/build/recipe/recipeModule/index.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions lib/build/recipe/recipeModule/types.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/build/recipe/session/utils.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions lib/build/recipeModule-shared.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions lib/build/session-shared2.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/build/superTokens.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions lib/build/types.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions lib/ts/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* 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.
*/

import { package_version as version } from "./version";

const SUPERTOKENS_DEBUG_NAMESPACE = "com.supertokens.auth-react";

let __debugLogsEnabled = false;

export function enableLogging(): void {
__debugLogsEnabled = true;
}

export function logDebugMessage(message: string): void {
if (__debugLogsEnabled) {
// eslint-disable-next-line no-console
console.log(
`${SUPERTOKENS_DEBUG_NAMESPACE} {t: "${new Date().toISOString()}", message: "${message}", supertokens-auth-react-ver: "${version}"}`
);
}
}
15 changes: 14 additions & 1 deletion lib/ts/recipe/recipeModule/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* under the License.
*/

import { logDebugMessage } from "../../logger";
import SuperTokens from "../../superTokens";
import { appendQueryParamsToURL } from "../../utils";

Expand All @@ -32,12 +33,24 @@ export default abstract class RecipeModule<
queryParams?: Record<string, string>
): Promise<void> => {
let redirectUrl = await this.getRedirectUrl(context);

if (redirectUrl === null) {
logDebugMessage(
`Skipping redirection because the user override returned null for context ${JSON.stringify(
context,
null,
2
)}`
);
return;
}

redirectUrl = appendQueryParamsToURL(redirectUrl, queryParams);
return SuperTokens.getInstanceOrThrow().redirectToUrl(redirectUrl, history);
};

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
getRedirectUrl = async (context: GetRedirectionURLContextType): Promise<string> => {
getRedirectUrl = async (context: GetRedirectionURLContextType): Promise<string | null> => {
// If getRedirectionURL provided by user.
const redirectUrl = await this.config.getRedirectionURL(context);
if (redirectUrl !== undefined) {
Expand Down
4 changes: 2 additions & 2 deletions lib/ts/recipe/recipeModule/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export type RecipePostAPIHookFunction<Action> = (context: RecipePostAPIHookConte
export type RecipeOnHandleEventFunction<EventType> = (context: EventType) => void;

export type UserInput<GetRedirectionURLContextType, Action, OnHandleEventContextType> = {
getRedirectionURL?: (context: GetRedirectionURLContextType) => Promise<string | undefined>;
getRedirectionURL?: (context: GetRedirectionURLContextType) => Promise<string | undefined | null>;
preAPIHook?: RecipePreAPIHookFunction<Action>;
postAPIHook?: RecipePostAPIHookFunction<Action>;
onHandleEvent?: RecipeOnHandleEventFunction<OnHandleEventContextType>;
Expand All @@ -37,7 +37,7 @@ export type Config<GetRedirectionURLContextType, Action, OnHandleEventContextTyp
>;

export type NormalisedConfig<GetRedirectionURLContextType, Action, OnHandleEventContextType> = {
getRedirectionURL: (context: GetRedirectionURLContextType) => Promise<string | undefined>;
getRedirectionURL: (context: GetRedirectionURLContextType) => Promise<string | undefined | null>;
onHandleEvent: RecipeOnHandleEventFunction<OnHandleEventContextType>;
useShadowDom: boolean;
rootStyle: string;
Expand Down
Loading
Loading