Skip to content

Commit

Permalink
Feat(user): Invite expiration fix (#6758)
Browse files Browse the repository at this point in the history
* fix invite duration error

* add changeset

* undo change to auth

* re-add expiresIn
  • Loading branch information
pKorsholm authored Mar 21, 2024
1 parent 60070fb commit d930ebc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/breezy-clocks-applaud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/user": patch
---

feat(user): invite expiry fix
24 changes: 13 additions & 11 deletions packages/user/src/services/invite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
MedusaError,
ModulesSdkUtils,
arrayDifference,
isString,
} from "@medusajs/utils"
import jwt, { JwtPayload } from "jsonwebtoken"

Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(
Expand All @@ -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")

Expand Down

0 comments on commit d930ebc

Please sign in to comment.