@djangocfg/api 2.1.547 → 2.1.548

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.547",
3
+ "version": "2.1.548",
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.547",
88
+ "@djangocfg/typescript-config": "^2.1.548",
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);
@@ -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
 
@@ -4,6 +4,7 @@ export {
4
4
  resolveGuardIsLoading,
5
5
  shouldRedirectToAuth,
6
6
  resolveGuardIsAuthenticated,
7
+ isForbidden,
7
8
  type GuardInput,
8
9
  } from './guard';
9
10
  export { normalizePath, isAllowedAuthPath } from './path';