JwtService is a service for verifying and signing JWTs. It requires a store, config and an env with JWT_SECRET in it. It also manages socket authentication, storing auth data in the store.

const store: KeyValueService
const config: DeconfConfig
const env: DeconfEnv

const jwt = new JwtService({ store, config, env })

Hierarchy

  • JwtService

Constructors

Methods

  • getRequestAuth is a helper to find and verify an authentication token out of a http headers object. It returns null if one is not found or a AuthToken if it is. It will also throw the same errors as verifyToken.

    const payload = jwt.getRequestAuth({
    authorization: 'bearer abc.def.ghi',
    })

    Parameters

    • headers: any

    Returns null | AuthToken

  • getSocketAuth retrieves the authentication packet for a socket. It takes the socket's id as a parameter and it will throw a http/401 if the packet isn't found.

    const auth = await jwt.getSocketAuth('abcdefg')
    

    Parameters

    • socketId: string

    Returns Promise<SocketAuth>

  • verifyToken verifies a JWT string was signed by the app and conforms to a structure. It throws ApiError(401) errors if something is wrong:

    • auth.tooEarly
    • auth.tokenExpired
    • auth.badToken

    or a StructApiError if the payload does not match the structure. If it doesn't throw, it returns the decoded payload.

    const NameStruct = object({ name: string() })
    const payload = jwt.verifyToken('abc.def.ghi', NameStruct)

    Type Parameters

    • T extends object

    Parameters

    • token: string
    • struct: Struct<T, unknown>

    Returns T

Generated using TypeDoc