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(email_verification): should refresh email verification is called once per second #125

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [unreleased]

- Added a debounced mechanism in should refresh email verification to limit verification call to once per second.

## [0.14.0] - 2024-10-07

- Added the OAuth2Provider recipe
Expand Down
1 change: 1 addition & 0 deletions bundle/emailverification.7cd6a51e06782defcef5.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion bundle/emailverification.82f788ef8c6e54629d91.js

This file was deleted.

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

8 changes: 7 additions & 1 deletion lib/build/recipe/emailverification/emailVerificationClaim.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/recipe/emailverification/index.d.ts

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

8 changes: 7 additions & 1 deletion lib/build/recipe/emailverification/index.js

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

4 changes: 3 additions & 1 deletion lib/build/recipe/emailverification/recipe.js

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

11 changes: 10 additions & 1 deletion lib/ts/recipe/emailverification/emailVerificationClaim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ export class EmailVerificationClaimClass extends BooleanClaim {
refresh: this.refresh,
shouldRefresh: (payload, userContext) => {
const DateProvider = DateProviderReference.getReferenceOrThrow().dateProvider;
const currentTime = DateProvider.now();

if (this.shouldRefreshLastCalled && this.shouldRefreshLastCalled > currentTime - 1000) {
AmanAgarwal041 marked this conversation as resolved.
Show resolved Hide resolved
return false;
}

refetchTimeOnFalseInSeconds = refetchTimeOnFalseInSeconds ?? getThresholdAwareDefaultValue(10);

Expand All @@ -46,20 +51,22 @@ export class EmailVerificationClaimClass extends BooleanClaim {
const value = this.getValueFromPayload(payload, userContext);

if (value === undefined) {
this.shouldRefreshLastCalled = currentTime;
return true;
}

const currentTime = DateProvider.now();
const lastRefetchTime = this.getLastFetchedTime(payload, userContext)!;

if (maxAgeInSeconds !== undefined) {
if (lastRefetchTime < currentTime - maxAgeInSeconds * 1000) {
this.shouldRefreshLastCalled = currentTime;
return true;
}
}

if (value === false) {
if (lastRefetchTime < currentTime - refetchTimeOnFalseInSeconds * 1000) {
this.shouldRefreshLastCalled = currentTime;
return true;
}
}
Expand All @@ -82,4 +89,6 @@ export class EmailVerificationClaimClass extends BooleanClaim {
validators!: BooleanClaim["validators"] & {
isVerified: (refetchTimeOnFalseInSeconds?: number, maxAgeInSeconds?: number) => SessionClaimValidator;
};

shouldRefreshLastCalled?: number;
}
4 changes: 3 additions & 1 deletion lib/ts/recipe/emailverification/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import { getNormalisedUserContext } from "../../utils";
import { EmailVerificationClaimClass } from "./emailVerificationClaim";

export default class RecipeWrapper {
static EmailVerificationClaim = Recipe.EmailVerificationClaim;
static get EmailVerificationClaim() {
return Recipe.EmailVerificationClaim;
}

static init(config?: UserInput) {
return Recipe.init(config);
Expand Down
4 changes: 3 additions & 1 deletion lib/ts/recipe/emailverification/recipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ export default class Recipe implements RecipeModule<PreAndPostAPIHookAction, Nor
return;
}
Recipe.instance = undefined;
return;
Recipe.EmailVerificationClaim = new EmailVerificationClaimClass(
() => Recipe.getInstanceOrThrow().recipeImplementation
);
}
}

Expand Down
82 changes: 54 additions & 28 deletions test/unit/emailverificationclaim.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,61 @@
*/
import jsdom from "mocha-jsdom";
import EmailVerification from "../../recipe/emailverification";
import EmailVerificationRecipe from "../../lib/build/recipe/emailverification/recipe";
import SuperTokens from "../../lib/build/supertokens";
import assert from "assert";

/*
* Delay function is used in case of email verification claim as we have added a debouncing
* mechanism that shouldRefresh will return false if multiple calls are done in 1000ms.
* Hence adding delay after each email verification or shouldRefresh calls will result in
* correct tests.
* Test for checking when the shouldRefresh returning false when called multiple times in 1000ms
* is added.
*/
let delayCounter = 0;
const delay = async () => {
++delayCounter;
};

const commonConfig = {
appInfo: {
appName: "SuperTokens",
apiDomain: "api.supertokens.io",
},
dateProvider: function () {
return {
getThresholdInSeconds: function () {
return 7;
},
now: function () {
return Date.now() + delayCounter * 1000;
},
};
},
recipeList: [EmailVerification.init()],
};

describe("EmailVerificationClaim test", function () {
jsdom({ url: "http://localhost.org" });

beforeEach(function () {
SuperTokens.reset();
EmailVerificationRecipe.reset();
delayCounter = 0;
});

describe("EmailVerification Claim", function () {
it("value should be refreshed if it is undefined", async function () {
SuperTokens.init({
appInfo: {
appName: "SuperTokens",
apiDomain: "api.supertokens.io",
},
recipeList: [EmailVerification.init()],
});
SuperTokens.init(commonConfig);

const validator = EmailVerification.EmailVerificationClaim.validators.isVerified();
const shouldRefreshUndefined = await validator.shouldRefresh({});
assert.strictEqual(shouldRefreshUndefined, true);
});

it("value should be refreshed as per maxAgeInSeconds if it is provided", async function () {
SuperTokens.init({
appInfo: {
appName: "SuperTokens",
apiDomain: "api.supertokens.io",
},
recipeList: [EmailVerification.init()],
});
SuperTokens.init(commonConfig);

const validator = EmailVerification.EmailVerificationClaim.validators.isVerified(10, 200);
const shouldRefreshValid = await validator.shouldRefresh({
Expand All @@ -60,13 +82,7 @@ describe("EmailVerificationClaim test", function () {
});

it("value should be refreshed as per refetchTimeOnFalseInSeconds if it is provided", async function () {
SuperTokens.init({
appInfo: {
appName: "SuperTokens",
apiDomain: "api.supertokens.io",
},
recipeList: [EmailVerification.init()],
});
SuperTokens.init(commonConfig);

const validator = EmailVerification.EmailVerificationClaim.validators.isVerified(8);
const shouldRefreshValid = await validator.shouldRefresh({
Expand All @@ -80,24 +96,34 @@ describe("EmailVerificationClaim test", function () {
});

it("value should be refreshed as per default the refetchTimeOnFalseInSeconds if it is not provided", async function () {
SuperTokens.init({
appInfo: {
appName: "SuperTokens",
apiDomain: "api.supertokens.io",
},
recipeList: [EmailVerification.init()],
});
SuperTokens.init(commonConfig);

// NOTE: the default value of refetchTimeOnFalseInSeconds is 10 seconds
const validator = EmailVerification.EmailVerificationClaim.validators.isVerified();
const shouldRefreshValid = await validator.shouldRefresh({
"st-ev": { v: false, t: Date.now() - 9 * 1000 },
});
await delay(); // Added for correct results, since shouldRefresh is debounced
const shouldRefreshExpired = await validator.shouldRefresh({
"st-ev": { v: false, t: Date.now() - 11 * 1000 },
});
assert.strictEqual(shouldRefreshValid, false);
assert.strictEqual(shouldRefreshExpired, true);
});

it("shouldRefresh should return false if called before 1000ms", async function () {
SuperTokens.init(commonConfig);

// NOTE: the default value of refetchTimeOnFalseInSeconds is 10 seconds
const validator = EmailVerification.EmailVerificationClaim.validators.isVerified();
const shouldRefresh = await validator.shouldRefresh({
"st-ev": { v: false, t: Date.now() - 11 * 1000 },
});
const shouldRefreshAgain = await validator.shouldRefresh({
"st-ev": { v: false, t: Date.now() - 11 * 1000 },
});
assert.strictEqual(shouldRefresh, true);
assert.strictEqual(shouldRefreshAgain, false);
});
});
});