@passflow/react 0.25.3 → 0.26.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 (139) hide show
  1. package/CHANGELOG.md +51 -0
  2. package/README.md +177 -18
  3. package/dist/index.cjs +1 -1
  4. package/dist/index.cjs.map +1 -1
  5. package/dist/index.es.js +146 -143
  6. package/dist/index.es.js.map +1 -1
  7. package/dist/src/app.d.cts +5 -0
  8. package/dist/src/app.d.ts +5 -0
  9. package/dist/src/app.d.ts.map +1 -0
  10. package/dist/src/components/flow/index.d.cts +2 -0
  11. package/dist/src/components/flow/index.d.ts +2 -0
  12. package/dist/src/components/flow/index.d.ts.map +1 -1
  13. package/dist/src/components/flow/passflow/flow-router.d.cts +1 -0
  14. package/dist/src/components/flow/passflow/flow-router.d.ts +1 -0
  15. package/dist/src/components/flow/passflow/flow-router.d.ts.map +1 -1
  16. package/dist/src/components/flow/passflow/index.d.cts +1 -1
  17. package/dist/src/components/flow/passflow/index.d.ts +1 -1
  18. package/dist/src/components/flow/passflow/index.d.ts.map +1 -1
  19. package/dist/src/components/flow/two-factor-setup/index.d.cts +1 -2
  20. package/dist/src/components/flow/two-factor-setup/index.d.ts +1 -2
  21. package/dist/src/components/flow/two-factor-setup/index.d.ts.map +1 -1
  22. package/dist/src/components/flow/two-factor-verify/index.d.cts +1 -2
  23. package/dist/src/components/flow/two-factor-verify/index.d.ts +1 -2
  24. package/dist/src/components/flow/two-factor-verify/index.d.ts.map +1 -1
  25. package/dist/src/components/form/forgot-password/forgot-password.d.ts.map +1 -1
  26. package/dist/src/components/form/signin/index.d.ts.map +1 -1
  27. package/dist/src/components/form/signup/index.d.ts.map +1 -1
  28. package/dist/src/components/form/two-factor-challenge/index.d.ts.map +1 -1
  29. package/dist/src/components/form/two-factor-setup/setup-screen.d.cts +2 -0
  30. package/dist/src/components/form/two-factor-setup/setup-screen.d.ts +2 -0
  31. package/dist/src/components/form/two-factor-setup/setup-screen.d.ts.map +1 -1
  32. package/dist/src/components/form/two-factor-setup/two-factor-setup-form.d.cts +1 -1
  33. package/dist/src/components/form/two-factor-setup/two-factor-setup-form.d.ts +1 -1
  34. package/dist/src/components/form/two-factor-setup/two-factor-setup-form.d.ts.map +1 -1
  35. package/dist/src/components/form/two-factor-verify/two-factor-recovery-form.d.cts +2 -0
  36. package/dist/src/components/form/two-factor-verify/two-factor-recovery-form.d.ts +2 -0
  37. package/dist/src/components/form/two-factor-verify/two-factor-recovery-form.d.ts.map +1 -1
  38. package/dist/src/components/form/two-factor-verify/two-factor-verify-form.d.cts +2 -0
  39. package/dist/src/components/form/two-factor-verify/two-factor-verify-form.d.ts +2 -0
  40. package/dist/src/components/form/two-factor-verify/two-factor-verify-form.d.ts.map +1 -1
  41. package/dist/src/components/login-button/index.d.cts +54 -0
  42. package/dist/src/components/login-button/index.d.ts +54 -0
  43. package/dist/src/components/login-button/index.d.ts.map +1 -0
  44. package/dist/src/components/provider/passflow-provider.d.cts +4 -2
  45. package/dist/src/components/provider/passflow-provider.d.ts +4 -2
  46. package/dist/src/components/provider/passflow-provider.d.ts.map +1 -1
  47. package/dist/src/components/provider/upgrade-provider.d.cts +14 -1
  48. package/dist/src/components/provider/upgrade-provider.d.ts +14 -1
  49. package/dist/src/components/provider/upgrade-provider.d.ts.map +1 -1
  50. package/dist/src/components/ui/fields/field-password.d.ts.map +1 -1
  51. package/dist/src/components/ui/fields/field-phone.d.ts.map +1 -1
  52. package/dist/src/components/ui/index.d.cts +1 -0
  53. package/dist/src/components/ui/index.d.ts +1 -0
  54. package/dist/src/components/ui/index.d.ts.map +1 -1
  55. package/dist/src/components/ui/login-button/index.d.cts +2 -0
  56. package/dist/src/components/ui/login-button/index.d.ts +2 -0
  57. package/dist/src/components/ui/login-button/index.d.ts.map +1 -0
  58. package/dist/src/components/ui/login-button/login-button.d.cts +36 -0
  59. package/dist/src/components/ui/login-button/login-button.d.ts +36 -0
  60. package/dist/src/components/ui/login-button/login-button.d.ts.map +1 -0
  61. package/dist/src/components/ui/login-button/shadcn-theme.d.cts +23 -0
  62. package/dist/src/components/ui/login-button/shadcn-theme.d.ts +23 -0
  63. package/dist/src/components/ui/login-button/shadcn-theme.d.ts.map +1 -0
  64. package/dist/src/components/ui/login-button/use-shadcn-theme.d.cts +9 -0
  65. package/dist/src/components/ui/login-button/use-shadcn-theme.d.ts +9 -0
  66. package/dist/src/components/ui/login-button/use-shadcn-theme.d.ts.map +1 -0
  67. package/dist/src/components/ui/login-screen/credentials.d.ts.map +1 -1
  68. package/dist/src/components/ui/login-screen/shared.d.ts.map +1 -1
  69. package/dist/src/components/ui/login-screen/two-factor-code.d.cts +2 -1
  70. package/dist/src/components/ui/login-screen/two-factor-code.d.ts +2 -1
  71. package/dist/src/components/ui/login-screen/two-factor-code.d.ts.map +1 -1
  72. package/dist/src/components/ui/login-screen/types.d.cts +2 -1
  73. package/dist/src/components/ui/login-screen/types.d.ts +2 -1
  74. package/dist/src/components/ui/login-screen/types.d.ts.map +1 -1
  75. package/dist/src/components/ui/upgrade-dialog/state-mapping.d.cts +9 -3
  76. package/dist/src/components/ui/upgrade-dialog/state-mapping.d.ts +9 -3
  77. package/dist/src/components/ui/upgrade-dialog/state-mapping.d.ts.map +1 -1
  78. package/dist/src/components/ui/upgrade-dialog/status.d.ts.map +1 -1
  79. package/dist/src/components/ui/upgrade-dialog/types.d.cts +6 -2
  80. package/dist/src/components/ui/upgrade-dialog/types.d.ts +6 -2
  81. package/dist/src/components/ui/upgrade-dialog/types.d.ts.map +1 -1
  82. package/dist/src/context/auth-context.d.cts +8 -2
  83. package/dist/src/context/auth-context.d.ts +8 -2
  84. package/dist/src/context/auth-context.d.ts.map +1 -1
  85. package/dist/src/context/upgrade-context.d.cts +1 -1
  86. package/dist/src/context/upgrade-context.d.ts +1 -1
  87. package/dist/src/context/upgrade-context.d.ts.map +1 -1
  88. package/dist/src/dev-account.d.cts +2 -0
  89. package/dist/src/dev-account.d.ts +2 -0
  90. package/dist/src/dev-account.d.ts.map +1 -0
  91. package/dist/src/dev-in-app-sign-in.d.cts +2 -0
  92. package/dist/src/dev-in-app-sign-in.d.ts +2 -0
  93. package/dist/src/dev-in-app-sign-in.d.ts.map +1 -0
  94. package/dist/src/dev-step-up.d.cts +2 -0
  95. package/dist/src/dev-step-up.d.ts +2 -0
  96. package/dist/src/dev-step-up.d.ts.map +1 -0
  97. package/dist/src/dev-two-factor-challenge.d.cts +2 -0
  98. package/dist/src/dev-two-factor-challenge.d.ts +2 -0
  99. package/dist/src/dev-two-factor-challenge.d.ts.map +1 -0
  100. package/dist/src/hooks/index.d.cts +3 -1
  101. package/dist/src/hooks/index.d.ts +3 -1
  102. package/dist/src/hooks/index.d.ts.map +1 -1
  103. package/dist/src/hooks/use-logout.d.cts +3 -1
  104. package/dist/src/hooks/use-logout.d.ts +3 -1
  105. package/dist/src/hooks/use-logout.d.ts.map +1 -1
  106. package/dist/src/hooks/use-signin.d.cts +19 -2
  107. package/dist/src/hooks/use-signin.d.ts +19 -2
  108. package/dist/src/hooks/use-signin.d.ts.map +1 -1
  109. package/dist/src/hooks/use-signup.d.cts +6 -1
  110. package/dist/src/hooks/use-signup.d.ts +6 -1
  111. package/dist/src/hooks/use-signup.d.ts.map +1 -1
  112. package/dist/src/hooks/use-two-factor-enrollment.d.cts +1 -0
  113. package/dist/src/hooks/use-two-factor-enrollment.d.ts +1 -0
  114. package/dist/src/hooks/use-two-factor-enrollment.d.ts.map +1 -1
  115. package/dist/src/hooks/use-two-factor-setup.d.cts +4 -1
  116. package/dist/src/hooks/use-two-factor-setup.d.ts +4 -1
  117. package/dist/src/hooks/use-two-factor-setup.d.ts.map +1 -1
  118. package/dist/src/hooks/use-user.d.cts +10 -0
  119. package/dist/src/hooks/use-user.d.ts +10 -0
  120. package/dist/src/hooks/use-user.d.ts.map +1 -0
  121. package/dist/src/index.d.cts +1 -0
  122. package/dist/src/index.d.ts +1 -0
  123. package/dist/src/index.d.ts.map +1 -1
  124. package/dist/src/utils/auth-error.d.cts +6 -0
  125. package/dist/src/utils/auth-error.d.ts +6 -0
  126. package/dist/src/utils/auth-error.d.ts.map +1 -1
  127. package/dist/src/utils/index.d.cts +2 -2
  128. package/dist/src/utils/index.d.ts +2 -2
  129. package/dist/src/utils/index.d.ts.map +1 -1
  130. package/dist/src/utils/is-sign-in-page.d.cts +5 -0
  131. package/dist/src/utils/is-sign-in-page.d.ts +5 -0
  132. package/dist/src/utils/is-sign-in-page.d.ts.map +1 -1
  133. package/dist/src/utils/upgrade-auth.d.cts +7 -4
  134. package/dist/src/utils/upgrade-auth.d.ts +7 -4
  135. package/dist/src/utils/upgrade-auth.d.ts.map +1 -1
  136. package/dist/src/utils/url-params.d.ts.map +1 -1
  137. package/dist/src/utils/validation-schemas/index.d.ts.map +1 -1
  138. package/dist/style.css +1 -1
  139. package/package.json +3 -3
package/CHANGELOG.md CHANGED
@@ -2,6 +2,57 @@
2
2
 
3
3
  All notable changes to `@passflow/react` are documented in this file.
4
4
 
5
+ ## [0.26.1] - 2026-11-19
6
+
7
+ ### Fixed
8
+
9
+ - A sign-in through `PassflowFlow` whose `successAuthRedirect` is a full address on the page's own origin (`"http://localhost:5173/"`) no longer loads the page again. On the flow's built-in browser router, the flow moves to that path, query and fragment in the app, so the page draws once. Hash and memory flows, a `location` and `navigate` the app passes, and the hosted login page (`federatedLanding="login-page"`) still load such an address.
10
+ - The password length rule counts characters (code points), as the server does. An emoji counts as one, so the form no longer accepts an emoji password the server refuses.
11
+ - The free `withAuth(fetch, options)` and `attachAuth(instance, options)` take `origins`, like `usePassflow().withAuth` and `passflow.attachAuth`. Without it, a call to another origin went without a token.
12
+ - `dist/style.css` holds no empty `url()`. `--passflow-background-image` defaults to `none`, and a Next.js 16 app with Turbopack builds with the stylesheet imported. The build fails if any shipped stylesheet holds an empty `url()`.
13
+
14
+ ## [0.26.0] - 2026-11-19
15
+
16
+ ### Added
17
+
18
+ - `<LoginButton />`, one button that signs in and out. Signed out, it starts the hosted sign-in (`authRedirect`). Signed in, it shows the user's email and signs out on click, which ends the hosted session after a hosted sign-in. It is disabled and `aria-busy` while signing out.
19
+ - Wrapping one element makes the app's own component the button. It keeps its props, ref and content; only `onClick` is replaced. Its own `onClick` runs first, and `preventDefault()` skips the action. A click with no event still works.
20
+ - A function child receives `{ isSignedIn, label, user, busy }`. While signing out, a wrapped element also gets `disabled` and `aria-busy`.
21
+ - `appearance="shadcn"` takes `--primary`, `--primary-foreground`, `--ring`, `--radius` and `--font-sans` from the app's shadcn theme. It accepts HSL triplets and complete colours (oklch, hex), falls back to the Passflow tokens, and follows theme switches (`class`, `style` or `data-theme` on `<html>` or `<body>`, and `prefers-color-scheme`).
22
+ - The visual part is the `LoginButtonView` primitive.
23
+ - `useSignIn({ onPasswordChangeRequired })` for an account that must change its password. The bundled form opens forgot-password with the identifier filled in and shows a fixed notice.
24
+ - `useUser()` returns the signed-in user's `{ id, email, phone }`, after hosted and embedded sign-in.
25
+ - `useAuth().isSignedIn`, a boolean that updates on sign-in, sign-up, sign-out and session expiry.
26
+ - `UpgradeProvider` option `onSignInRequired` (default: the flow's sign-in route, else `authRedirect()`), and `failures: 'dialog' | 'caller'`.
27
+ - Exports `usePassflowStore`, `TwoFactorSetupFlow` and `TwoFactorVerifyFlow`.
28
+ - `TwoFactorSetupForm` shows an enrolled state for a user with two-factor authentication on.
29
+
30
+ ### Changed
31
+
32
+ - Requires `@passflow/core` ^0.32.0 (was ^0.30.1). On 0.x a caret range stops at the minor, so an app on core 0.32.0 got a second, older core under this package. `usePassflow().withAuth(fetch, { origins })` then failed to compile, and the request went out with no token.
33
+ - `useSignIn().fetch` and `useSignUp().fetch` resolve `'signed-in'` or `'two-factor-required'` (both truthy) instead of `true`, the server's answer for passwordless, or `false`. Code that compared with `=== true` must check the outcome.
34
+ - `useAuth().logout` is typed `() => Promise<void>`. It and `useLogout().fetch` take core's `LogOutOptions` (`postLogoutRedirectUri`). After a hosted sign-in, core also ends the hosted login's session, so the page leaves and code after `await logout()` may not run.
35
+ - 2FA code entry sizes itself from the enrolment's code length. The step-up dialog allows 8 digits for authenticator codes; emailed and texted codes stay at 6.
36
+ - Step-up offers exactly the factors the server lists, in its order. Sign-in required closes the dialog and calls `onSignInRequired`. Unsatisfiable says the account can't reach the level. Rate limited gives the wait as a duration ("15 minutes", "1 day"). Insufficient offers "Sign in again".
37
+ - The hosted login page reads an exchange refusal with core's `parseUpgradeChallenge`, keeping the server's factor list and sign-in flag.
38
+ - Password rules follow the app's policy and the server's checks. The length rule applies only when a minimum is set, symbols are exactly the server's set, and capitals and small letters count in any script.
39
+ - Recovery codes are described in their current format, `XXXX-XXXX-XXXX`.
40
+ - Sign-in and sign-up subtitles use the application name from Login Appearance.
41
+ - The sign-in and sign-up email error shows once the user leaves the field or submits.
42
+ - "Trust this device" is hidden when the server says `device_trust_available: false`.
43
+
44
+ ### Fixed
45
+
46
+ - Changing the `appId` prop switches the app: the old app's tokens no longer count as signed in, and the new app's settings, scopes and password policy are loaded.
47
+ - Cancelling a step-up dialog no longer hides the next step-up's outcome.
48
+ - The hosted step-up no longer cancels itself on a fast server. App settings that arrived after the refusal rebuilt the instance and closed the prompt, and a refusal could land before the new instance's step-up was enabled.
49
+ - A refused hosted sign-in return shows "Sign-in failed. Start again." instead of nothing.
50
+ - A confirm answer with no recovery codes skips the codes screen instead of showing an empty list or crashing.
51
+ - Forgot-password fills in the identifier from the URL, sends only the identifier on screen, and refuses an empty field.
52
+ - `FieldPhone` shows a value it is given and reports country changes.
53
+ - `useUrlParams` undoes only `&amp;` in the query, so a key such as `notice` is no longer decoded as an HTML entity.
54
+ - `LoginAlert` loads its own stylesheet, so its `info` tone is styled outside the login screen.
55
+
5
56
  ## [0.25.3] - 2026-11-15
6
57
 
7
58
  ### Changed
package/README.md CHANGED
@@ -16,6 +16,9 @@ This is a SDK for react application.
16
16
  - [Integration](#integration)
17
17
  - [Passflow Cloud](#passflow-cloud)
18
18
  - [PassflowFlow](#passflowflow)
19
+ - [Login button](#login-button)
20
+ - [Two-factor setup for a signed-in user](#two-factor-setup-for-a-signed-in-user)
21
+ - [Step-up](#step-up)
19
22
  - [React Router](#react-router)
20
23
  - [Wouter](#wouter)
21
24
  - [TanStack Router](#tanstack-router)
@@ -32,6 +35,7 @@ This is a SDK for react application.
32
35
  - [InvitationJoin](#invitationjoin)
33
36
  - [Hooks](#hooks)
34
37
  - [useAuth](#useauth)
38
+ - [useUser](#useuser)
35
39
  - [usePassflow](#usepassflow)
36
40
  - [usePassflowStore](#usepassflowstore)
37
41
  - [useSignIn](#usesignin)
@@ -134,7 +138,7 @@ Or launch Playwright's interactive test UI:
134
138
  pnpm test:ui
135
139
  ```
136
140
 
137
- Both scripts automatically start the demo app and Storybook. `pnpm test` uses the economical headless configuration, while `pnpm test:ui` retains traces and a final full-page screenshot for every successful test. Select a runnable leaf test in Playwright UI to inspect its action timeline, DOM snapshots, and screenshot attachment. Tests live in `playwright/tests`; component-state tests render the real Storybook stories rather than recreating components in a test-only harness. Vitest remains for non-visual hooks, controllers, and utility logic.
141
+ Both scripts start their own demo app on port 5173 and Storybook on port 6107, and never reuse a running server, so the run fails if either port is taken. `pnpm test` uses the economical headless configuration, while `pnpm test:ui` retains traces and a final full-page screenshot for every successful test. Select a runnable leaf test in Playwright UI to inspect its action timeline, DOM snapshots, and screenshot attachment. Tests live in `playwright/tests`; component-state tests render the real Storybook stories rather than recreating components in a test-only harness. Vitest remains for non-visual hooks, controllers, and utility logic.
138
142
 
139
143
  ## Installation
140
144
 
@@ -227,14 +231,17 @@ the provider:
227
231
  `max_age`, `id_token_hint`) without re-prompting an
228
232
  already-authenticated user.
229
233
 
230
- 3. **Renders a placeholder during exchange** (theme via the
231
- `continuingToAppPlaceholder` prop on PassflowFlow — TODO once
232
- the prop is wired through; today the default placeholder
233
- appears).
234
+ 3. **Shows a status line during exchange.** While the exchange
235
+ runs, the provider renders "Continuing to <app name>…" in place
236
+ of its children, with the app name taken from the parent
237
+ challenge, or "Continuing…" when the challenge has none.
234
238
 
235
- 4. **Falls through to the login form** when exchange is impossible
236
- or returns `403 upgrade_required`. The login form gets an
237
- explanatory banner (also themeable).
239
+ 4. **Falls through to the login form** when exchange is impossible,
240
+ or when its `403 upgrade_required` can only be met by a fresh sign-in.
241
+ The login form gets an explanatory banner (also themeable). A 403 the
242
+ hosted session can meet opens the step-up dialog instead; after a
243
+ successful step-up the hosted page tries the exchange once more, and a
244
+ second refusal goes back to the app as `unmet_authentication_requirements`.
238
245
 
239
246
  Consumer code remains a single line of plumbing — mount
240
247
  `<PassflowProvider>`, render `<PassflowFlow>` inside, done. No
@@ -299,8 +306,80 @@ function AuthPage() {
299
306
  }
300
307
  ```
301
308
 
309
+ With a location the app passes, a sign-in whose `successAuthRedirect` is a full address loads that page, as before; only the built-in browser router moves there in the app without a reload.
310
+
302
311
  `TwoFactorSetupMagicLinkFlow` can also be mounted on its own page. It reads the token from the URL (the segment after `/two-factor-setup-magic-link/`), or takes it as a `token` prop.
303
312
 
313
+ ### Login button
314
+
315
+ One button that signs in and out, inside `PassflowProvider`:
316
+
317
+ ```tsx
318
+ import { LoginButton } from '@passflow/react';
319
+
320
+ <LoginButton />;
321
+ ```
322
+
323
+ Signed out, it shows **Sign in** (`signInLabel`) and a click starts the hosted sign-in, `passflow.authRedirect(authRedirectOptions)`. Signed in, it shows the user's email (`signedInLabel={(user) => …}`) and a click signs out with `useAuth().logout()`, which also ends the hosted session after a hosted sign-in; `postLogoutRedirectUri` says where the browser lands. The button is disabled and `aria-busy` while the sign-out is in flight, and it switches on sign-in and sign-out events.
324
+
325
+ To use your own button, wrap it. Your element becomes the button: it keeps its props, content and look, and gets a click handler that calls its own `onClick` first, then signs in or out unless that called `event.preventDefault()` (a component that calls `onClick()` with no event still signs in or out). No label is injected, so its text stays as you wrote it. While the sign-out is in flight it gets `disabled` and `aria-busy`; otherwise those stay as you passed them. For text that switches, pass a function, which also receives `busy`:
326
+
327
+ ```tsx
328
+ <LoginButton>
329
+ <MyButton>Account</MyButton>
330
+ </LoginButton>
331
+
332
+ <LoginButton>{({ isSignedIn, label, user, busy }) => <MyButton disabled={busy}>{label}</MyButton>}</LoginButton>
333
+ ```
334
+
335
+ In an app styled with shadcn/ui, `appearance="shadcn"` takes `--primary`, `--primary-foreground`, `--radius`, `--ring` and `--font-sans` from your theme, in either the HSL-triplet (`222.2 47.4% 11.2%`) or the complete-colour (`oklch(…)`, hex) format. A missing variable falls back to the Passflow style, and a missing `--font-sans` to the inherited font. The button follows a theme switch: a `dark` class, `data-theme` or style change on `<html>` or `<body>`, or a change of `prefers-color-scheme`.
336
+
337
+ ```tsx
338
+ <LoginButton appearance='shadcn' />
339
+ ```
340
+
341
+ ### Two-factor setup for a signed-in user
342
+
343
+ When the app's two-factor policy is **Optional**, sign-in does not ask the user to set up an authenticator. Let a signed-in user turn it on from your own page with one component:
344
+
345
+ ```tsx
346
+ import '@passflow/react/dist/style.css';
347
+ import { TwoFactorSetupForm, useAuth } from '@passflow/react';
348
+
349
+ const Security = () => (useAuth().isSignedIn ? <TwoFactorSetupForm /> : null);
350
+ ```
351
+
352
+ In the admin panel, on the app's two-factor settings, set the policy to **Optional** and turn **TOTP** on.
353
+
354
+ The form reads the user's two-factor status when it mounts. A user who has it on sees that it is on. Anyone else gets **Get Started**, then the QR code (with the key to type in by hand), then a field for the code from the authenticator app, then the recovery codes to save, and then the same "it is on" state. When the confirm answer carries no recovery codes, the codes screen is skipped.
355
+
356
+ ### Step-up
357
+
358
+ When your own API answers `403 upgrade_required`, the provider can ask the signed-in user to verify again and then repeat the call. Name the API in `backend` and turn the fetch interceptor on:
359
+
360
+ ```tsx
361
+ <PassflowProvider url={url} appId={appId} backend='https://api.example.com' upgrade={{ interceptFetch: true }}>
362
+ <App />
363
+ </PassflowProvider>
364
+ ```
365
+
366
+ The dialog offers the factors the server lists in `available_factors`, in its order, with the first one selected. When the step-up cannot go ahead, it ends in one of four ways:
367
+
368
+ | Outcome | What the user sees |
369
+ |---------|--------------------|
370
+ | Sign-in required (`sign_in_required: true`) | The dialog closes and the provider calls `upgrade.onSignInRequired`. The call is not repeated. |
371
+ | Unsatisfiable (no factor reaches the level) | "This account can't reach the security level this app asks for. Add a passkey on this device, or contact the app.", with **Close**. Nothing navigates. |
372
+ | Rate limited | "Too many attempts. Try again in 30 seconds.", with the wait in seconds, minutes, hours or days, rounded up ("15 minutes", "1 day"), or the server's own text when it names no wait. No new code is asked for. |
373
+ | Insufficient (verified, but the call was challenged again) | The levels reached and required, with **Sign in again**, which calls `upgrade.onSignInRequired`. |
374
+
375
+ `upgrade.onSignInRequired` is where those sign-ins go. By default it opens the app's own sign-in route when a `PassflowFlow` is mounted, and otherwise calls `passflow.authRedirect()`. Pass your own to send the user somewhere else:
376
+
377
+ ```tsx
378
+ <PassflowProvider {...config} upgrade={{ interceptFetch: true, onSignInRequired: () => router.navigate('/login') }}>
379
+ ```
380
+
381
+ The hosted login page never calls `onSignInRequired`. It shows the sign-in form for sign-in required and insufficient, and puts the unsatisfiable and rate-limited messages on the page in place of the form.
382
+
304
383
  The sections below wire the app's router into the **standalone form components** (`SignIn`, `SignUp`, …) through `PassflowProvider`'s `navigate` and `router` props. `PassflowFlow` does not need them.
305
384
 
306
385
  ### React Router
@@ -707,22 +786,71 @@ Component for accepting invitations and joining organizations.
707
786
  Hook for authentication management. Provides methods for checking authentication status, obtaining tokens, and logging out.
708
787
 
709
788
  ```typescript
710
- const { isAuthenticated, getTokens, logout, isLoading } = useAuth();
789
+ const { isSignedIn, isAuthenticated, getTokens, logout, isLoading } = useAuth();
711
790
  ```
712
791
 
713
- | Parameter | Type | Description |
714
- |-----------|------|-------------|
715
- | initialRefresh | `boolean` (optional) | Whether to refresh tokens on mount |
716
-
717
792
  **Returns:**
718
793
 
719
794
  | Property | Type | Description |
720
795
  |----------|------|-------------|
721
- | isAuthenticated | `() => boolean` | Current authentication status |
796
+ | isSignedIn | `boolean` | Whether a user is signed in. Updates after a sign-in or sign-up, after `logout()` or `useLogout().fetch()`, and when the session expires, so the component re-renders with the new value |
797
+ | isAuthenticated | `() => boolean` | Checks the session at the moment you call it |
798
+ | isSessionExpired | `boolean` | `true` after the session has ended on its own, until the next sign-in |
722
799
  | getTokens | `(doRefresh: boolean) => Promise<{ tokens: Tokens \| undefined; parsedTokens: ParsedTokens \| undefined; }>` | Function to get authentication tokens |
723
- | logout | `() => void` | Function to log out user |
800
+ | logout | `(options?: LogOutOptions) => Promise<void>` | Signs the user out; resolves once the sign-out finishes. See [Signing out after a hosted sign-in](#signing-out-after-a-hosted-sign-in) |
724
801
  | isLoading | `boolean` | Loading state indicator |
725
802
 
803
+ Render on `isSignedIn`:
804
+
805
+ ```tsx
806
+ const AccountMenu = () => {
807
+ const { isSignedIn, logout } = useAuth();
808
+ if (!isSignedIn) return <a href='/web/signin'>Sign in</a>;
809
+ return <button onClick={() => void logout()}>Sign out</button>;
810
+ };
811
+ ```
812
+
813
+ #### Signing out after a hosted sign-in
814
+
815
+ When the user signed in through the hosted login (`authRedirect()`), `logout()` also ends the hosted login's own session: once the app is signed out, the browser leaves the page for the hosted login's sign-out and comes back to `postLogoutRedirectUri`. Code after `await logout()` may then not run, so don't put anything you need after it. A sign-in made in the app itself (`PassflowFlow`) never leaves the page.
816
+
817
+ ```tsx
818
+ const { logout } = useAuth();
819
+
820
+ // Must match one of the app's registered redirect URLs. Default: the address the hosted sign-in landed on.
821
+ await logout({ postLogoutRedirectUri: 'https://app.example.com/signed-out' });
822
+ ```
823
+
824
+ `useLogout().fetch` takes the same options. `LogOutOptions` is exported from `@passflow/react`.
825
+
826
+ ### useUser
827
+ Hook for the signed-in user's id, email and phone, read from the tokens. It re-renders on sign-in, sign-up, token refresh, sign-out and session expiry.
828
+
829
+ ```typescript
830
+ const { id, email, phone } = useUser();
831
+ ```
832
+
833
+ The claim is read from the id token first and from the access token when the id token lacks it. A hosted sign-in (`authRedirect()`) puts `email` in the access token and the embedded screens put it in the id token, so `email` is filled either way.
834
+
835
+ **Returns:**
836
+
837
+ | Property | Type | Description |
838
+ |----------|------|-------------|
839
+ | id | `string \| undefined` | The user id (`sub`) |
840
+ | email | `string \| undefined` | The user's email |
841
+ | phone | `string \| undefined` | The user's phone number |
842
+
843
+ Every field is `undefined` when no one is signed in.
844
+
845
+ ```tsx
846
+ const AccountMenu = () => {
847
+ const { isSignedIn, logout } = useAuth();
848
+ const { email } = useUser();
849
+ if (!isSignedIn) return null;
850
+ return <button onClick={() => void logout()}>Sign out {email}</button>;
851
+ };
852
+ ```
853
+
726
854
  ### usePassflow
727
855
  Hook for accessing the Passflow SDK instance. Must be used within PassflowProvider.
728
856
 
@@ -758,17 +886,46 @@ Hook for implementing sign-in functionality. Supports password, passkey, and pas
758
886
 
759
887
  ```typescript
760
888
  const { fetch, isLoading, isError, error } = useSignIn();
889
+
890
+ const outcome = await fetch({ email, password }, 'password');
891
+ if (outcome === 'two-factor-required') navigate('/2fa');
892
+ else if (outcome === 'signed-in') navigate('/');
893
+ ```
894
+
895
+ **Options:**
896
+
897
+ | Option | Type | Description |
898
+ |--------|------|-------------|
899
+ | onPasswordChangeRequired | `(notice: string) => void` (optional) | Called when the server refuses the sign-in until the user changes their password. `fetch` resolves `false` and `error` stays empty; send the user to your password reset. `notice` is the server's message for the user. Without this option, that message becomes the form `error`. |
900
+
901
+ ```typescript
902
+ const { fetch } = useSignIn({
903
+ onPasswordChangeRequired: () => navigate('/forgot-password?default_method=email_or_username&reason=password_change_required'),
904
+ });
761
905
  ```
762
906
 
907
+ The bundled `SignIn` form does this for you: it opens `ForgotPassword` with the identifier filled in and a fixed notice. `ForgotPassword` shows that notice only for `reason=password_change_required` and never displays text from the URL.
908
+
763
909
  **Returns:**
764
910
 
765
911
  | Property | Type | Description |
766
912
  |----------|------|-------------|
767
- | fetch | `(payload: PassflowPasskeyAuthenticateStartPayload \| PassflowSignInPayload \| PassflowPasswordlessSignInPayload, type: 'passkey' \| 'password' \| 'passwordless') => Promise<boolean \| string \| PassflowPasswordlessResponse>` | Sign in function |
913
+ | fetch | `(payload: PassflowPasskeyAuthenticateStartPayload \| PassflowSignInPayload \| PassflowPasswordlessSignInPayload, type: 'passkey' \| 'password' \| 'passwordless') => Promise<false \| SignInOutcome \| PassflowPasswordlessResponse>` | Sign in function |
768
914
  | isLoading | `boolean` | Loading state |
769
915
  | isError | `boolean` | Error state |
770
916
  | error | `string` | Error message |
771
917
 
918
+ `fetch` resolves one of:
919
+
920
+ | Value | Meaning |
921
+ |-------|---------|
922
+ | `'signed-in'` | A password or passkey sign-in finished; the session is ready. |
923
+ | `'two-factor-required'` | The credentials were accepted but a second factor is still owed. There is no session yet; send the user to your 2FA screen. |
924
+ | `PassflowPasswordlessResponse` | The server's answer to a passwordless request. |
925
+ | `false` | The request failed (see `error`), was superseded, entered the emailed-code step, or was handed to `onPasswordChangeRequired`. |
926
+
927
+ Both outcome strings are truthy, so `if (await fetch(...))` also passes for `'two-factor-required'`, when there is no session yet. Check for `'two-factor-required'` before treating the user as signed in. Code that compared the result with `=== true` must check the outcome instead.
928
+
772
929
  ### useSignUp
773
930
  Hook for implementing registration functionality. Supports password, passkey, and passwordless registration.
774
931
 
@@ -780,11 +937,13 @@ const { fetch, isLoading, isError, error } = useSignUp();
780
937
 
781
938
  | Property | Type | Description |
782
939
  |----------|------|-------------|
783
- | fetch | `(payload: PassflowPasskeyRegisterStartPayload \| PassflowSignUpPayload \| PassflowPasswordlessSignInPayload, type: 'passkey' \| 'password' \| 'passwordless') => Promise<boolean \| PassflowPasswordlessResponse>` | Sign up function |
940
+ | fetch | `(payload: PassflowPasskeyRegisterStartPayload \| PassflowSignUpPayload \| PassflowPasswordlessSignInPayload, type: 'passkey' \| 'password' \| 'passwordless') => Promise<false \| SignInOutcome \| PassflowPasswordlessResponse>` | Sign up function |
784
941
  | isLoading | `boolean` | Loading state |
785
942
  | isError | `boolean` | Error state |
786
943
  | error | `string` | Error message |
787
944
 
945
+ `fetch` resolves the same outcomes as `useSignIn().fetch`.
946
+
788
947
  ### useNavigation
789
948
  Hook for navigation between pages. Supports various routers (react-router, wouter, tanstack-router).
790
949
 
@@ -929,7 +1088,7 @@ const { fetch, isLoading, isError, error } = useLogout();
929
1088
 
930
1089
  | Property | Type | Description |
931
1090
  |----------|------|-------------|
932
- | fetch | `() => Promise<boolean>` | Logout function |
1091
+ | fetch | `(options?: LogOutOptions) => Promise<boolean>` | Signs the user out, with the same `postLogoutRedirectUri` option as `useAuth().logout`. After a hosted sign-in the page may leave before it resolves |
933
1092
  | isLoading | `boolean` | Loading state |
934
1093
  | isError | `boolean` | Error state |
935
1094
  | error | `string` | Error message |