Netlify Auth0 Wrapper
The Netlify Auth0 wrapper makes it easy to use the Beta Auth0 integration on Netlify at runtime.
It makes use of the serverless-jwt package, and injects it with the values defined in the UI by using the integration.
Basic use
To add authentication and authorization checks to a handler, you can wrap it with the withAuth0
method.
import type { Handler } from "@netlify/functions";
import { withAuth0 } from "@netlify/auth0";
export const handler: Handler = withAuth0(
async (event, context) => {
const users = [
];
return {
statusCode: 200,
body: JSON.stringify(users),
};
},
{
auth0: {
required: true,
roles: ["admin"],
},
}
);
To call this handler, you should pass a JWT token in the Authorization
header with the prefix Bearer
/
const userResponse = await fetch("/.netlify/functions/users", {
method: "GET",
headers: {
"Authorization": `Bearer ${token}`
}
});
Chaining
To chain with another integration, you can import the wrap
method from @netlify/integrations
. An example of this can be seen on the netlify docs.
Options
Options can be set by passing in an object to the second parameter of the withAuth0
method. To enable the chaining mentioned above, the object must have a property auth0
which has the option values set.
Option | Type | Description |
---|
required | bool | If set to true , the handler will return 401 if no token is present or the token is invalid |
roles | string[] | If this array is truthy , it requires all roles to be present in the scope claim of the JWT |