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 2 commits
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
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,26 @@ 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.35.8] - 2023-11-24

### Changes

rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
- `getRedirectionURL` now supports returning `null` to prevent automatic redirection, useful for customizing the behavior after successful sign-in or sign-up.
anku255 marked this conversation as resolved.
Show resolved Hide resolved

Here's an example of how to use this:

```tsx
EmailPassword.init({
getRedirectionURL: async (context) => {
if (context.action === "SUCCESS") {
return null;
}
// Returning undefined falls back to the default redirection strategy
return undefined;
},
});
```

## [0.35.7] - 2023-11-16
anku255 marked this conversation as resolved.
Show resolved Hide resolved

### Added
Expand Down
6 changes: 6 additions & 0 deletions examples/for-tests/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,9 @@ function getEmailPasswordConfigs({ disableDefaultUI, formFieldType }) {
console.log(`ST_LOGS EMAIL_PASSWORD GET_REDIRECTION_URL ${context.action}`);
if (context.action === "SUCCESS") {
setIsNewUserToStorage("emailpassword", context.isNewRecipeUser);
if (testContext.disableRedirectionAfterSuccessfulSignInUp) {
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
return null;
}
return context.redirectToPath || "/dashboard";
}
},
Expand Down Expand Up @@ -1049,6 +1052,9 @@ function getPasswordlessConfigs({ disableDefaultUI }) {
console.log(`ST_LOGS PASSWORDLESS GET_REDIRECTION_URL ${context.action}`);
if (context.action === "SUCCESS") {
setIsNewUserToStorage("passwordless", context.isNewRecipeUser);
if (testContext.disableRedirectionAfterSuccessfulSignInUp) {
return null;
}
return context.redirectToPath || "/dashboard";
}
},
Expand Down
2 changes: 2 additions & 0 deletions examples/for-tests/src/testContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export function getTestContext() {
signIn: localStorage.getItem("SIGNIN_SETTING_TYPE"),
signUp: localStorage.getItem("SIGNUP_SETTING_TYPE"),
},
disableRedirectionAfterSuccessfulSignInUp:
localStorage.getItem("disableRedirectionAfterSuccessfulSignInUp") === "true",
};
return ret;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/build/genericComponentOverrideContext.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/version.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/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.35.7";
export const package_version = "0.35.8";
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 @@
{
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
"name": "supertokens-auth-react",
"version": "0.35.7",
"version": "0.35.8",
"description": "ReactJS SDK that provides login functionality with SuperTokens.",
"main": "./index.js",
"engines": {
Expand Down
149 changes: 147 additions & 2 deletions test/end-to-end/getRedirectionURL.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ describe("getRedirectionURL Tests", function () {
if (!_isPasswordlessSupported) {
didSkip = true;
this.skip();
return;
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
}

await backendBeforeEach();
Expand Down Expand Up @@ -287,7 +286,6 @@ describe("getRedirectionURL Tests", function () {
if (!_isThirdPartyPasswordlessSupported) {
didSkip = true;
this.skip();
return;
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
}

await backendBeforeEach();
Expand Down Expand Up @@ -377,5 +375,152 @@ describe("getRedirectionURL Tests", function () {
assert.equal(newUserCheck, "thirdpartypasswordless-true");
});
});

describe("No Redirection", function () {
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
describe("Email Password Recipe", function () {
let browser;
let page;

before(async function () {
await backendBeforeEach();

await fetch(`${TEST_SERVER_BASE_URL}/startst`, {
method: "POST",
}).catch(console.error);

browser = await puppeteer.launch({
args: ["--no-sandbox", "--disable-setuid-sandbox"],
headless: true,
});

page = await browser.newPage();
// We need to set the localStorage value before the page loads to ensure ST initialises with the correct value
await page.evaluateOnNewDocument(() => {
localStorage.setItem("disableRedirectionAfterSuccessfulSignInUp", "true");
localStorage.removeItem("isNewUserCheck");
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
});

await clearBrowserCookiesWithoutAffectingConsole(page, []);
});

after(async function () {
await browser.close();
await fetch(`${TEST_SERVER_BASE_URL}/after`, {
method: "POST",
}).catch(console.error);
await fetch(`${TEST_SERVER_BASE_URL}/stopst`, {
method: "POST",
}).catch(console.error);

await screenshotOnFailure(this, browser);
});

it("should not do any redirection after successful sign up", async function () {
await Promise.all([
page.goto(`${TEST_CLIENT_BASE_URL}/auth?authRecipe=emailpassword`),
page.waitForNavigation({ waitUntil: "networkidle0" }),
]);

await toggleSignInSignUp(page);
const urlBeforeSignUp = await page.url();
await defaultSignUp(page);
const urlAfterSignUp = await page.url();

const newUserCheck = await page.evaluate(() => localStorage.getItem("isNewUserCheck"));
assert.equal(newUserCheck, "emailpassword-true");
assert.equal(urlBeforeSignUp, urlAfterSignUp);
});
});

rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
describe("Passwordless recipe", function () {
let browser;
let page;
const exampleEmail = "[email protected]";
// Mocha calls cleanup functions even if the test block is skipped, this helps skipping the after block
let didSkip = false;

before(async function () {
let _isPasswordlessSupported = await isPasswordlessSupported();
if (!_isPasswordlessSupported) {
didSkip = true;
this.skip();
}

await backendBeforeEach();

await fetch(`${TEST_SERVER_BASE_URL}/startst`, {
method: "POST",
headers: [["content-type", "application/json"]],
body: JSON.stringify({
coreConfig: {
passwordless_code_lifetime: 4000,
passwordless_max_code_input_attempts: 3,
},
}),
}).catch(console.error);

browser = await puppeteer.launch({
args: ["--no-sandbox", "--disable-setuid-sandbox"],
headless: true,
});
page = await browser.newPage();
// We need to set the localStorage value before the page loads to ensure ST initialises with the correct value
await page.evaluateOnNewDocument(() => {
localStorage.setItem("disableRedirectionAfterSuccessfulSignInUp", "true");
localStorage.removeItem("isNewUserCheck");
});
await clearBrowserCookiesWithoutAffectingConsole(page, []);
await Promise.all([
page.goto(
`${TEST_CLIENT_BASE_URL}/auth?authRecipe=passwordless&passwordlessContactMethodType=EMAIL`
),
page.waitForNavigation({ waitUntil: "networkidle0" }),
]);
await setPasswordlessFlowType("EMAIL", "USER_INPUT_CODE");
});

after(async function () {
// Dont cleanup if tests were skipped
if (didSkip) {
return;
}
await browser.close();
await fetch(`${TEST_SERVER_BASE_URL}/after`, {
method: "POST",
}).catch(console.error);
await fetch(`${TEST_SERVER_BASE_URL}/stopst`, {
method: "POST",
}).catch(console.error);

return screenshotOnFailure(this, browser);
});

it("should not do any redirection after successful sign up", async function () {
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
await Promise.all([
page.goto(`${TEST_CLIENT_BASE_URL}/auth`),
page.waitForNavigation({ waitUntil: "networkidle0" }),
]);
await setInputValues(page, [{ name: "email", value: exampleEmail }]);
await submitForm(page);
await waitForSTElement(page, "[data-supertokens~=input][name=userInputCode]");

const urlBeforeSignUp = await page.url();

const loginAttemptInfo = JSON.parse(
await page.evaluate(() => localStorage.getItem("supertokens-passwordless-loginAttemptInfo"))
);
const device = await getPasswordlessDevice(loginAttemptInfo);
await setInputValues(page, [{ name: "userInputCode", value: device.codes[0].userInputCode }]);
await submitForm(page);
// wait until network idle to ensure that the page has not been redirected
await page.waitForNetworkIdle();

const urlAfterSignUp = await page.url();
const newUserCheck = await page.evaluate(() => localStorage.getItem("isNewUserCheck"));
assert.equal(newUserCheck, "passwordless-true");
assert.equal(urlBeforeSignUp, urlAfterSignUp);
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
});
});
});
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
});
});
10 changes: 9 additions & 1 deletion test/with-typescript/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import EmailPassword, {
OnHandleEventContext as EmailPasswordOnHandleEventContext,
PreAPIHookContext as EmailPasswordPreAPIHookContext,
} from "../../../recipe/emailpassword";
import Session, { SessionAuth } from "../../../recipe/session";
import Session, { BooleanClaim, SessionAuth } from "../../../recipe/session";
import Multitenancy, { AllowedDomainsClaim } from "../../../recipe/multitenancy";
import ThirdParty, {
GetRedirectionURLContext as ThirdPartyGetRedirectionURLContext,
Expand Down Expand Up @@ -1501,3 +1501,11 @@ SuperTokens.init({
}),
],
});

export const PhoneVerifiedClaim = new BooleanClaim({
id: "phone-verified",
refresh: async () => {
// This is something we have no way of refreshing, so this is a no-op
},
onFailureRedirection: () => null,
});
Loading