@aiquants/auth-react-router 0.41.0 → 0.41.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.
Files changed (2) hide show
  1. package/README.md +32 -11
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -71,16 +71,18 @@ createAuthServer({
71
71
  ```
72
72
 
73
73
  A page loader hands its guard the page's path and query, so an unauthenticated reader is sent to the sign-in page that
74
- returns here (an action passes `{ failureRedirect: null }` instead and answers 401 data):
74
+ returns here (an action passes `{ failureRedirect: null }` instead and answers 401 data). Build the return-to from the
75
+ loader's `url` argument, React Router's normalized address of the page, never from `request.url`: a client-side
76
+ navigation runs the loader through a single-fetch data request, whose `request.url` ends in `.data` and carries
77
+ `_routes`, and a return-to built from it sends the reader back to that data address after the sign-in:
75
78
 
76
79
  ```ts
77
- export const loader = async ({ request }: LoaderFunctionArgs) => {
78
- const url = new URL(request.url)
79
- const returnTo = url.pathname + url.search
80
+ export const loader = async ({ request, url }: LoaderFunctionArgs) => {
81
+ const returnTo = url.pathname + url.search // the page's address; a data request's request.url ends in .data
80
82
  const { user, cookie } = await authServer.authenticateInLoader(request, { failureRedirect: "/auth/login", returnTo }) // failureRedirect must be loginPath; GET and HEAD only
81
83
  ...
82
84
  }
83
- // requireUser(request, returnTo) and requireAdminUser(request, returnTo) take the same return-to
85
+ // requireUser(request, returnTo) and requireAdminUser(request, returnTo) take the same return-to, built from the loader's url
84
86
  ```
85
87
 
86
88
  ```ts
@@ -179,6 +181,7 @@ assistive technology, so the button's name is its label.
179
181
  A splat-mounted admin UI for users, groups, group members, and the sign-in allowlist — the identity-side counterpart to `@aiquants/authz-react-router`.
180
182
 
181
183
  ```ts
184
+ import { accessFailureResponse } from "@aiquants/auth-core"
182
185
  import { createUserAdminApp, jaUserAdminLabels } from "@aiquants/auth-react-router/admin"
183
186
 
184
187
  export const userAdminApp = createUserAdminApp({
@@ -186,9 +189,10 @@ export const userAdminApp = createUserAdminApp({
186
189
  fieldLimits: myFieldLimits, // REQUIRED — the widths of the columns your store writes; see "Field limits" below
187
190
  labels: jaUserAdminLabels, // optional; package default is English
188
191
  guards: { // REQUIRED — see below
192
+ // Serves the loader and the action alike and receives only the request: it checks without redirecting, and
193
+ // the route below has already sent an unauthenticated reader of the page to the sign-in page
189
194
  requireAccess: async (request, { action }) => {
190
- const url = new URL(request.url)
191
- await requireUser(request, url.pathname + url.search)
195
+ if ((await getSessionUser(request)) === undefined) throw accessFailureResponse("unauthenticated")
192
196
  await requirePermission(request, { resourceKey: "user_admin", action })
193
197
  },
194
198
  },
@@ -196,13 +200,29 @@ export const userAdminApp = createUserAdminApp({
196
200
  ```
197
201
 
198
202
  ```tsx
203
+ import { accessFailureBody } from "@aiquants/auth-core"
199
204
  import { AuthUserAdminAppView } from "@aiquants/auth-react-router/admin"
200
- export const loader = userAdminApp.loader
201
- export const action = userAdminApp.action
205
+ import { type ActionFunctionArgs, data, type LoaderFunctionArgs } from "react-router"
206
+
202
207
  // mounted as the splat route "/users/*"
208
+ export const loader = async (args: LoaderFunctionArgs) => {
209
+ // the page's address (args.url), so the sign-in returns here; a data request's request.url ends in .data
210
+ await authenticateInLoader(args.request, { failureRedirect: "/auth/login", returnTo: args.url.pathname + args.url.search })
211
+ return await userAdminApp.loader(args)
212
+ }
213
+ export const action = async (args: ActionFunctionArgs) => {
214
+ const { user, cookie } = await authenticateInLoader(args.request, { failureRedirect: null })
215
+ // a write is answered in place: the view draws the 401 body through the presenter (place write)
216
+ if (user === undefined) return data(accessFailureBody("unauthenticated"), { status: 401, headers: cookie === undefined ? undefined : { "Set-Cookie": cookie } })
217
+ return await userAdminApp.action(args)
218
+ }
203
219
  export default () => <AuthUserAdminAppView basePath="/users" accessFailure={accessFailurePresenter} />
204
220
  ```
205
221
 
222
+ The guard receives only the request, and a client-side navigation reaches the loader through a single-fetch data request
223
+ whose `request.url` ends in `.data`, so the guard cannot build the page's return-to; the route authenticates the page
224
+ first from the loader's `url`. The guard's own check is reached only by a session that ends between the two checks.
225
+
206
226
  - **`basePath` is required** (`AuthUserAdminAppView` / `UserAdminShell`): the absolute path the console is mounted at,
207
227
  without a trailing slash (`"/users"` for a `/users/*` route). The five tabs and the per-row "members" links of the
208
228
  groups tab are built from it. There is no default: the host decides where the console lives, and a built-in default
@@ -210,8 +230,9 @@ export default () => <AuthUserAdminAppView basePath="/users" accessFailure={acce
210
230
  passes, only the links point at a place that is not served.
211
231
  - **`accessFailure` is required** (`AuthUserAdminAppView` / `UserAdminShell`): the host's access-failure presenter
212
232
  (`AccessFailurePresenter` of `@aiquants/auth-core`), the same one its other screens use, so a denial reads the same
213
- everywhere. The shell's root (`ConsoleUiRoot`) holds it, and the views draw every denied write through it (place
214
- `write`, with `labels.errors.forbidden` as the denied sentence). `ConsoleUiRoot` throws `TypeError` when it is not a
233
+ everywhere. The shell's root (`ConsoleUiRoot`) holds it, and the views draw through it every write the action answers
234
+ with an access-failure body (place `write`): a denied one (403, with `labels.errors.forbidden` as the denied sentence)
235
+ and one from a reader who is not signed in (401). `ConsoleUiRoot` throws `TypeError` when it is not a
215
236
  function.
216
237
  - **Serve the view with `userAdminApp.loader`**: `AuthUserAdminAppView` draws the tab its loader's `segment` names
217
238
  (`users`, `groups`, `group_members`, `allowlist` or `directory`; the package's loader reads any other path below the
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiquants/auth-react-router",
3
- "version": "0.41.0",
3
+ "version": "0.41.1",
4
4
  "description": "React Router v7 / v8 auth adapter for @aiquants/auth-core: Google OAuth strategy factory (remix-auth), cookie session storage with DI test backdoor, a loader guard that authenticates at most once per request (token refresh + photo cache), auth route handlers, and a <GoogleForm> login button.",
5
5
  "sideEffects": [
6
6
  "**/*.css"
@@ -58,7 +58,7 @@
58
58
  "@aiquants/console-ui": "0.7.0",
59
59
  "@aiquants/fuzzy-search": "3.2.1",
60
60
  "@aiquants/select-box": "0.22.2",
61
- "@aiquants/virtualscroll": "3.24.0",
61
+ "@aiquants/virtualscroll": "3.26.0",
62
62
  "@tailwindcss/cli": "^4.1.18",
63
63
  "@testing-library/react": "^16.3.2",
64
64
  "@types/node": "^25.9.1",