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',
})
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')
signToken
takes a JWT payload, signs it and returns it as a JWT string.
You can pass extra options with a JwtSIgnOptions
.
See https://github.com/auth0/node-jsonwebtoken#usage for more options.
const token = jwt.signToken({ name: 'Geoff' }, { expiresIn: '55m' })
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)
Generated using TypeDoc
JwtService
is a service for verifying and signing JWTs. It requires astore
,config
and anenv
withJWT_SECRET
in it. It also manages socket authentication, storing auth data in thestore
.