From fff1918bd4da09dd67ae18ed18b83c72c7c6d3be Mon Sep 17 00:00:00 2001 From: Dipendra Upreti Date: Thu, 23 May 2024 17:27:12 +0545 Subject: [PATCH] chore: fix style --- packages/user/src/index.ts | 5 ++- .../config/session/createNewSession.ts | 13 ++++--- .../session/getGlobalClaimValidators.ts | 9 ++++- .../recipes/config/session/verifySession.ts | 2 ++ .../utils/profileVerificationClaim.ts | 35 ++++--------------- 5 files changed, 27 insertions(+), 37 deletions(-) diff --git a/packages/user/src/index.ts b/packages/user/src/index.ts index 367d2ca38..b09c8942e 100644 --- a/packages/user/src/index.ts +++ b/packages/user/src/index.ts @@ -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; password?: StrongPasswordOptions; permissions?: string[]; services?: { diff --git a/packages/user/src/supertokens/recipes/config/session/createNewSession.ts b/packages/user/src/supertokens/recipes/config/session/createNewSession.ts index 771d16b4f..17ca0fe7a 100644 --- a/packages/user/src/supertokens/recipes/config/session/createNewSession.ts +++ b/packages/user/src/supertokens/recipes/config/session/createNewSession.ts @@ -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 = ( @@ -16,7 +15,7 @@ const createNewSession = ( } const request = input.userContext._default.request - .request as SessionRequest; + .request as FastifyRequest; const userService = getUserService( request.config, @@ -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, diff --git a/packages/user/src/supertokens/recipes/config/session/getGlobalClaimValidators.ts b/packages/user/src/supertokens/recipes/config/session/getGlobalClaimValidators.ts index c5a37affe..870f9393e 100644 --- a/packages/user/src/supertokens/recipes/config/session/getGlobalClaimValidators.ts +++ b/packages/user/src/supertokens/recipes/config/session/getGlobalClaimValidators.ts @@ -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, ]; }; }; diff --git a/packages/user/src/supertokens/recipes/config/session/verifySession.ts b/packages/user/src/supertokens/recipes/config/session/verifySession.ts index a0c288e96..edd2d8935 100644 --- a/packages/user/src/supertokens/recipes/config/session/verifySession.ts +++ b/packages/user/src/supertokens/recipes/config/session/verifySession.ts @@ -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, diff --git a/packages/user/src/supertokens/utils/profileVerificationClaim.ts b/packages/user/src/supertokens/utils/profileVerificationClaim.ts index 3414abe2d..339aedf54 100644 --- a/packages/user/src/supertokens/utils/profileVerificationClaim.ts +++ b/packages/user/src/supertokens/utils/profileVerificationClaim.ts @@ -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 { @@ -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) - ); - }, - }), - }; } }