@usefultools/auth0-node
v1.2.3
Published
Simple Auth0 module for token verification
Downloads
9
Readme
Auth0 Node
Simple Auth0 module for token verification.
Prereqs & Install
- Node >=9.10.0
- npm >=6.1.0
Please note that the TypeScript target is ES6.
npm install @usefultools/auth0-node
Usage
1) Initialise the client 👾
import { Auth0 } from "@usefultools/auth0-node"
const auth0 = new Auth0("test.auth0.com")
2) Verify, verify, verify 😎
import { Auth0, Session } from "@usefultools/auth0-node"
import { isNonEmptyString } from "@usefultools/utils"
const auth0 = new Auth0("test.auth0.com")
async function isAuthorised(req: Request, res: Response, next: Next): Promise<void> {
const { headers } = req
const bearerStr = "Bearer "
const authHeader = headers[HeaderKey.Authorization]
if (isNonEmptyString(authHeader) && authHeader.startsWith(bearerStr)) {
const token = authHeader.substring(bearerStr.length, authHeader.length)
try {
const decoded = await auth0.verifyToken(token)
Object.assign(req, { ctx: {
...req.ctx,
session: decoded,
}})
next()
} catch (err) {
res.error(unauthorized(err))
}
} else {
res.error(unauthorized("Invalid or missing token"))
}
}
Contributing
If you have comments, complaints, or ideas for improvements, feel free to open an issue or a pull request! See Contributing guide for details about project setup, testing, etc.
Author and license
This library was created by @LITCHI.IO. Main author and maintainer is Slavo Vojacek.
Contributors: Slavo Vojacek
@usefultools/auth0-node
is available under the ISC license. See the LICENSE file for more info.