@djangocfg/api 2.1.604 → 2.1.605
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/auth.cjs +32 -0
- package/dist/auth.cjs.map +1 -1
- package/dist/auth.mjs +32 -0
- package/dist/auth.mjs.map +1 -1
- package/package.json +2 -2
- package/src/auth/__tests__/legacyStorage.test.ts +21 -0
- package/src/auth/context/AccountsContext.tsx +6 -0
- package/src/auth/context/AuthContext.tsx +7 -0
- package/src/auth/hooks/legacyStorage.ts +32 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@djangocfg/api",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.605",
|
|
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",
|
|
@@ -91,7 +91,7 @@
|
|
|
91
91
|
"zod": "^4.3.6"
|
|
92
92
|
},
|
|
93
93
|
"devDependencies": {
|
|
94
|
-
"@djangocfg/typescript-config": "^2.1.
|
|
94
|
+
"@djangocfg/typescript-config": "^2.1.605",
|
|
95
95
|
"@testing-library/react": "^16.3.2",
|
|
96
96
|
"@types/node": "^25.9.5",
|
|
97
97
|
"@types/react": "19.2.15",
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { beforeEach, describe, expect, it } from 'vitest';
|
|
3
|
+
|
|
4
|
+
import { purgeLegacyTokenStorage } from '../hooks/legacyStorage';
|
|
5
|
+
|
|
6
|
+
describe('purgeLegacyTokenStorage', () => {
|
|
7
|
+
beforeEach(() => localStorage.clear());
|
|
8
|
+
|
|
9
|
+
it('removes the pre-cookie token entries and keeps everything else', () => {
|
|
10
|
+
localStorage.setItem('cfg.auth.access_token', 'a');
|
|
11
|
+
localStorage.setItem('cfg.auth.refresh_token', 'r');
|
|
12
|
+
localStorage.setItem('cfg.ui.sidebar_width', '240');
|
|
13
|
+
expect(purgeLegacyTokenStorage().sort()).toEqual(['cfg.auth.access_token', 'cfg.auth.refresh_token']);
|
|
14
|
+
expect(localStorage.getItem('cfg.ui.sidebar_width')).toBe('240');
|
|
15
|
+
expect(localStorage.getItem('cfg.auth.access_token')).toBeNull();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('does nothing when there is nothing to remove', () => {
|
|
19
|
+
expect(purgeLegacyTokenStorage()).toEqual([]);
|
|
20
|
+
});
|
|
21
|
+
});
|
|
@@ -91,6 +91,12 @@ interface AccountsProviderProps {
|
|
|
91
91
|
|
|
92
92
|
export function AccountsProvider({ children }: AccountsProviderProps) {
|
|
93
93
|
const [profile, setProfile] = useState<User | undefined>(() => {
|
|
94
|
+
// A cached profile is only a head start for a live session; with no session (tokens gone after the move to cookies,
|
|
95
|
+
// or signed out elsewhere) it would render the private UI with nothing behind it.
|
|
96
|
+
if (!auth.isAuthenticated()) {
|
|
97
|
+
clearProfileCache();
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
94
100
|
const cached = getCachedProfile() as User | null;
|
|
95
101
|
return cached || undefined;
|
|
96
102
|
});
|
|
@@ -34,6 +34,7 @@ import { CFG_KEYS } from '../../_api/generated/helpers/keys';
|
|
|
34
34
|
import { applyRoleLogPolicy } from '../../log-control';
|
|
35
35
|
import { useCfgRouter, useLocalStorage, useQueryParams, useSession } from '../hooks';
|
|
36
36
|
import { clearProfileCache } from '../hooks/useProfileCache';
|
|
37
|
+
import { purgeLegacyTokenStorage } from '../hooks/legacyStorage';
|
|
37
38
|
import { useAuthRedirectManager } from '../hooks/useAuthRedirect';
|
|
38
39
|
import { Analytics, AnalyticsCategory, AnalyticsEvent } from '../utils/analytics';
|
|
39
40
|
import { formatAuthError, logAuthFailure } from '../utils/errors';
|
|
@@ -144,6 +145,12 @@ const AuthProviderInternal: React.FC<AuthProviderProps> = ({ children, config })
|
|
|
144
145
|
authLogger.warn('Dead session in storage — clearing');
|
|
145
146
|
auth.clearSession();
|
|
146
147
|
clearProfileCache();
|
|
148
|
+
} else if (!hasTokens && !auth.isAuthenticated()) {
|
|
149
|
+
// No session at all: a leftover profile cache and the old localStorage tokens (pre-cookie auth) must not keep
|
|
150
|
+
// the private UI alive. Drop them; the page's own guard then sends the user to /auth.
|
|
151
|
+
clearProfileCache();
|
|
152
|
+
purgeLegacyTokenStorage();
|
|
153
|
+
if (accountsRef.current.profile) accountsRef.current.logout();
|
|
147
154
|
}
|
|
148
155
|
setProfileSettled(true);
|
|
149
156
|
return;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One-time cleanup of what the pre-cookie auth left in the browser.
|
|
3
|
+
*
|
|
4
|
+
* Before 2.1.604 the session tokens lived in `localStorage`. They are cookies now (shared across subdomains), so the old
|
|
5
|
+
* entries are never read again; left alone they are harmless but misleading, and the 4 h profile cache next to them used
|
|
6
|
+
* to keep the UI looking signed in without any token (empty private pages). Cookies are untouched.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const LEGACY_TOKEN_KEYS = [
|
|
10
|
+
'cfg.auth.access_token',
|
|
11
|
+
'cfg.auth.refresh_token',
|
|
12
|
+
'auth_token',
|
|
13
|
+
'refresh_token',
|
|
14
|
+
'access_token',
|
|
15
|
+
] as const;
|
|
16
|
+
|
|
17
|
+
/** Removes the old `localStorage` token entries. Safe on the server and with blocked storage. */
|
|
18
|
+
export function purgeLegacyTokenStorage(): string[] {
|
|
19
|
+
const removed: string[] = [];
|
|
20
|
+
try {
|
|
21
|
+
if (typeof window === 'undefined') return removed;
|
|
22
|
+
for (const key of LEGACY_TOKEN_KEYS) {
|
|
23
|
+
if (window.localStorage.getItem(key) !== null) {
|
|
24
|
+
window.localStorage.removeItem(key);
|
|
25
|
+
removed.push(key);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
} catch {
|
|
29
|
+
/* storage blocked: nothing to purge */
|
|
30
|
+
}
|
|
31
|
+
return removed;
|
|
32
|
+
}
|