React Admin MSAL Authentication Provider

3.1.0 · active · verified Wed Apr 22

ra-auth-msal provides an authentication provider for React-Admin applications, integrating seamlessly with the Microsoft Authentication Library (MSAL) to handle user authentication via Azure Active Directory. The current stable version is 3.1.0, with minor and patch releases occurring as needed to address bugs and introduce small enhancements. Major version updates tend to align with breaking changes in its core dependencies, `@azure/msal-browser` or `react-admin`. Key differentiators include built-in support for various MSAL authentication flows (Authorization Code, Implicit Grant), automatic token refresh, capabilities to fetch access tokens for Microsoft Graph API calls, and the ability to leverage user roles and groups for permission management within React-Admin. It simplifies the setup of MSAL within a React-Admin context, providing a custom login page and an HTTP client helper for authorized requests.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the basic setup of `ra-auth-msal` with a React-Admin application. It shows how to configure MSAL, initialize the `PublicClientApplication` asynchronously (required since MSAL v3), create the `msalAuthProvider`, and integrate it into the `Admin` component along with the provided `LoginPage`.

import React, { useEffect } from "react";
import { Admin, Resource } from 'react-admin';
import { BrowserRouter } from "react-router-dom";
import { PublicClientApplication } from "@azure/msal-browser";
import { LoginPage, msalAuthProvider } from "ra-auth-msal";

const msalConfig = {
  auth: {
    clientId: "12345678-1234-1234-1234-123456789012", // Replace with your Azure AD App (client) ID
    authority: "https://login.microsoftonline.com/common",
    redirectUri: "http://localhost:8080/auth-callback", // Ensure this matches your Azure AD redirect URI
    navigateToLoginRequestUrl: false,
  },
  cache: {
    cacheLocation: "sessionStorage",
    storeAuthStateInCookie: false,
  },
};

const dataProvider = {
  getList: () => Promise.resolve({ data: [{ id: 1, title: 'Post 1' }], total: 1 }),
  getOne: () => Promise.resolve({ data: { id: 1, title: 'Post 1' } }),
  getMany: () => Promise.resolve({ data: [{ id: 1, title: 'Post 1' }] }),
  getManyReference: () => Promise.resolve({ data: [{ id: 1, title: 'Post 1' }], total: 1 }),
  update: () => Promise.resolve({ data: { id: 1, title: 'Post 1' } }),
  updateMany: () => Promise.resolve({ data: [{ id: 1, title: 'Post 1' }] }),
  create: () => Promise.resolve({ data: { id: 1, title: 'Post 1' } }),
  delete: () => Promise.resolve({ data: { id: 1, title: 'Post 1' } }),
  deleteMany: () => Promise.resolve({ data: [{ id: 1, title: 'Post 1' }] })
}; // Placeholder dataProvider

const myMSALObj = new PublicClientApplication(msalConfig);

const App = () => {
  useEffect(() => {
    myMSALObj.initialize();
  }, []);

  const authProvider = msalAuthProvider({
    msalInstance: myMSALObj,
    loginRequest: {
      scopes: ["User.Read"]
    },
    // permissions: getPermissionsFromAccount
  });

  return (
    <BrowserRouter>
      <Admin
        authProvider={authProvider}
        dataProvider={dataProvider}
        title="Example Admin"
        loginPage={LoginPage}
      >
        <Resource name="posts" />
      </Admin>
    </BrowserRouter>
  );
};
export default App;

view raw JSON →