From d930ebc1ed711631454376a13a22b66f31cc1a7d Mon Sep 17 00:00:00 2001 From: Philip Korsholm <88927411+pKorsholm@users.noreply.github.com> Date: Thu, 21 Mar 2024 10:01:58 +0100 Subject: [PATCH] Feat(user): Invite expiration fix (#6758) * fix invite duration error * add changeset * undo change to auth * re-add expiresIn --- .changeset/breezy-clocks-applaud.md | 5 +++++ packages/user/src/services/invite.ts | 24 +++++++++++++----------- 2 files changed, 18 insertions(+), 11 deletions(-) create mode 100644 .changeset/breezy-clocks-applaud.md diff --git a/.changeset/breezy-clocks-applaud.md b/.changeset/breezy-clocks-applaud.md new file mode 100644 index 0000000000..291b443938 --- /dev/null +++ b/.changeset/breezy-clocks-applaud.md @@ -0,0 +1,5 @@ +--- +"@medusajs/user": patch +--- + +feat(user): invite expiry fix diff --git a/packages/user/src/services/invite.ts b/packages/user/src/services/invite.ts index 553735bad7..db87a4697d 100644 --- a/packages/user/src/services/invite.ts +++ b/packages/user/src/services/invite.ts @@ -6,6 +6,7 @@ import { MedusaError, ModulesSdkUtils, arrayDifference, + isString, } from "@medusajs/utils" import jwt, { JwtPayload } from "jsonwebtoken" @@ -17,7 +18,7 @@ type InjectedDependencies = { } // 1 day -const DEFAULT_VALID_INVITE_DURATION = 60 * 60 * 24 +const DEFAULT_VALID_INVITE_DURATION = 60 * 60 * 24 * 1000 export default class InviteService< TEntity extends Invite = Invite @@ -71,9 +72,7 @@ export default class InviteService< const invites = await super.create(data_, context) - const expiresIn: number = - parseInt(this.getOption("valid_duration")) || - DEFAULT_VALID_INVITE_DURATION + const expiresIn: number = this.getValidDuration() const updates = invites.map((invite) => { return { @@ -113,9 +112,7 @@ export default class InviteService< } } - const expiresIn: number = - parseInt(this.getOption("valid_duration")) || - DEFAULT_VALID_INVITE_DURATION + const expiresIn: number = this.getValidDuration() const updates = invites.map((invite) => { return { @@ -151,9 +148,7 @@ export default class InviteService< private generateToken(data: any): string { const jwtSecret: string = this.getOption("jwt_secret") - const expiresIn: number = - parseInt(this.getOption("valid_duration")) || - DEFAULT_VALID_INVITE_DURATION + const expiresIn: number = this.getValidDuration() / 1000 if (!jwtSecret) { throw new MedusaError( @@ -163,11 +158,18 @@ export default class InviteService< } return jwt.sign(data, jwtSecret, { - expiresIn, jwtid: crypto.randomUUID(), + expiresIn, }) } + private getValidDuration(): number { + return ( + parseInt(this.getOption("valid_duration")) || + DEFAULT_VALID_INVITE_DURATION + ) + } + private validateToken(data: any): JwtPayload { const jwtSecret = this.getOption("jwt_secret")