v5.3.4 — latest

Microsoft auth for Next.js App Router

MSAL integration that handles the hard parts — cookie sync, token refresh, multi-tenant access, and msal-browser v3 through v5 — so you don't have to.

terminal

$ npx @chemmangat/msal-next-cli init

✔ Client ID? 97f1e8c5-...✔ Tenant? common✔ Cache? sessionStorage

✅ .env.local · layout.tsx · app/auth/page.tsx — done

What it handles

msal-browser v3 · v4 · v5

Runtime version detection. No code changes needed when you upgrade MSAL.

Multi-tenant access control

Allow/block by domain or tenant ID. Require MFA. Detect B2B guests.

Multi-account switching

Up to five accounts in parallel. Switch without signing out.

Auto token refresh

Reads real expiry from the token. Refreshes silently before it lapses.

CLI scaffold + codemod

Init wires everything. Migrate rewrites popup calls to redirect.

Cookie sync out of the box

msal.account cookie written on every login — middleware just works.

Wrap once, done

Add MSALProvider to your root layout. Cookie sync, token refresh, and redirect handling are all automatic from there.

Use useMsalAuth() in any client component to get the account, trigger login, or acquire tokens.

Full API reference
layout.tsx
import { MSALProvider } from '@chemmangat/msal-next';

export default function RootLayout({ children }) {
  return (
    <html>
      <body>
        <MSALProvider
          clientId={process.env.NEXT_PUBLIC_CLIENT_ID}
          autoRefreshToken
        >
          {children}
        </MSALProvider>
      </body>
    </html>
  );
}

Ready to add Microsoft auth?

One command gets you to a working sign-in page.