Skip to content
Back to skills

Msal Singleton Pattern

ASecurity

Time Saved: 1+ hour debugging silent auth failures

  • 6 stars
  • 0 votes
  • 0 copies
  • 3 views
  • Added September 3, 2026
ai-agentstypescriptgoreactnextjsnodeazuredebugging

Works with

  • cli

Security analysis

A100/100

Scanned September 3, 2026

npx -y skills add fabioc-aloha/Alex_Skill_Mall --skill msal-singleton-pattern --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Msal Singleton Pattern?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Msal Singleton Pattern
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/fabioc-aloha-msal-singleton-pattern/badge)](https://www.skillsdirectory.com/skills/fabioc-aloha-msal-singleton-pattern)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: msal-singleton-pattern
description: "Time Saved: 1+ hour debugging silent auth failures"
lastReviewed: 2026-04-30
---

# MSAL Singleton Pattern

**Category**: Azure
**Time Saved**: 1+ hour debugging silent auth failures
**Battle-tested**: Yes — HeadstartWebsite, Next.js + Azure projects

---

## The Problem

Your React/Next.js app uses MSAL.js for Azure AD authentication. Login works, but token refresh fails silently. Sometimes users get logged out randomly. The console shows cryptic "interaction_in_progress" errors.

## Why It Happens

MSAL.js `PublicClientApplication` maintains an internal token cache and tracks in-flight auth requests. Creating multiple instances causes:

- Token cache conflicts (different instances don't share tokens)
- Race conditions on `acquireTokenSilent`
- "interaction_in_progress" errors when two instances try to redirect

## The Rule

**Initialize MSAL once, share the singleton across your entire app**

```typescript
// ❌ BROKEN — new instance on every import/render
function getAuth() {
  return new PublicClientApplication(msalConfig);
}

// ✅ CORRECT — module-level singleton with async init
let msalInstance: PublicClientApplication | null = null;
let initPromise: Promise<PublicClientApplication> | null = null;

export async function getMsalInstance(): Promise<PublicClientApplication> {
  if (msalInstance) return msalInstance;
  
  if (!initPromise) {
    initPromise = (async () => {
      const instance = new PublicClientApplication(msalConfig);
      await instance.initialize();  // Required in MSAL.js 2.x+
      msalInstance = instance;
      return instance;
    })();
  }
  
  return initPromise;
}
```

## React Integration

```typescript
// MsalProvider.tsx
import { MsalProvider } from '@azure/msal-react';
import { getMsalInstance } from './msal-singleton';

export function AuthProvider({ children }: { children: React.ReactNode }) {
  const [instance, setInstance] = useState<PublicClientApplication | null>(null);

  useEffect(() => {
    getMsalInstance().then(setInstance);
  }, []);

  if (!instance) return <Loading />;

  return <MsalProvider instance={instance}>{children}</MsalProvider>;
}
```

## Next.js App Router Pattern

```typescript
// app/providers.tsx
'use client';

import { MsalProvider } from '@azure/msal-react';
import { getMsalInstance } from '@/lib/msal-singleton';

export function Providers({ children }: { children: React.ReactNode }) {
  const [instance, setInstance] = useState<IPublicClientApplication | null>(null);

  useEffect(() => {
    getMsalInstance().then(setInstance);
  }, []);

  if (!instance) return null;

  return <MsalProvider instance={instance}>{children}</MsalProvider>;
}
```

## Common Mistakes

| Mistake | Symptom | Fix |
|---------|---------|-----|
| New instance per request | Token cache miss, re-auth required | Use singleton |
| Missing `initialize()` | Silent failures in MSAL 2.x+ | Call and await `initialize()` |
| Instance in component state | Re-created on re-render | Module-level singleton |
| Multiple `MsalProvider`s | "interaction_in_progress" | Single provider at app root |

## Verification

```typescript
// Add to singleton for debugging
export function getMsalDebugInfo() {
  return {
    instanceExists: !!msalInstance,
    accounts: msalInstance?.getAllAccounts() ?? [],
    cacheSize: Object.keys(msalInstance?.getTokenCache()?.serialize() ?? {}).length,
  };
}
```

If `accounts` is empty after login, the singleton isn't being used consistently.

---

**Source**: Promoted from AI-Memory global-knowledge.md (2026-04-27)

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.