Skip to content

Commit

Permalink
donotmerge: add extra logs and limit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
porcellus committed May 24, 2024
1 parent dfab8db commit c152666
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 22 deletions.
1 change: 1 addition & 0 deletions examples/for-tests/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ SuperTokens.init({
apiDomain: getApiDomain(),
websiteBasePath,
},
enableDebugLogs: true,
languageTranslations: {
translations: {
en: {
Expand Down
73 changes: 60 additions & 13 deletions lib/build/multifactorauth-shared.js

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

31 changes: 31 additions & 0 deletions lib/ts/recipe/session/recipe.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import type {
import type { ClaimValidationError, SessionClaimValidator } from "supertokens-web-js/recipe/session";
import type { SessionClaim } from "supertokens-web-js/recipe/session";
import type { RecipeEvent } from "supertokens-web-js/recipe/session/types";
import { logDebugMessage } from "../../logger";

export default class Session extends RecipeModule<unknown, unknown, unknown, NormalisedSessionConfig> {
static instance?: Session;
Expand Down Expand Up @@ -132,9 +133,13 @@ export default class Session extends RecipeModule<unknown, unknown, unknown, Nor
userContext?: UserContext,
navigate?: Navigate
): Promise<void> => {
logDebugMessage("validateGlobalClaimsAndHandleSuccessRedirection called");
userContext = getNormalisedUserContext(userContext);
// First we check if there is an active session
if (!(await this.doesSessionExist({ userContext }))) {
logDebugMessage(
"validateGlobalClaimsAndHandleSuccessRedirection no session exists, so redirecting to auth"
);
// If there is none, we have no way of checking claims, so we redirect to the auth page
// This can happen e.g.: if the user clicked on the email verification link in a browser without an active session
return SuperTokens.getInstanceOrThrow().redirectToAuth({
Expand All @@ -144,11 +149,16 @@ export default class Session extends RecipeModule<unknown, unknown, unknown, Nor
});
}

logDebugMessage("validateGlobalClaimsAndHandleSuccessRedirection validating claims");
// We validate all the global claims
const invalidClaims = await this.validateClaims({ userContext });

logDebugMessage(
`validateGlobalClaimsAndHandleSuccessRedirection: invalid claims: ${invalidClaims.map((c) => c.id)}`
);
if (invalidClaims.length > 0) {
if (successRedirectContext !== undefined) {
logDebugMessage(`validateGlobalClaimsAndHandleSuccessRedirection: saving successRedirectContext`);
// if we have to redirect and we have success context we wanted to use we save it in localstorage
// this way after the other page did solved the validation error it can continue
// the sign in process by calling this function without passing the redirect info
Expand All @@ -164,6 +174,9 @@ export default class Session extends RecipeModule<unknown, unknown, unknown, Nor

// if redirectPath is string that means failed claim had callback that returns path, we redirect there otherwise continue
if (failureRedirectInfo.redirectPath !== undefined) {
logDebugMessage(
`validateGlobalClaimsAndHandleSuccessRedirection: failureRedirection: ${failureRedirectInfo.redirectPath} (${failureRedirectInfo.failedClaim?.id})`
);
// the validation part can throw, but this is handled in all places where this is called,
// since getFailureRedirectionInfo can also throw
if (validateAndCompareOnFailureRedirectionURLToCurrent(failureRedirectInfo.redirectPath)) {
Expand All @@ -173,10 +186,16 @@ export default class Session extends RecipeModule<unknown, unknown, unknown, Nor
}
return SuperTokens.getInstanceOrThrow().redirectToUrl(failureRedirectInfo.redirectPath, navigate);
}
logDebugMessage(
`validateGlobalClaimsAndHandleSuccessRedirection: no redirectPath based on validation errors`
);
}

// If we don't need to redirect because of a claim, we try and execute the original redirection
if (successRedirectContext === undefined) {
logDebugMessage(
`validateGlobalClaimsAndHandleSuccessRedirection: loading successRedirectContext from storage`
);
// if this wasn't set directly we try and grab it from local storage
// generally this means this is a secondary factor completion or emailverification
const successContextStr = await getLocalStorage("supertokens-success-redirection-context");
Expand All @@ -193,6 +212,9 @@ export default class Session extends RecipeModule<unknown, unknown, unknown, Nor
await removeFromLocalStorage("supertokens-success-redirection-context");
}
} else {
logDebugMessage(
`validateGlobalClaimsAndHandleSuccessRedirection: using default because loading failed`
);
// If there was nothing in localstorage we set a default
// this can happen if the user visited email verification screen without an auth recipe redirecting them there
// but already had the email verified and an active session
Expand All @@ -211,9 +233,18 @@ export default class Session extends RecipeModule<unknown, unknown, unknown, Nor
}

if (redirectToPath !== undefined) {
logDebugMessage(
`validateGlobalClaimsAndHandleSuccessRedirection: using redirectToPath directly passed here`
);
successRedirectContext.redirectToPath = redirectToPath;
}

logDebugMessage(
`validateGlobalClaimsAndHandleSuccessRedirection: redirecting with context: ${JSON.stringify(
successRedirectContext
)}`
);

return SuperTokens.getInstanceOrThrow().redirect(
successRedirectContext as SuccessRedirectContext,
navigate,
Expand Down
18 changes: 9 additions & 9 deletions test/end-to-end/passwordless.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ const unregEmail = "[email protected]";
/*
* Tests.
*/
describe("SuperTokens Passwordless", function () {
describe.only("SuperTokens Passwordless", function () {
getPasswordlessTestCases({
authRecipe: "passwordless",
logId: "PASSWORDLESS",
generalErrorRecipeName: "PASSWORDLESS",
});
});

describe("SuperTokens Passwordless w/ all recipes enabled", function () {
describe.only("SuperTokens Passwordless w/ all recipes enabled", function () {
getPasswordlessTestCases({
authRecipe: "all",
logId: "PASSWORDLESS",
Expand Down Expand Up @@ -907,7 +907,7 @@ export function getPasswordlessTestCases({ authRecipe, logId, generalErrorRecipe
consoleLogs.length = 0;
});

it("Successful signin", async function () {
it.only("Successful signin", async function () {
await Promise.all([
page.goto(`${TEST_CLIENT_BASE_URL}/auth`),
page.waitForNavigation({ waitUntil: "networkidle0" }),
Expand Down Expand Up @@ -940,7 +940,7 @@ export function getPasswordlessTestCases({ authRecipe, logId, generalErrorRecipe
]);
});

it("Successful signin w/ email verification", async function () {
it.only("Successful signin w/ email verification", async function () {
await Promise.all([
page.goto(`${TEST_CLIENT_BASE_URL}/auth?mode=required`),
page.waitForNavigation({ waitUntil: "networkidle0" }),
Expand Down Expand Up @@ -974,7 +974,7 @@ export function getPasswordlessTestCases({ authRecipe, logId, generalErrorRecipe
]);
});

it("Successful signin on new device", async function () {
it.only("Successful signin on new device", async function () {
await Promise.all([
page.goto(`${TEST_CLIENT_BASE_URL}/auth`),
page.waitForNavigation({ waitUntil: "networkidle0" }),
Expand Down Expand Up @@ -1015,7 +1015,7 @@ export function getPasswordlessTestCases({ authRecipe, logId, generalErrorRecipe
]);
});

it("Successful signin w/ stored redirectToPath", async function () {
it.only("Successful signin w/ stored redirectToPath", async function () {
await Promise.all([
page.goto(`${TEST_CLIENT_BASE_URL}/auth?redirectToPath=%2Fredirect-here`),
page.waitForNavigation({ waitUntil: "networkidle0" }),
Expand Down Expand Up @@ -1057,7 +1057,7 @@ export function getPasswordlessTestCases({ authRecipe, logId, generalErrorRecipe
]);
});

it("Successful signin w/ stored redirectToPath and email verification", async function () {
it.only("Successful signin w/ stored redirectToPath and email verification", async function () {
await Promise.all([
page.goto(`${TEST_CLIENT_BASE_URL}/auth?redirectToPath=%2Fredirect-here&mode=REQUIRED`),
page.waitForNavigation({ waitUntil: "networkidle0" }),
Expand Down Expand Up @@ -1099,7 +1099,7 @@ export function getPasswordlessTestCases({ authRecipe, logId, generalErrorRecipe
]);
});

it("Successful signin w/ stored redirectToPath (only fragment) and email verification", async function () {
it.only("Successful signin w/ stored redirectToPath (only fragment) and email verification", async function () {
await Promise.all([
page.goto(`${TEST_CLIENT_BASE_URL}/auth?redirectToPath=${encodeURIComponent("#cell=4,1-6,2")}`),
page.waitForNavigation({ waitUntil: "networkidle0" }),
Expand Down Expand Up @@ -1937,7 +1937,7 @@ async function initBrowser(contactMethod, consoleLogs, authRecipe, { defaultCoun
const page = await browser.newPage();
page.on("console", (consoleObj) => {
const log = consoleObj.text();
// console.log(log);
console.log(log);
if (log.startsWith("ST_LOGS")) {
consoleLogs.push(log);
}
Expand Down

0 comments on commit c152666

Please sign in to comment.