📦 EqualifyEverything / equalify

📄 useMsalTokenRefresh.ts · 46 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46import { useEffect } from 'react';
import { useMsal } from '@azure/msal-react';

export const useMsalTokenRefresh = () => {
  const { instance } = useMsal();

  useEffect(() => {
    const ssoToken = localStorage.getItem('sso_token');
    
    // Only run if we have an SSO token
    if (!ssoToken) {
      return;
    }

    const refreshToken = async () => {
      try {
        const currentAccounts = instance.getAllAccounts();
        if (currentAccounts.length === 0) {
          console.log('No MSAL accounts loaded, skipping token refresh');
          return;
        }

        const account = currentAccounts[0];
        const response = await instance.acquireTokenSilent({
          scopes: ['openid', 'profile', 'email'],
          account: account,
        });

        // Update the stored token
        if (response.idToken) {
          localStorage.setItem('sso_token', response.idToken);
          console.log('SSO token refreshed successfully');
        }
      } catch (error: any) {
        console.error('Token refresh failed:', error);
        // Don't logout here - let the error boundary handle it if it's a real auth issue
      }
    };

    // Refresh token every 10 minutes (before the 15-minute expiry)
    const intervalId = setInterval(refreshToken, 10 * 60 * 1000);

    return () => clearInterval(intervalId);
  }, [instance]);
};