@chemmangat/msal-next 4.0.2 → 4.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -264,6 +264,41 @@ interface MsalAuthConfig {
264
264
  * ```
265
265
  */
266
266
  onInitialized?: (instance: IPublicClientApplication) => void;
267
+ /**
268
+ * Enable automatic token refresh
269
+ *
270
+ * @remarks
271
+ * Automatically refreshes access tokens before they expire to prevent
272
+ * session interruptions. Tokens are refreshed silently in the background.
273
+ *
274
+ * @defaultValue false
275
+ *
276
+ * @example
277
+ * ```tsx
278
+ * <MSALProvider
279
+ * clientId="..."
280
+ * autoRefreshToken={true}
281
+ * refreshBeforeExpiry={300} // Refresh 5 min before expiry
282
+ * >
283
+ * {children}
284
+ * </MSALProvider>
285
+ * ```
286
+ */
287
+ autoRefreshToken?: boolean;
288
+ /**
289
+ * Refresh token this many seconds before expiry
290
+ *
291
+ * @remarks
292
+ * Only used when autoRefreshToken is enabled.
293
+ *
294
+ * @defaultValue 300 (5 minutes)
295
+ *
296
+ * @example
297
+ * ```tsx
298
+ * refreshBeforeExpiry={600} // Refresh 10 minutes before expiry
299
+ * ```
300
+ */
301
+ refreshBeforeExpiry?: number;
267
302
  }
268
303
  /**
269
304
  * Props for MsalAuthProvider component
@@ -283,7 +318,7 @@ interface MsalAuthProviderProps extends MsalAuthConfig {
283
318
  * @returns The MSAL instance or null if not initialized
284
319
  */
285
320
  declare function getMsalInstance(): PublicClientApplication | null;
286
- declare function MsalAuthProvider({ children, loadingComponent, onInitialized, ...config }: MsalAuthProviderProps): react_jsx_runtime.JSX.Element;
321
+ declare function MsalAuthProvider({ children, loadingComponent, onInitialized, autoRefreshToken, refreshBeforeExpiry, ...config }: MsalAuthProviderProps): react_jsx_runtime.JSX.Element;
287
322
 
288
323
  /**
289
324
  * Zero-Config Protected Routes - Type Definitions
@@ -393,12 +428,15 @@ interface MSALProviderProps extends MsalAuthProviderProps {
393
428
  }
394
429
  /**
395
430
  * Pre-configured MSALProvider component for Next.js App Router layouts.
396
- * This component is already marked as 'use client', so you can use it directly
397
- * in your server-side layout.tsx without needing to create a separate client component.
431
+ *
432
+ * @remarks
433
+ * This component is already marked as 'use client' internally, so you can import
434
+ * and use it directly in your server-side layout.tsx without adding 'use client'
435
+ * to your layout file.
398
436
  *
399
437
  * @example
400
438
  * ```tsx
401
- * // app/layout.tsx
439
+ * // app/layout.tsx (Server Component - no 'use client' needed!)
402
440
  * import { MSALProvider } from '@chemmangat/msal-next'
403
441
  *
404
442
  * export default function RootLayout({ children }) {
@@ -416,6 +454,13 @@ interface MSALProviderProps extends MsalAuthProviderProps {
416
454
  * )
417
455
  * }
418
456
  * ```
457
+ *
458
+ * @security
459
+ * - All authentication happens client-side (browser)
460
+ * - Tokens are never sent to your Next.js server
461
+ * - Uses Microsoft's official MSAL library
462
+ * - Supports secure token storage (sessionStorage/localStorage)
463
+ * - No server-side token handling required
419
464
  */
420
465
  declare function MSALProvider({ children, protection, ...props }: MSALProviderProps): react_jsx_runtime.JSX.Element;
421
466
 
@@ -948,6 +993,80 @@ interface UseRolesReturn {
948
993
  */
949
994
  declare function useRoles(): UseRolesReturn;
950
995
 
996
+ /**
997
+ * Automatic token refresh hook
998
+ * Refreshes tokens before they expire to prevent session interruptions
999
+ */
1000
+ interface UseTokenRefreshOptions {
1001
+ /**
1002
+ * Enable automatic token refresh
1003
+ * @default true
1004
+ */
1005
+ enabled?: boolean;
1006
+ /**
1007
+ * Refresh token this many seconds before expiry
1008
+ * @default 300 (5 minutes)
1009
+ */
1010
+ refreshBeforeExpiry?: number;
1011
+ /**
1012
+ * Scopes to refresh
1013
+ * @default ['User.Read']
1014
+ */
1015
+ scopes?: string[];
1016
+ /**
1017
+ * Callback when token is refreshed
1018
+ */
1019
+ onRefresh?: (expiresIn: number) => void;
1020
+ /**
1021
+ * Callback when refresh fails
1022
+ */
1023
+ onError?: (error: Error) => void;
1024
+ }
1025
+ interface UseTokenRefreshReturn {
1026
+ /**
1027
+ * Seconds until token expires
1028
+ */
1029
+ expiresIn: number | null;
1030
+ /**
1031
+ * Whether token is expiring soon
1032
+ */
1033
+ isExpiringSoon: boolean;
1034
+ /**
1035
+ * Manually trigger token refresh
1036
+ */
1037
+ refresh: () => Promise<void>;
1038
+ /**
1039
+ * Last refresh timestamp
1040
+ */
1041
+ lastRefresh: Date | null;
1042
+ }
1043
+ /**
1044
+ * Hook for automatic token refresh
1045
+ *
1046
+ * @remarks
1047
+ * Automatically refreshes access tokens before they expire to prevent
1048
+ * session interruptions. Runs in the background without user interaction.
1049
+ *
1050
+ * @example
1051
+ * ```tsx
1052
+ * // Basic usage - automatic refresh
1053
+ * useTokenRefresh();
1054
+ *
1055
+ * // With options
1056
+ * const { expiresIn, isExpiringSoon } = useTokenRefresh({
1057
+ * refreshBeforeExpiry: 600, // 10 minutes
1058
+ * scopes: ['User.Read', 'Mail.Read'],
1059
+ * onRefresh: (expiresIn) => console.log(`Token refreshed, expires in ${expiresIn}s`),
1060
+ * });
1061
+ *
1062
+ * // Show warning when expiring soon
1063
+ * if (isExpiringSoon) {
1064
+ * return <div>Your session will expire soon</div>;
1065
+ * }
1066
+ * ```
1067
+ */
1068
+ declare function useTokenRefresh(options?: UseTokenRefreshOptions): UseTokenRefreshReturn;
1069
+
951
1070
  declare function createMsalConfig(config: MsalAuthConfig): Configuration;
952
1071
 
953
1072
  interface WithAuthOptions extends Omit<AuthGuardProps, 'children'> {
@@ -1429,4 +1548,4 @@ interface ServerSession {
1429
1548
  accessToken?: string;
1430
1549
  }
1431
1550
 
1432
- export { AuthGuard, type AuthGuardProps, type AuthMiddlewareConfig, type AuthProtectionConfig, AuthStatus, type AuthStatusProps, type CustomTokenClaims, type DebugLoggerConfig, ErrorBoundary, type ErrorBoundaryProps, type GraphApiOptions, MSALProvider, MicrosoftSignInButton, type MicrosoftSignInButtonProps, type MsalAuthConfig, MsalAuthProvider, type MsalAuthProviderProps, MsalError, type PageAuthConfig, ProtectedPage, type RetryConfig, type ServerSession, SignOutButton, type SignOutButtonProps, type UseGraphApiReturn, type UseMsalAuthReturn, type UseRolesReturn, type UseUserProfileReturn, UserAvatar, type UserAvatarProps, type UserProfile, type ValidatedAccountData, type ValidationError, type ValidationResult, type ValidationWarning, type WithAuthOptions, createAuthMiddleware, createMissingEnvVarError, createMsalConfig, createRetryWrapper, createScopedLogger, displayValidationResults, getDebugLogger, getMsalInstance, isValidAccountData, isValidRedirectUri, isValidScope, retryWithBackoff, safeJsonParse, sanitizeError, useGraphApi, useMsalAuth, useRoles, useUserProfile, validateConfig, validateScopes, withAuth, withPageAuth, wrapMsalError };
1551
+ export { AuthGuard, type AuthGuardProps, type AuthMiddlewareConfig, type AuthProtectionConfig, AuthStatus, type AuthStatusProps, type CustomTokenClaims, type DebugLoggerConfig, ErrorBoundary, type ErrorBoundaryProps, type GraphApiOptions, MSALProvider, MicrosoftSignInButton, type MicrosoftSignInButtonProps, type MsalAuthConfig, MsalAuthProvider, type MsalAuthProviderProps, MsalError, type PageAuthConfig, ProtectedPage, type RetryConfig, type ServerSession, SignOutButton, type SignOutButtonProps, type UseGraphApiReturn, type UseMsalAuthReturn, type UseRolesReturn, type UseTokenRefreshOptions, type UseTokenRefreshReturn, type UseUserProfileReturn, UserAvatar, type UserAvatarProps, type UserProfile, type ValidatedAccountData, type ValidationError, type ValidationResult, type ValidationWarning, type WithAuthOptions, createAuthMiddleware, createMissingEnvVarError, createMsalConfig, createRetryWrapper, createScopedLogger, displayValidationResults, getDebugLogger, getMsalInstance, isValidAccountData, isValidRedirectUri, isValidScope, retryWithBackoff, safeJsonParse, sanitizeError, useGraphApi, useMsalAuth, useRoles, useTokenRefresh, useUserProfile, validateConfig, validateScopes, withAuth, withPageAuth, wrapMsalError };
package/dist/index.d.ts CHANGED
@@ -264,6 +264,41 @@ interface MsalAuthConfig {
264
264
  * ```
265
265
  */
266
266
  onInitialized?: (instance: IPublicClientApplication) => void;
267
+ /**
268
+ * Enable automatic token refresh
269
+ *
270
+ * @remarks
271
+ * Automatically refreshes access tokens before they expire to prevent
272
+ * session interruptions. Tokens are refreshed silently in the background.
273
+ *
274
+ * @defaultValue false
275
+ *
276
+ * @example
277
+ * ```tsx
278
+ * <MSALProvider
279
+ * clientId="..."
280
+ * autoRefreshToken={true}
281
+ * refreshBeforeExpiry={300} // Refresh 5 min before expiry
282
+ * >
283
+ * {children}
284
+ * </MSALProvider>
285
+ * ```
286
+ */
287
+ autoRefreshToken?: boolean;
288
+ /**
289
+ * Refresh token this many seconds before expiry
290
+ *
291
+ * @remarks
292
+ * Only used when autoRefreshToken is enabled.
293
+ *
294
+ * @defaultValue 300 (5 minutes)
295
+ *
296
+ * @example
297
+ * ```tsx
298
+ * refreshBeforeExpiry={600} // Refresh 10 minutes before expiry
299
+ * ```
300
+ */
301
+ refreshBeforeExpiry?: number;
267
302
  }
268
303
  /**
269
304
  * Props for MsalAuthProvider component
@@ -283,7 +318,7 @@ interface MsalAuthProviderProps extends MsalAuthConfig {
283
318
  * @returns The MSAL instance or null if not initialized
284
319
  */
285
320
  declare function getMsalInstance(): PublicClientApplication | null;
286
- declare function MsalAuthProvider({ children, loadingComponent, onInitialized, ...config }: MsalAuthProviderProps): react_jsx_runtime.JSX.Element;
321
+ declare function MsalAuthProvider({ children, loadingComponent, onInitialized, autoRefreshToken, refreshBeforeExpiry, ...config }: MsalAuthProviderProps): react_jsx_runtime.JSX.Element;
287
322
 
288
323
  /**
289
324
  * Zero-Config Protected Routes - Type Definitions
@@ -393,12 +428,15 @@ interface MSALProviderProps extends MsalAuthProviderProps {
393
428
  }
394
429
  /**
395
430
  * Pre-configured MSALProvider component for Next.js App Router layouts.
396
- * This component is already marked as 'use client', so you can use it directly
397
- * in your server-side layout.tsx without needing to create a separate client component.
431
+ *
432
+ * @remarks
433
+ * This component is already marked as 'use client' internally, so you can import
434
+ * and use it directly in your server-side layout.tsx without adding 'use client'
435
+ * to your layout file.
398
436
  *
399
437
  * @example
400
438
  * ```tsx
401
- * // app/layout.tsx
439
+ * // app/layout.tsx (Server Component - no 'use client' needed!)
402
440
  * import { MSALProvider } from '@chemmangat/msal-next'
403
441
  *
404
442
  * export default function RootLayout({ children }) {
@@ -416,6 +454,13 @@ interface MSALProviderProps extends MsalAuthProviderProps {
416
454
  * )
417
455
  * }
418
456
  * ```
457
+ *
458
+ * @security
459
+ * - All authentication happens client-side (browser)
460
+ * - Tokens are never sent to your Next.js server
461
+ * - Uses Microsoft's official MSAL library
462
+ * - Supports secure token storage (sessionStorage/localStorage)
463
+ * - No server-side token handling required
419
464
  */
420
465
  declare function MSALProvider({ children, protection, ...props }: MSALProviderProps): react_jsx_runtime.JSX.Element;
421
466
 
@@ -948,6 +993,80 @@ interface UseRolesReturn {
948
993
  */
949
994
  declare function useRoles(): UseRolesReturn;
950
995
 
996
+ /**
997
+ * Automatic token refresh hook
998
+ * Refreshes tokens before they expire to prevent session interruptions
999
+ */
1000
+ interface UseTokenRefreshOptions {
1001
+ /**
1002
+ * Enable automatic token refresh
1003
+ * @default true
1004
+ */
1005
+ enabled?: boolean;
1006
+ /**
1007
+ * Refresh token this many seconds before expiry
1008
+ * @default 300 (5 minutes)
1009
+ */
1010
+ refreshBeforeExpiry?: number;
1011
+ /**
1012
+ * Scopes to refresh
1013
+ * @default ['User.Read']
1014
+ */
1015
+ scopes?: string[];
1016
+ /**
1017
+ * Callback when token is refreshed
1018
+ */
1019
+ onRefresh?: (expiresIn: number) => void;
1020
+ /**
1021
+ * Callback when refresh fails
1022
+ */
1023
+ onError?: (error: Error) => void;
1024
+ }
1025
+ interface UseTokenRefreshReturn {
1026
+ /**
1027
+ * Seconds until token expires
1028
+ */
1029
+ expiresIn: number | null;
1030
+ /**
1031
+ * Whether token is expiring soon
1032
+ */
1033
+ isExpiringSoon: boolean;
1034
+ /**
1035
+ * Manually trigger token refresh
1036
+ */
1037
+ refresh: () => Promise<void>;
1038
+ /**
1039
+ * Last refresh timestamp
1040
+ */
1041
+ lastRefresh: Date | null;
1042
+ }
1043
+ /**
1044
+ * Hook for automatic token refresh
1045
+ *
1046
+ * @remarks
1047
+ * Automatically refreshes access tokens before they expire to prevent
1048
+ * session interruptions. Runs in the background without user interaction.
1049
+ *
1050
+ * @example
1051
+ * ```tsx
1052
+ * // Basic usage - automatic refresh
1053
+ * useTokenRefresh();
1054
+ *
1055
+ * // With options
1056
+ * const { expiresIn, isExpiringSoon } = useTokenRefresh({
1057
+ * refreshBeforeExpiry: 600, // 10 minutes
1058
+ * scopes: ['User.Read', 'Mail.Read'],
1059
+ * onRefresh: (expiresIn) => console.log(`Token refreshed, expires in ${expiresIn}s`),
1060
+ * });
1061
+ *
1062
+ * // Show warning when expiring soon
1063
+ * if (isExpiringSoon) {
1064
+ * return <div>Your session will expire soon</div>;
1065
+ * }
1066
+ * ```
1067
+ */
1068
+ declare function useTokenRefresh(options?: UseTokenRefreshOptions): UseTokenRefreshReturn;
1069
+
951
1070
  declare function createMsalConfig(config: MsalAuthConfig): Configuration;
952
1071
 
953
1072
  interface WithAuthOptions extends Omit<AuthGuardProps, 'children'> {
@@ -1429,4 +1548,4 @@ interface ServerSession {
1429
1548
  accessToken?: string;
1430
1549
  }
1431
1550
 
1432
- export { AuthGuard, type AuthGuardProps, type AuthMiddlewareConfig, type AuthProtectionConfig, AuthStatus, type AuthStatusProps, type CustomTokenClaims, type DebugLoggerConfig, ErrorBoundary, type ErrorBoundaryProps, type GraphApiOptions, MSALProvider, MicrosoftSignInButton, type MicrosoftSignInButtonProps, type MsalAuthConfig, MsalAuthProvider, type MsalAuthProviderProps, MsalError, type PageAuthConfig, ProtectedPage, type RetryConfig, type ServerSession, SignOutButton, type SignOutButtonProps, type UseGraphApiReturn, type UseMsalAuthReturn, type UseRolesReturn, type UseUserProfileReturn, UserAvatar, type UserAvatarProps, type UserProfile, type ValidatedAccountData, type ValidationError, type ValidationResult, type ValidationWarning, type WithAuthOptions, createAuthMiddleware, createMissingEnvVarError, createMsalConfig, createRetryWrapper, createScopedLogger, displayValidationResults, getDebugLogger, getMsalInstance, isValidAccountData, isValidRedirectUri, isValidScope, retryWithBackoff, safeJsonParse, sanitizeError, useGraphApi, useMsalAuth, useRoles, useUserProfile, validateConfig, validateScopes, withAuth, withPageAuth, wrapMsalError };
1551
+ export { AuthGuard, type AuthGuardProps, type AuthMiddlewareConfig, type AuthProtectionConfig, AuthStatus, type AuthStatusProps, type CustomTokenClaims, type DebugLoggerConfig, ErrorBoundary, type ErrorBoundaryProps, type GraphApiOptions, MSALProvider, MicrosoftSignInButton, type MicrosoftSignInButtonProps, type MsalAuthConfig, MsalAuthProvider, type MsalAuthProviderProps, MsalError, type PageAuthConfig, ProtectedPage, type RetryConfig, type ServerSession, SignOutButton, type SignOutButtonProps, type UseGraphApiReturn, type UseMsalAuthReturn, type UseRolesReturn, type UseTokenRefreshOptions, type UseTokenRefreshReturn, type UseUserProfileReturn, UserAvatar, type UserAvatarProps, type UserProfile, type ValidatedAccountData, type ValidationError, type ValidationResult, type ValidationWarning, type WithAuthOptions, createAuthMiddleware, createMissingEnvVarError, createMsalConfig, createRetryWrapper, createScopedLogger, displayValidationResults, getDebugLogger, getMsalInstance, isValidAccountData, isValidRedirectUri, isValidScope, retryWithBackoff, safeJsonParse, sanitizeError, useGraphApi, useMsalAuth, useRoles, useTokenRefresh, useUserProfile, validateConfig, validateScopes, withAuth, withPageAuth, wrapMsalError };