An auth provider for react-admin which handles authentication via a Auth0 / Okta instance.
This repository contains:
- The actual
ra-auth-auth0package - A simple demo app you can run locally to try out
ra-auth-auth0with your own Auth0 instance
- You need to have a Auth0 account
- Clone this project
We need to add some minimal configuration to our Auth0 instance to use it. This need to be done from the Auth0 Admin Console.
- Go to the Auth0 Dashboard
- Select your Auth0 Application (or create a new one)
- Add
[email protected]user to your Auth0 Application. For this, you need to go to theUser Managementsection and create a new user. - Add
adminto theRolessection of your Auth0 Application. Assign[email protected]to theadminrole. - Do the same for
[email protected]and theuserrole. - Go to the
Applicationssection of your Auth0 Application and selectSettings - Create a
single pageapplication with the following settings:- Allowed Callback URLs:
http://127.0.0.1:8081/auth-callback - Allowed Logout URLs:
http://127.0.0.1:8081 - Allowed Web Origins:
http://127.0.0.1:8081 - Allowed Origins (CORS):
http://127.0.0.1:8081
- Allowed Callback URLs:
If you want to use permissions, you need to add the following to your Auth0 Application as an Action:
exports.onExecutePostLogin = async (event, api) => {
const namespace = 'https://my-app.example.com';
if (event.authorization) {
api.idToken.setCustomClaim(`${namespace}/roles`, event.authorization.roles);
api.accessToken.setCustomClaim(`${namespace}/roles`, event.authorization.roles);
}
}The authProvider exported by this package will look for a claim that has a name with the term role in it and return its value.
For react-admin, you need to setup environment variables. You can do this by creating a .env file in the root of the project. The following variables are required:
VITE_AUTH0_DOMAIN="your-domain.auth0.com"
VITE_AUTH0_CLIENT_ID="your-client-id"
VITE_AUTH0_AUDIENCE="https://your-domain.auth0.com/api/v2/" // optional but recommended for having non opaque tokens
VITE_LOGIN_REDIRECT_URL="http://127.0.0.1:8081/auth-callback"
VITE_LOGOUT_REDIRECT_URL="http://127.0.0.1:8081"- Run
make install startto install the dependencies and start the Demo App
Now that all is configured and running, you can browse to http://127.0.0.1:8081/ to access the React Admin App.
- Signing in with
[email protected]will only grant theuserrole permissions - Signing in with
[email protected]will grant fulladminrole permissions, allowing for instance to see the 'Users' resource in the main menu
Feel free to play around with this demo, along with the Auth0 config, to understand better how it works!
The demo app uses a JSON Server to provide a REST API. You can find the configuration in the demo-fake-api folder.
It shows how use middleware to validate the JWT token via the Auth0 API.
This repository and the code it contains are licensed under the MIT License and sponsored by marmelab.