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 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
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.6bb70c065a3e900daf8f.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.

6 changes: 5 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.

10 changes: 9 additions & 1 deletion lib/ts/recipe/emailverification/emailVerificationClaim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ 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;
}

this.shouldRefreshLastCalled = currentTime;

refetchTimeOnFalseInSeconds = refetchTimeOnFalseInSeconds ?? getThresholdAwareDefaultValue(10);

Expand All @@ -49,7 +56,6 @@ export class EmailVerificationClaimClass extends BooleanClaim {
return true;
}

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

if (maxAgeInSeconds !== undefined) {
Expand Down Expand Up @@ -82,4 +88,6 @@ export class EmailVerificationClaimClass extends BooleanClaim {
validators!: BooleanClaim["validators"] & {
isVerified: (refetchTimeOnFalseInSeconds?: number, maxAgeInSeconds?: number) => SessionClaimValidator;
};

shouldRefreshLastCalled?: number;
}
43 changes: 43 additions & 0 deletions test/unit/emailverificationclaim.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ import EmailVerification from "../../recipe/emailverification";
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.
*/
const delay = async () => {
return new Promise((resolve) => {
setTimeout(resolve, 1000);
});
};

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer if you did this through manipulating the DateProvider

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

Expand All @@ -35,6 +49,7 @@ describe("EmailVerificationClaim test", function () {
});

const validator = EmailVerification.EmailVerificationClaim.validators.isVerified();
await delay(); // Added for correct results, since shouldRefresh is debounced
const shouldRefreshUndefined = await validator.shouldRefresh({});
assert.strictEqual(shouldRefreshUndefined, true);
});
Expand All @@ -49,9 +64,11 @@ describe("EmailVerificationClaim test", function () {
});

const validator = EmailVerification.EmailVerificationClaim.validators.isVerified(10, 200);
await delay(); // Added for correct results, since shouldRefresh is debounced
const shouldRefreshValid = await validator.shouldRefresh({
"st-ev": { v: true, t: Date.now() - 199 * 1000 },
});
await delay(); // Added for correct results, since shouldRefresh is debounced
const shouldRefreshExpired = await validator.shouldRefresh({
"st-ev": { v: true, t: Date.now() - 201 * 1000 },
});
Expand All @@ -69,9 +86,11 @@ describe("EmailVerificationClaim test", function () {
});

const validator = EmailVerification.EmailVerificationClaim.validators.isVerified(8);
await delay(); // Added for correct results, since shouldRefresh is debounced
const shouldRefreshValid = await validator.shouldRefresh({
"st-ev": { v: false, t: Date.now() - 7 * 1000 },
});
await delay(); // Added for correct results, since shouldRefresh is debounced
const shouldRefreshExpired = await validator.shouldRefresh({
"st-ev": { v: false, t: Date.now() - 9 * 1000 },
});
Expand All @@ -90,14 +109,38 @@ describe("EmailVerificationClaim test", function () {

// NOTE: the default value of refetchTimeOnFalseInSeconds is 10 seconds
const validator = EmailVerification.EmailVerificationClaim.validators.isVerified();
await delay(); // Added for correct results, since shouldRefresh is debounced
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({
appInfo: {
appName: "SuperTokens",
apiDomain: "api.supertokens.io",
},
recipeList: [EmailVerification.init()],
});

// NOTE: the default value of refetchTimeOnFalseInSeconds is 10 seconds
const validator = EmailVerification.EmailVerificationClaim.validators.isVerified();
await delay(); // Added for correct results, since shouldRefresh is debounced
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);
});
});
});