diff --git a/CHANGELOG.md b/CHANGELOG.md index d42b7dd74..5b84a503e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html) +## [0.36.1] - 2023-12-20 + +### Fixes + +- Previously, when calling `redirectToAuth` with the `redirectBack` option, query parameters were stripped when redirecting back to the previous page after authentication. This issue has been fixed, and now query parameters are preserved as intended. + ## [0.36.0] - 2023-12-07 ### Changes diff --git a/lib/build/genericComponentOverrideContext.js b/lib/build/genericComponentOverrideContext.js index bd279f688..bd7d250bb 100644 --- a/lib/build/genericComponentOverrideContext.js +++ b/lib/build/genericComponentOverrideContext.js @@ -265,7 +265,7 @@ var SSR_ERROR = * License for the specific language governing permissions and limitations * under the License. */ -var package_version = "0.36.0"; +var package_version = "0.36.1"; /* Copyright (c) 2021, VRAI Labs and/or its affiliates. All rights reserved. * @@ -356,7 +356,17 @@ function getRedirectToPathFromURL() { try { var normalisedURLPath = new NormalisedURLPath__default.default(param).getAsStringDangerous(); var pathQueryParams = param.split("?")[1] !== undefined ? "?".concat(param.split("?")[1]) : ""; - return normalisedURLPath + pathQueryParams; + var pathWithQueryParams = normalisedURLPath + pathQueryParams; + // Ensure a leading "/" if `normalisedUrlPath` is empty but `pathWithQueryParams` is not to ensure proper redirection. + // Example: "?test=1" will not redirect the user to `/?test=1` if we don't add a leading "/". + if ( + normalisedURLPath.length === 0 && + pathWithQueryParams.length > 0 && + !pathWithQueryParams.startsWith("/") + ) { + return "/" + pathWithQueryParams; + } + return pathWithQueryParams; } catch (_a) { return undefined; } @@ -474,6 +484,13 @@ function getCurrentNormalisedUrlPath() { windowHandler.WindowHandlerReference.getReferenceOrThrow().windowHandler.location.getPathName() ); } +function getCurrentNormalisedUrlPathWithQueryParams() { + var normalisedUrlPath = getCurrentNormalisedUrlPath().getAsStringDangerous(); + return ( + normalisedUrlPath + + windowHandler.WindowHandlerReference.getReferenceOrThrow().windowHandler.location.getSearch() + ); +} function appendQueryParamsToURL(stringUrl, queryParams) { if (queryParams === undefined) { return stringUrl; @@ -1169,7 +1186,7 @@ var SuperTokens = /** @class */ (function () { queryParams.show = options.show; } if (options.redirectBack === true) { - queryParams.redirectToPath = getCurrentNormalisedUrlPath().getAsStringDangerous(); + queryParams.redirectToPath = getCurrentNormalisedUrlPathWithQueryParams(); } return [ 4 /*yield*/, diff --git a/lib/build/utils.d.ts b/lib/build/utils.d.ts index 3eafd3f63..8810c2549 100644 --- a/lib/build/utils.d.ts +++ b/lib/build/utils.d.ts @@ -22,6 +22,7 @@ export declare function validateForm( configFormFields: NormalisedFormField[] ): Promise; export declare function getCurrentNormalisedUrlPath(): NormalisedURLPath; +export declare function getCurrentNormalisedUrlPathWithQueryParams(): string; export declare function appendQueryParamsToURL(stringUrl: string, queryParams?: Record): string; export declare function appendTrailingSlashToURL(stringUrl: string): string; export declare function matchRecipeIdUsingQueryParams(recipeId: string): () => boolean; diff --git a/lib/build/version.d.ts b/lib/build/version.d.ts index e85100a93..b9b6c1902 100644 --- a/lib/build/version.d.ts +++ b/lib/build/version.d.ts @@ -1 +1 @@ -export declare const package_version = "0.36.0"; +export declare const package_version = "0.36.1"; diff --git a/lib/ts/superTokens.tsx b/lib/ts/superTokens.tsx index 193b41108..a991488e0 100644 --- a/lib/ts/superTokens.tsx +++ b/lib/ts/superTokens.tsx @@ -28,7 +28,7 @@ import { saveCurrentLanguage, TranslationController } from "./translation/transl import { appendQueryParamsToURL, appendTrailingSlashToURL, - getCurrentNormalisedUrlPath, + getCurrentNormalisedUrlPathWithQueryParams, getDefaultCookieScope, getOriginOfPage, isTest, @@ -193,7 +193,7 @@ export default class SuperTokens { queryParams.show = options.show; } if (options.redirectBack === true) { - queryParams.redirectToPath = getCurrentNormalisedUrlPath().getAsStringDangerous(); + queryParams.redirectToPath = getCurrentNormalisedUrlPathWithQueryParams(); } let redirectUrl = await this.getRedirectUrl( diff --git a/lib/ts/utils.ts b/lib/ts/utils.ts index f13e4c19d..71de1a53a 100644 --- a/lib/ts/utils.ts +++ b/lib/ts/utils.ts @@ -80,7 +80,18 @@ export function getRedirectToPathFromURL(): string | undefined { try { const normalisedURLPath = new NormalisedURLPath(param).getAsStringDangerous(); const pathQueryParams = param.split("?")[1] !== undefined ? `?${param.split("?")[1]}` : ""; - return normalisedURLPath + pathQueryParams; + const pathWithQueryParams = normalisedURLPath + pathQueryParams; + + // Ensure a leading "/" if `normalisedUrlPath` is empty but `pathWithQueryParams` is not to ensure proper redirection. + // Example: "?test=1" will not redirect the user to `/?test=1` if we don't add a leading "/". + if ( + normalisedURLPath.length === 0 && + pathWithQueryParams.length > 0 && + !pathWithQueryParams.startsWith("/") + ) { + return "/" + pathWithQueryParams; + } + return pathWithQueryParams; } catch { return undefined; } @@ -189,6 +200,11 @@ export function getCurrentNormalisedUrlPath(): NormalisedURLPath { return new NormalisedURLPath(WindowHandlerReference.getReferenceOrThrow().windowHandler.location.getPathName()); } +export function getCurrentNormalisedUrlPathWithQueryParams(): string { + const normalisedUrlPath = getCurrentNormalisedUrlPath().getAsStringDangerous(); + return normalisedUrlPath + WindowHandlerReference.getReferenceOrThrow().windowHandler.location.getSearch(); +} + export function appendQueryParamsToURL(stringUrl: string, queryParams?: Record): string { if (queryParams === undefined) { return stringUrl; diff --git a/lib/ts/version.ts b/lib/ts/version.ts index 1fd0f9cc7..dff31d9af 100644 --- a/lib/ts/version.ts +++ b/lib/ts/version.ts @@ -12,4 +12,4 @@ * License for the specific language governing permissions and limitations * under the License. */ -export const package_version = "0.36.0"; +export const package_version = "0.36.1"; diff --git a/package-lock.json b/package-lock.json index d49cefd40..9e01540a7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "supertokens-auth-react", - "version": "0.36.0", + "version": "0.36.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "supertokens-auth-react", - "version": "0.36.0", + "version": "0.36.1", "license": "Apache-2.0", "dependencies": { "intl-tel-input": "^17.0.19", diff --git a/package.json b/package.json index f666f121f..14806b32f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "supertokens-auth-react", - "version": "0.36.0", + "version": "0.36.1", "description": "ReactJS SDK that provides login functionality with SuperTokens.", "main": "./index.js", "engines": { @@ -105,6 +105,7 @@ "build": "rm -rf lib/build && npx rollup -c", "watch": "npx rollup -cw", "watch-mac": "chokidar lib/ts/* -c 'npx rollup -c'", + "watch-mac-2": "chokidar lib/ts/* -c 'npx rollup -c && cp -r lib recipe ui index.d.ts index.js package.json rollup.config.mjs webJsInterfaceSupported.json ../../supertokens-demo/frontend/node_modules/supertokens-auth-react/ && rm -rf ../../supertokens-demo/frontend/node_modules/.cache'", "pretty": "npx pretty-quick .", "build-pretty": "npm run build && npm run pretty && npm run pretty", "lint": "node other/checkTranslationKeys.js && cd lib && eslint ./ts --ext .ts,.tsx", diff --git a/test/end-to-end/signup.test.js b/test/end-to-end/signup.test.js index c747339ca..25f577250 100644 --- a/test/end-to-end/signup.test.js +++ b/test/end-to-end/signup.test.js @@ -134,10 +134,18 @@ describe("SuperTokens SignUp", function () { await page.evaluate(() => window.SuperTokens.redirectToAuth()); await page.waitForNavigation({ waitUntil: "networkidle0" }); let text = await getAuthPageHeaderText(page); - let { pathname: pathAfterRedirectToAuth } = await page.evaluate(() => window.location); + let { pathname: pathAfterRedirectToAuth, href: hrefAfterRedirectToAuth } = await page.evaluate( + () => window.location + ); + + const url = new URL(hrefAfterRedirectToAuth); + const redirectToPath = url.searchParams.get("redirectToPath"); + assert.equal(pathAfterRedirectToAuth, "/auth/"); // Only the EmailPassword recipe has this header on the sign in page assert.deepStrictEqual(text, "Sign In"); + // Test that redirecToPath contains query params + assert.equal(redirectToPath, "?authRecipe=both"); }); });