-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathemailVerificationClaim.ts
51 lines (46 loc) · 2.19 KB
/
emailVerificationClaim.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import EmailVerificationRecipe from "./recipe";
import { BooleanClaim } from "../session/claims";
import { SessionClaimValidator } from "../session";
/**
* We include "Class" in the class name, because it makes it easier to import the right thing (the instance) instead of this.
* */
export class EmailVerificationClaimClass extends BooleanClaim {
constructor() {
super({
key: "st-ev",
async fetchValue(userId, userContext) {
const recipe = EmailVerificationRecipe.getInstanceOrThrowError();
let emailInfo = await recipe.getEmailForUserId(userId, userContext);
if (emailInfo.status === "OK") {
return recipe.recipeInterfaceImpl.isEmailVerified({ userId, email: emailInfo.email, userContext });
} else if (emailInfo.status === "EMAIL_DOES_NOT_EXIST_ERROR") {
// We consider people without email addresses as validated
return true;
} else {
throw new Error("UNKNOWN_USER_ID");
}
},
defaultMaxAgeInSeconds: 300,
});
this.validators = {
...this.validators,
isVerified: (refetchTimeOnFalseInSeconds: number = 10, maxAgeInSeconds: number = 300) => ({
...this.validators.hasValue(true, maxAgeInSeconds),
shouldRefetch: (payload, userContext) => {
const value = this.getValueFromPayload(payload, userContext);
return (
value === undefined ||
this.getLastRefetchTime(payload, userContext)! < Date.now() - maxAgeInSeconds * 1000 ||
(value === false &&
this.getLastRefetchTime(payload, userContext)! <
Date.now() - refetchTimeOnFalseInSeconds * 1000)
);
},
}),
};
}
validators!: BooleanClaim["validators"] & {
isVerified: (refetchTimeOnFalseInSeconds?: number, maxAgeInSeconds?: number) => SessionClaimValidator;
};
}
export const EmailVerificationClaim = new EmailVerificationClaimClass();