Skip to content

Commit

Permalink
Hotfix: jwt utils types fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
joaobose committed Jul 2, 2023
1 parent f87c8d2 commit 4e48025
Showing 1 changed file with 10 additions and 22 deletions.
32 changes: 10 additions & 22 deletions src/utils/jwt.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Request, Response } from 'express'
import { Request } from 'express'
import * as jwt from 'jsonwebtoken'
import * as uuid from 'uuid'

import { JWTErrors, UnauthorizedError } from '../errors/common'

const defaultIndexIsValid = (id: string | number) =>
const defaultIndexIsValid = <ID extends string | number>(id: ID) =>
typeof id === 'string' ? uuid.validate(id) : !isNaN(id)

export namespace BearerJWT {
Expand All @@ -31,16 +31,11 @@ export namespace BearerJWT {
}
}

export const verifyUserToken = async (
export const verifyUserToken = async <ID extends string | number>(
scheme: ReturnType<typeof decodeBearerScheme>,
secretSource: (id: string | number) => Promise<{ secret: string }>,
{
indexProp,
indexIsValid,
}: {
indexProp: string
indexIsValid: (id: string | number) => boolean
} = { indexProp: 'user', indexIsValid: defaultIndexIsValid }
secretSource: (id: ID) => Promise<{ secret: string } | null>,
indexProp = 'userId',
indexIsValid = defaultIndexIsValid<ID>
) => {
const id = scheme.decoded[indexProp]
if (!indexIsValid(id))
Expand Down Expand Up @@ -86,18 +81,11 @@ export namespace httpBearerJWT {
}
}

export const verifyUserToken = async (
req: Request,
res: Response,
export const verifyUserToken = async <ID extends string | number>(
scheme: ReturnType<typeof decodeBearerScheme>,
secretSource: (id: string | number) => Promise<{ secret: string }>,
{
indexProp,
indexIsValid,
}: {
indexProp: string
indexIsValid: (id: string | number) => boolean
} = { indexProp: 'user', indexIsValid: defaultIndexIsValid }
secretSource: (id: ID) => Promise<{ secret: string } | null>,
indexProp = 'userId',
indexIsValid = defaultIndexIsValid<ID>
) => {
const id = scheme.decoded[indexProp]
if (!indexIsValid(id)) throw JWTErrors.BadJWTError(scheme.token)
Expand Down

0 comments on commit 4e48025

Please sign in to comment.