jsonwebtoken#VerifyErrors TypeScript Examples
The following examples show how to use
jsonwebtoken#VerifyErrors.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: user.ts From api-example with MIT License | 5 votes |
async function auth(bearerToken: string): Promise<AuthResponse> {
const token = bearerToken.replace('Bearer ', '')
try {
const userId = await cacheExternal.getProp(token)
if (userId) {
return {userId: userId}
}
} catch (err) {
logger.warn(`login.cache.addToken: ${err}`)
}
return new Promise(function(resolve, reject) {
jwt.verify(token, publicKey, verifyOptions, (err: VerifyErrors | null, decoded: object | undefined) => {
if (err === null && decoded !== undefined && (decoded as any).userId !== undefined) {
const d = decoded as {userId: string, exp: number}
const expireAfter = d.exp - Math.round((new Date()).valueOf() / 1000)
cacheExternal.setProp(token, d.userId, expireAfter)
.then(() => {
resolve({userId: d.userId})
})
.catch((err) => {
resolve({userId: d.userId})
logger.warn(`auth.cache.addToken: ${err}`)
})
} else {
resolve({error: {type: 'unauthorized', message: 'Authentication Failed'}})
}
})
})
}