Skip to content

Commit

Permalink
Preserve query params when redirectBack is used
Browse files Browse the repository at this point in the history
  • Loading branch information
anku255 committed Dec 20, 2023
1 parent 51976b8 commit 22493d3
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 12 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 20 additions & 3 deletions lib/build/genericComponentOverrideContext.js

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

1 change: 1 addition & 0 deletions lib/build/utils.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/version.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/ts/superTokens.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { saveCurrentLanguage, TranslationController } from "./translation/transl
import {
appendQueryParamsToURL,
appendTrailingSlashToURL,
getCurrentNormalisedUrlPath,
getCurrentNormalisedUrlPathWithQueryParams,
getDefaultCookieScope,
getOriginOfPage,
isTest,
Expand Down Expand Up @@ -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(
Expand Down
18 changes: 17 additions & 1 deletion lib/ts/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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, string>): string {
if (queryParams === undefined) {
return stringUrl;
Expand Down
2 changes: 1 addition & 1 deletion lib/ts/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
4 changes: 2 additions & 2 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down Expand Up @@ -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",
Expand Down
10 changes: 9 additions & 1 deletion test/end-to-end/signup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
});

Expand Down

0 comments on commit 22493d3

Please sign in to comment.