DEV Community

Hari Manoj
Hari Manoj

Posted on

Moving Beyond Official MSAL: A Better Way to Handle Entra ID in Next.js

Microsoft's official MSAL libraries are powerful, but they were not built with the Next.js App Router in mind. Developers attempting to integrate @azure/msal-react often face immediate hurdles: hydration mismatches, complex middleware logic, and the difficulty of sharing authentication state between Client and Server Components.

The @chemmangat/msal-next library was designed specifically to bridge this gap. Currently sitting at 4,775 downloads, it has become a preferred choice for teams who need production-ready Microsoft authentication without the typical boilerplate.

The Problem with Standard Implementations

In a standard Next.js 13+ environment, the MSAL provider must be placed in the layout. Because MSAL relies heavily on browser APIs like localStorage and window.crypto, wrapping your root layout often forces your entire application into client-side rendering or causes "Text content does not match" errors during hydration.

Furthermore, implementing Role-Based Access Control (RBAC) usually requires manual claim parsing on every single protected route.

How msal-next Solves This

This library abstracts the complexity of the MSAL Browser instance and provides a Next-native interface. Key technical advantages include:

  1. Automated Scaffolding: Instead of manual configuration, you can initialize your entire auth flow via CLI using npx @chemmangat/msal-next-cli init.
  2. Per-Page Configuration: You can export a PageAuthConfig object directly from any page file to define required roles or custom validation logic.
  3. App Router Native: It handles the hydration boundaries internally, allowing you to keep your layouts clean.
  4. Middleware Integration: It provides a structured way to handle redirects at the edge before the page even begins to render.

Quick Start

Install the library and its peer dependencies with a single command:

npm install @chemmangat/msal-next @azure/msal-browser @azure/msal-react
Enter fullscreen mode Exit fullscreen mode

1. Initialize the Provider

In your app/layout.tsx, wrap the children with the MSALProvider. This handles the initialization of the Public Client Application (PCA) automatically.

import { MSALProvider } from '@chemmangat/msal-next';

export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <body>
        <MSALProvider
          clientId={process.env.NEXT_PUBLIC_CLIENT_ID!}
          tenantId={process.env.NEXT_PUBLIC_TENANT_ID!}
        >
          {children}
        </MSALProvider>
      </body>
    </html>
  );
}
Enter fullscreen mode Exit fullscreen mode

2. Protect a Page with RBAC

Version 5.3.3 allows you to protect routes using a simple configuration object exported directly from the page file.

// app/admin/page.tsx
import { PageAuthConfig } from '@chemmangat/msal-next';

export const authConfig: PageAuthConfig = {
  required: true,
  roles: ['GlobalAdmin', 'ApplicationEditor'],
  onDenied: '/unauthorized'
};

export default function AdminPage() {
  return <h1>Secure Admin Dashboard</h1>;
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

With over 4,700 downloads, @chemmangat/msal-next demonstrates that enterprise authentication does not have to be a bottleneck in Next.js development. By shifting the focus from configuration to implementation, it allows developers to spend less time on OAuth flows and more time on core application features.

For full documentation and advanced multi-tenant configurations, visit the GitHub repository.

Top comments (0)