@djangocfg/api 2.1.190 → 2.1.192

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@djangocfg/api",
3
- "version": "2.1.190",
3
+ "version": "2.1.192",
4
4
  "description": "Auto-generated TypeScript API client with React hooks, SWR integration, and Zod validation for Django REST Framework backends",
5
5
  "keywords": [
6
6
  "django",
@@ -84,7 +84,7 @@
84
84
  "devDependencies": {
85
85
  "@types/node": "^24.7.2",
86
86
  "@types/react": "^19.0.0",
87
- "@djangocfg/typescript-config": "^2.1.190",
87
+ "@djangocfg/typescript-config": "^2.1.192",
88
88
  "next": "^16.0.0",
89
89
  "react": "^19.0.0",
90
90
  "tsup": "^8.5.0",
@@ -5,6 +5,7 @@ import { usePathname } from 'next/navigation';
5
5
  import React, {
6
6
  createContext, ReactNode, useCallback, useContext, useEffect, useMemo, useRef, useState
7
7
  } from 'react';
8
+ import { SWRConfig } from 'swr';
8
9
 
9
10
  import { useCfgRouter, useLocalStorage, useQueryParams } from '../hooks';
10
11
 
@@ -131,6 +132,25 @@ const AuthProviderInternal: React.FC<AuthProviderProps> = ({ children, config })
131
132
  return false;
132
133
  }, [clearAuthState]);
133
134
 
135
+ // SWR onError: auto-logout on 401 from any SWR hook
136
+ const isAutoLoggingOutRef = useRef(false);
137
+ const swrOnError = useCallback((error: any) => {
138
+ const isAuthError = error?.status === 401 ||
139
+ error?.statusCode === 401 ||
140
+ error?.code === 'token_not_valid' ||
141
+ error?.code === 'authentication_failed';
142
+
143
+ if (isAuthError && !isAutoLoggingOutRef.current) {
144
+ isAutoLoggingOutRef.current = true;
145
+ authLogger.warn('SWR 401 error detected, auto-logout');
146
+ clearAuthState('swrOnError:401');
147
+
148
+ const authCallbackUrl = configRef.current?.routes?.defaultAuthCallback || defaultRoutes.defaultAuthCallback;
149
+ router.hardReplace(authCallbackUrl);
150
+ }
151
+ }, [clearAuthState, router]);
152
+ const swrConfig = useMemo(() => ({ onError: swrOnError }), [swrOnError]);
153
+
134
154
  // Simple profile loading without retry - now uses AccountsContext with memoization
135
155
  const loadCurrentProfile = useCallback(async (callerId?: string): Promise<void> => {
136
156
  const finalCallerId = callerId || 'AuthContext.loadCurrentProfile';
@@ -602,7 +622,11 @@ const AuthProviderInternal: React.FC<AuthProviderProps> = ({ children, config })
602
622
  ],
603
623
  );
604
624
 
605
- return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
625
+ return (
626
+ <AuthContext.Provider value={value}>
627
+ <SWRConfig value={swrConfig}>{children}</SWRConfig>
628
+ </AuthContext.Provider>
629
+ );
606
630
  };
607
631
 
608
632
  // Wrapper that provides AccountsContext