@djangocfg/api 2.1.547 → 2.1.549
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/README.md +25 -0
- package/dist/auth.cjs +17 -4
- package/dist/auth.cjs.map +1 -1
- package/dist/auth.d.cts +17 -2
- package/dist/auth.d.ts +17 -2
- package/dist/auth.mjs +17 -4
- package/dist/auth.mjs.map +1 -1
- package/package.json +2 -2
- package/src/auth/__tests__/guard.test.ts +65 -0
- package/src/auth/context/AccountsContext.tsx +17 -4
- package/src/auth/utils/guard.ts +27 -1
- package/src/auth/utils/index.ts +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@djangocfg/api",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.549",
|
|
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",
|
|
@@ -85,7 +85,7 @@
|
|
|
85
85
|
"zod": "^4.3.6"
|
|
86
86
|
},
|
|
87
87
|
"devDependencies": {
|
|
88
|
-
"@djangocfg/typescript-config": "^2.1.
|
|
88
|
+
"@djangocfg/typescript-config": "^2.1.549",
|
|
89
89
|
"@testing-library/react": "^16.3.2",
|
|
90
90
|
"@types/node": "^25.9.5",
|
|
91
91
|
"@types/react": "19.2.15",
|
|
@@ -15,6 +15,7 @@ import {
|
|
|
15
15
|
resolveGuardIsLoading,
|
|
16
16
|
resolveGuardIsAuthenticated,
|
|
17
17
|
shouldRedirectToAuth,
|
|
18
|
+
isForbidden,
|
|
18
19
|
type GuardInput,
|
|
19
20
|
} from '../utils/guard';
|
|
20
21
|
|
|
@@ -101,6 +102,70 @@ describe('safety-net timeout (effectiveAuthLoading = authLoading && !timedOut)',
|
|
|
101
102
|
});
|
|
102
103
|
});
|
|
103
104
|
|
|
105
|
+
/**
|
|
106
|
+
* Staff gate. A signed-in non-staff user reaching a staff-only shell is NOT an
|
|
107
|
+
* auth failure: signing in again cannot fix it. The guard must resolve to a
|
|
108
|
+
* distinct "forbidden" state — never a redirect (which loops, since the guard
|
|
109
|
+
* saves the current URL first) and never a spinner (the original hang bug).
|
|
110
|
+
*/
|
|
111
|
+
describe('isForbidden (staff gate)', () => {
|
|
112
|
+
const staff = (o: Partial<GuardInput>): GuardInput =>
|
|
113
|
+
s({ requireStaff: true, isAdminUser: false, ...o });
|
|
114
|
+
|
|
115
|
+
it('signed-in non-staff user on a staff route is forbidden', () => {
|
|
116
|
+
expect(isForbidden(staff({}))).toBe(true);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('staff user passes the gate', () => {
|
|
120
|
+
expect(isForbidden(staff({ isAdminUser: true }))).toBe(false);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('a superuser who is not is_staff passes (isAdminUser folds both flags)', () => {
|
|
124
|
+
// useAuth() computes isAdminUser as is_staff || is_superuser, so the guard
|
|
125
|
+
// only ever sees the fold. Checking is_staff alone here would hide the
|
|
126
|
+
// surface from the people most likely to need it.
|
|
127
|
+
expect(isForbidden(staff({ isAdminUser: true }))).toBe(false);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it('is not forbidden while auth is still resolving (role unknown yet)', () => {
|
|
131
|
+
expect(isForbidden(staff({ authLoading: true }))).toBe(false);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it('is not forbidden before mount (SSR never knows the role)', () => {
|
|
135
|
+
expect(isForbidden(staff({ mounted: false }))).toBe(false);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('unauthenticated is a redirect, not a forbidden wall', () => {
|
|
139
|
+
expect(isForbidden(staff({ isAuthenticated: false }))).toBe(false);
|
|
140
|
+
expect(shouldRedirectToAuth(staff({ isAuthenticated: false }))).toBe(true);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it('routes that do not require staff are never forbidden', () => {
|
|
144
|
+
expect(isForbidden(s({ requireStaff: false, isAdminUser: false }))).toBe(false);
|
|
145
|
+
expect(isForbidden(base)).toBe(false); // requireStaff undefined
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it('a public route is never forbidden', () => {
|
|
149
|
+
expect(isForbidden(staff({ requireAuth: false }))).toBe(false);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it('REGRESSION: a forbidden user stops loading (renders the wall, never spins)', () => {
|
|
153
|
+
expect(resolveGuardIsLoading(staff({}))).toBe(false);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it('REGRESSION: a forbidden user never triggers a redirect (no login loop)', () => {
|
|
157
|
+
expect(shouldRedirectToAuth(staff({}))).toBe(false);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it('a staff user on a staff route renders content', () => {
|
|
161
|
+
expect(resolveGuardIsLoading(staff({ isAdminUser: true }))).toBe(false);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it('still blocks while a staff route is resolving auth', () => {
|
|
165
|
+
expect(resolveGuardIsLoading(staff({ authLoading: true }))).toBe(true);
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
|
|
104
169
|
describe('resolveGuardIsAuthenticated', () => {
|
|
105
170
|
it('is true on a public route regardless of session', () => {
|
|
106
171
|
expect(resolveGuardIsAuthenticated({ requireAuth: false, isAuthenticated: false })).toBe(true);
|
|
@@ -101,6 +101,9 @@ export function AccountsProvider({ children }: AccountsProviderProps) {
|
|
|
101
101
|
|
|
102
102
|
const profileRef = useRef<User | undefined>(profile);
|
|
103
103
|
const isLoadingRef = useRef(false);
|
|
104
|
+
// Monotonic id per fetch. A forced re-read can now overlap the bootstrap
|
|
105
|
+
// one, and the slower response must not overwrite the newer profile.
|
|
106
|
+
const fetchSeqRef = useRef(0);
|
|
104
107
|
|
|
105
108
|
useEffect(() => {
|
|
106
109
|
profileRef.current = profile;
|
|
@@ -129,11 +132,16 @@ export function AccountsProvider({ children }: AccountsProviderProps) {
|
|
|
129
132
|
return undefined;
|
|
130
133
|
}
|
|
131
134
|
|
|
132
|
-
|
|
135
|
+
// `force` is how a write says "I just changed the server, re-read it".
|
|
136
|
+
// Honouring the in-flight guard there would hand the caller back the
|
|
137
|
+
// pre-write profile and never fetch again, so the UI keeps showing the
|
|
138
|
+
// old value indefinitely.
|
|
139
|
+
if (isLoadingRef.current && !force) {
|
|
133
140
|
authLogger.debug(`Profile loading in progress, skipping (caller: ${callerId})`);
|
|
134
141
|
return profileRef.current;
|
|
135
142
|
}
|
|
136
143
|
|
|
144
|
+
const seq = ++fetchSeqRef.current;
|
|
137
145
|
setIsLoadingProfile(true);
|
|
138
146
|
isLoadingRef.current = true;
|
|
139
147
|
setProfileError(null);
|
|
@@ -141,17 +149,22 @@ export function AccountsProvider({ children }: AccountsProviderProps) {
|
|
|
141
149
|
authLogger.debug(`Fetching profile from API (caller: ${callerId}, force: ${force})`);
|
|
142
150
|
const res = await CfgAccountsProfile.cfgAccountsProfileRetrieve({ throwOnError: true });
|
|
143
151
|
const result = res.data;
|
|
152
|
+
// A newer fetch already landed — its profile is the current one, and
|
|
153
|
+
// this older response would roll it back.
|
|
154
|
+
if (seq !== fetchSeqRef.current) return result;
|
|
144
155
|
setProfile(result);
|
|
145
156
|
profileRef.current = result;
|
|
146
157
|
setCachedProfile(result);
|
|
147
158
|
return result;
|
|
148
159
|
} catch (error) {
|
|
149
160
|
const err = error instanceof Error ? error : new Error('Failed to fetch profile');
|
|
150
|
-
setProfileError(err);
|
|
161
|
+
if (seq === fetchSeqRef.current) setProfileError(err);
|
|
151
162
|
throw err;
|
|
152
163
|
} finally {
|
|
153
|
-
|
|
154
|
-
|
|
164
|
+
if (seq === fetchSeqRef.current) {
|
|
165
|
+
setIsLoadingProfile(false);
|
|
166
|
+
isLoadingRef.current = false;
|
|
167
|
+
}
|
|
155
168
|
}
|
|
156
169
|
}, []);
|
|
157
170
|
|
package/src/auth/utils/guard.ts
CHANGED
|
@@ -9,7 +9,9 @@
|
|
|
9
9
|
*
|
|
10
10
|
* Invariant that fixes the bug: once auth is resolved (`authLoading === false`)
|
|
11
11
|
* and the user is unauthenticated, `isLoading` must become false so the guard
|
|
12
|
-
* redirects to the form instead of spinning forever.
|
|
12
|
+
* redirects to the form instead of spinning forever. The same invariant covers
|
|
13
|
+
* the forbidden case: a signed-in user without the required privilege must stop
|
|
14
|
+
* loading too, or the preloader spins on a state no redirect will ever clear.
|
|
13
15
|
*/
|
|
14
16
|
|
|
15
17
|
export interface GuardInput {
|
|
@@ -23,6 +25,29 @@ export interface GuardInput {
|
|
|
23
25
|
isRedirecting: boolean;
|
|
24
26
|
/** User currently has a (valid-looking) session. */
|
|
25
27
|
isAuthenticated: boolean;
|
|
28
|
+
/** Route requires staff/admin privileges, not just a session. */
|
|
29
|
+
requireStaff?: boolean;
|
|
30
|
+
/** `is_staff || is_superuser`, from `useAuth().isAdminUser`. */
|
|
31
|
+
isAdminUser?: boolean;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* True when the session is valid but lacks the required privilege.
|
|
36
|
+
*
|
|
37
|
+
* Distinct from unauthenticated: the fix is a different account, not signing
|
|
38
|
+
* in, so this must never redirect to the auth form. Redirecting would also
|
|
39
|
+
* loop — the guard saves the current URL first, so a successful re-login on
|
|
40
|
+
* the same account returns straight to the wall.
|
|
41
|
+
*/
|
|
42
|
+
export function isForbidden(s: GuardInput): boolean {
|
|
43
|
+
return Boolean(
|
|
44
|
+
s.mounted &&
|
|
45
|
+
s.requireAuth &&
|
|
46
|
+
s.requireStaff &&
|
|
47
|
+
!s.authLoading &&
|
|
48
|
+
s.isAuthenticated &&
|
|
49
|
+
!s.isAdminUser
|
|
50
|
+
);
|
|
26
51
|
}
|
|
27
52
|
|
|
28
53
|
/**
|
|
@@ -32,6 +57,7 @@ export interface GuardInput {
|
|
|
32
57
|
export function resolveGuardIsLoading(s: GuardInput): boolean {
|
|
33
58
|
if (!s.mounted) return true; // pre-hydration: always preloader
|
|
34
59
|
if (!s.requireAuth) return false; // public route: never blocks
|
|
60
|
+
if (isForbidden(s)) return false; // resolved: render the wall, don't spin
|
|
35
61
|
return s.authLoading || s.isRedirecting || !s.isAuthenticated;
|
|
36
62
|
}
|
|
37
63
|
|