npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ra-auth-auth0

v2.0.1

Published

An auth provider for [react-admin](https://github.com/marmelab/react-admin) which handles authentication via a [Auth0](https://auth0.com) instance.

Downloads

3,568

Readme

ra-auth-auth0

An auth provider for react-admin which handles authentication via a Auth0 instance.

This package provides:

  • An Auth0AuthProvider for react-admin

Installation

yarn add ra-auth-auth0
# or
npm install --save ra-auth-auth0

Example usage

// in src/App.tsx
import React, { useEffect, useRef, useState } from 'react';
import { Admin, Resource } from 'react-admin';
import { BrowserRouter } from 'react-router-dom';
import { Auth0AuthProvider } from 'ra-auth-auth0';
import { Auth0Client } from '@auth0/auth0-spa-js';
import dataProvider from './dataProvider';
import posts from './posts';

const auth0 = new Auth0Client({
    domain: import.meta.env.VITE_AUTH0_DOMAIN,
    clientId: import.meta.env.VITE_AUTH0_CLIENT_ID,
    cacheLocation: 'localstorage',
    authorizationParams: {
        audience: import.meta.env.VITE_AUTH0_AUDIENCE,
    },
});

const authProvider = Auth0AuthProvider(auth0, {
    loginRedirectUri: import.meta.env.VITE_LOGIN_REDIRECT_URL,
    logoutRedirectUri: import.meta.env.VITE_LOGOUT_REDIRECT_URL,
});

const App = () => {
    return (
        <BrowserRouter>
            <Admin authProvider={authProvider} dataProvider={dataProvider}>
                <Resource name="posts" {...posts} />
            </Admin>
        </BrowserRouter>
    );
};
export default App;

Note: You need to wrap your app in a BrowserRouter component from react-router-dom for the Auth0AuthProvider to work.

Auth0AuthProvider Parameters

  • loginRedirectUri - optional - URI used to override the redirect URI after successful login. Defaults to react-admin /auth-callback route.
  • logoutRedirectUri - optional - URI used to override the redirect URI after successful logout. Defaults to the current url.
  • redirectOnCheckAuth - optional - If set to true (the default), redirect users to the Auth0 login page inside the checkAuth method. Set it to false if you want users to go through a custom login page first.

Passing The Auth0 Token To Your Backend

If you want to pass the Auth0 authentication token to your backend, you can use the httpClient exported by this package. For instance, here's how to use it with the ra-data-json-server dataProvider:

import jsonServerProvider from 'ra-data-json-server';
import { httpClient } from 'ra-auth-auth0';
import { auth0 } from './auth0';

const dataProvider = jsonServerProvider(
    'http://localhost:3000',
    httpClient(auth0)
);

Handling Permissions

In order to get the users roles directly from Auth0, you have to configure it so that it includes them in the authentication token. This is done by adding an Action to the login flow.

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. To change this behavior, override the getPermissions method:

import { Auth0AuthProvider } from 'ra-auth-auth0';
import { Auth0Client } from './auth0';

const authProvider = {
    ...Auth0AuthProvider(Auth0Client),
    async getPermissions() {
        if (!(await client.isAuthenticated())) {
            return;
        }

        const claims = await client.getIdTokenClaims();
        return claims['https://my-app.example.com/roles'];
    },
};

Demo

You can find a working demo, along with the source code, in this project's repository: https://github.com/marmelab/ra-auth-auth0

License

This auth provider is licensed under the MIT License and sponsored by Marmelab.