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

fix: preserve query params when redirectBack is used #779

Merged
merged 3 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
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;
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
} 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.

2 changes: 1 addition & 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
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());
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
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
Loading