Skip to content

Commit

Permalink
chore: fix style
Browse files Browse the repository at this point in the history
  • Loading branch information
dipendraupreti committed May 23, 2024
1 parent 42c71e9 commit fff1918
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 37 deletions.
5 changes: 4 additions & 1 deletion packages/user/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ declare module "@dzangolab/fastify-config" {
users?: typeof userHandlers.users;
};
};
isProfileComplete?: (user: User, request?: FastifyRequest) => boolean;
isProfileComplete?: (
userId: string,
request: FastifyRequest
) => Promise<boolean>;
password?: StrongPasswordOptions;
permissions?: string[];
services?: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import getUserService from "../../../../lib/getUserService";
import profileVerificationClaim from "../../../utils/profileVerificationClaim";

import type { FastifyError, FastifyInstance } from "fastify";
import type { SessionRequest } from "supertokens-node/framework/fastify";
import type { FastifyError, FastifyInstance, FastifyRequest } from "fastify";
import type { RecipeInterface } from "supertokens-node/recipe/session/types";

const createNewSession = (
Expand All @@ -16,7 +15,7 @@ const createNewSession = (
}

const request = input.userContext._default.request
.request as SessionRequest;
.request as FastifyRequest;

const userService = getUserService(
request.config,
Expand All @@ -36,10 +35,10 @@ const createNewSession = (

input.userContext.user = user;

const userProfileBuild = await new profileVerificationClaim(fastify).build(
input.userId,
input.userContext
);
const userProfileBuild = await new profileVerificationClaim(
fastify,
request
).build(input.userId, input.userContext);

input.accessTokenPayload = {
...input.accessTokenPayload,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@ const getGlobalClaimValidators = (
throw new Error("Should never come here");
}

console.log("getGlobalClaimValidators input", input);

const profileVerificationValidator = new ProfileVerificationClaim(
fastify,
input.userContext._default.request.request
).validators.isTrue();

return [
...input.claimValidatorsAddedByOtherRecipes,
new ProfileVerificationClaim(fastify).validators.isTrue(),
profileVerificationValidator,
];
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const verifySession = (
throw new Error("Should never come here");
}

console.log("verifySession input", input);

input.verifySessionOptions = {
checkDatabase:
fastify.config.user.supertokens.checkSessionInDatabase ?? true,
Expand Down
35 changes: 7 additions & 28 deletions packages/user/src/supertokens/utils/profileVerificationClaim.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BooleanClaim } from "supertokens-node/lib/build/recipe/session/claims";

import type { FastifyInstance } from "fastify";
import type { FastifyInstance, FastifyRequest } from "fastify";
import type { SessionClaimValidator } from "supertokens-node/recipe/session";

class ProfileVerificationClaim extends BooleanClaim {
Expand All @@ -11,39 +11,18 @@ class ProfileVerificationClaim extends BooleanClaim {
) => SessionClaimValidator;
};

constructor(fastify: FastifyInstance) {
constructor(fastify: FastifyInstance, request: FastifyRequest) {
super({
key: "pv",
fetchValue: async (userId, userContext) => {
fetchValue: async (userId) => {
const { isProfileComplete } = fastify.config.user;

return isProfileComplete ? isProfileComplete(userContext.user) : true;
return isProfileComplete
? await isProfileComplete(userId, request)
: true;
},
defaultMaxAgeInSeconds: 300,
defaultMaxAgeInSeconds: 0,
});

this.validators = {
...this.validators,
isVerified: (
refetchTimeOnFalseInSeconds = 10,
maxAgeInSeconds = 300
) => ({
...this.validators.hasValue(true, maxAgeInSeconds),

shouldRefetch: (payload: unknown, userContext: unknown) => {
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)
);
},
}),
};
}
}

Expand Down

0 comments on commit fff1918

Please sign in to comment.