@passflow/react 0.25.3 → 0.26.0

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 (124) hide show
  1. package/CHANGELOG.md +42 -0
  2. package/README.md +175 -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/two-factor-setup/index.d.cts +1 -2
  14. package/dist/src/components/flow/two-factor-setup/index.d.ts +1 -2
  15. package/dist/src/components/flow/two-factor-setup/index.d.ts.map +1 -1
  16. package/dist/src/components/flow/two-factor-verify/index.d.cts +1 -2
  17. package/dist/src/components/flow/two-factor-verify/index.d.ts +1 -2
  18. package/dist/src/components/flow/two-factor-verify/index.d.ts.map +1 -1
  19. package/dist/src/components/form/forgot-password/forgot-password.d.ts.map +1 -1
  20. package/dist/src/components/form/signin/index.d.ts.map +1 -1
  21. package/dist/src/components/form/signup/index.d.ts.map +1 -1
  22. package/dist/src/components/form/two-factor-challenge/index.d.ts.map +1 -1
  23. package/dist/src/components/form/two-factor-setup/setup-screen.d.cts +2 -0
  24. package/dist/src/components/form/two-factor-setup/setup-screen.d.ts +2 -0
  25. package/dist/src/components/form/two-factor-setup/setup-screen.d.ts.map +1 -1
  26. package/dist/src/components/form/two-factor-setup/two-factor-setup-form.d.cts +1 -1
  27. package/dist/src/components/form/two-factor-setup/two-factor-setup-form.d.ts +1 -1
  28. package/dist/src/components/form/two-factor-setup/two-factor-setup-form.d.ts.map +1 -1
  29. package/dist/src/components/form/two-factor-verify/two-factor-recovery-form.d.cts +2 -0
  30. package/dist/src/components/form/two-factor-verify/two-factor-recovery-form.d.ts +2 -0
  31. package/dist/src/components/form/two-factor-verify/two-factor-recovery-form.d.ts.map +1 -1
  32. package/dist/src/components/form/two-factor-verify/two-factor-verify-form.d.cts +2 -0
  33. package/dist/src/components/form/two-factor-verify/two-factor-verify-form.d.ts +2 -0
  34. package/dist/src/components/form/two-factor-verify/two-factor-verify-form.d.ts.map +1 -1
  35. package/dist/src/components/login-button/index.d.cts +54 -0
  36. package/dist/src/components/login-button/index.d.ts +54 -0
  37. package/dist/src/components/login-button/index.d.ts.map +1 -0
  38. package/dist/src/components/provider/passflow-provider.d.cts +4 -2
  39. package/dist/src/components/provider/passflow-provider.d.ts +4 -2
  40. package/dist/src/components/provider/passflow-provider.d.ts.map +1 -1
  41. package/dist/src/components/provider/upgrade-provider.d.cts +14 -1
  42. package/dist/src/components/provider/upgrade-provider.d.ts +14 -1
  43. package/dist/src/components/provider/upgrade-provider.d.ts.map +1 -1
  44. package/dist/src/components/ui/fields/field-password.d.ts.map +1 -1
  45. package/dist/src/components/ui/fields/field-phone.d.ts.map +1 -1
  46. package/dist/src/components/ui/index.d.cts +1 -0
  47. package/dist/src/components/ui/index.d.ts +1 -0
  48. package/dist/src/components/ui/index.d.ts.map +1 -1
  49. package/dist/src/components/ui/login-button/index.d.cts +2 -0
  50. package/dist/src/components/ui/login-button/index.d.ts +2 -0
  51. package/dist/src/components/ui/login-button/index.d.ts.map +1 -0
  52. package/dist/src/components/ui/login-button/login-button.d.cts +36 -0
  53. package/dist/src/components/ui/login-button/login-button.d.ts +36 -0
  54. package/dist/src/components/ui/login-button/login-button.d.ts.map +1 -0
  55. package/dist/src/components/ui/login-button/shadcn-theme.d.cts +23 -0
  56. package/dist/src/components/ui/login-button/shadcn-theme.d.ts +23 -0
  57. package/dist/src/components/ui/login-button/shadcn-theme.d.ts.map +1 -0
  58. package/dist/src/components/ui/login-button/use-shadcn-theme.d.cts +9 -0
  59. package/dist/src/components/ui/login-button/use-shadcn-theme.d.ts +9 -0
  60. package/dist/src/components/ui/login-button/use-shadcn-theme.d.ts.map +1 -0
  61. package/dist/src/components/ui/login-screen/credentials.d.ts.map +1 -1
  62. package/dist/src/components/ui/login-screen/shared.d.ts.map +1 -1
  63. package/dist/src/components/ui/login-screen/two-factor-code.d.cts +2 -1
  64. package/dist/src/components/ui/login-screen/two-factor-code.d.ts +2 -1
  65. package/dist/src/components/ui/login-screen/two-factor-code.d.ts.map +1 -1
  66. package/dist/src/components/ui/login-screen/types.d.cts +2 -1
  67. package/dist/src/components/ui/login-screen/types.d.ts +2 -1
  68. package/dist/src/components/ui/login-screen/types.d.ts.map +1 -1
  69. package/dist/src/components/ui/upgrade-dialog/state-mapping.d.cts +9 -3
  70. package/dist/src/components/ui/upgrade-dialog/state-mapping.d.ts +9 -3
  71. package/dist/src/components/ui/upgrade-dialog/state-mapping.d.ts.map +1 -1
  72. package/dist/src/components/ui/upgrade-dialog/status.d.ts.map +1 -1
  73. package/dist/src/components/ui/upgrade-dialog/types.d.cts +6 -2
  74. package/dist/src/components/ui/upgrade-dialog/types.d.ts +6 -2
  75. package/dist/src/components/ui/upgrade-dialog/types.d.ts.map +1 -1
  76. package/dist/src/context/auth-context.d.cts +8 -2
  77. package/dist/src/context/auth-context.d.ts +8 -2
  78. package/dist/src/context/auth-context.d.ts.map +1 -1
  79. package/dist/src/context/upgrade-context.d.cts +1 -1
  80. package/dist/src/context/upgrade-context.d.ts +1 -1
  81. package/dist/src/context/upgrade-context.d.ts.map +1 -1
  82. package/dist/src/dev-account.d.cts +2 -0
  83. package/dist/src/dev-account.d.ts +2 -0
  84. package/dist/src/dev-account.d.ts.map +1 -0
  85. package/dist/src/dev-step-up.d.cts +2 -0
  86. package/dist/src/dev-step-up.d.ts +2 -0
  87. package/dist/src/dev-step-up.d.ts.map +1 -0
  88. package/dist/src/dev-two-factor-challenge.d.cts +2 -0
  89. package/dist/src/dev-two-factor-challenge.d.ts +2 -0
  90. package/dist/src/dev-two-factor-challenge.d.ts.map +1 -0
  91. package/dist/src/hooks/index.d.cts +3 -1
  92. package/dist/src/hooks/index.d.ts +3 -1
  93. package/dist/src/hooks/index.d.ts.map +1 -1
  94. package/dist/src/hooks/use-logout.d.cts +3 -1
  95. package/dist/src/hooks/use-logout.d.ts +3 -1
  96. package/dist/src/hooks/use-logout.d.ts.map +1 -1
  97. package/dist/src/hooks/use-signin.d.cts +19 -2
  98. package/dist/src/hooks/use-signin.d.ts +19 -2
  99. package/dist/src/hooks/use-signin.d.ts.map +1 -1
  100. package/dist/src/hooks/use-signup.d.cts +6 -1
  101. package/dist/src/hooks/use-signup.d.ts +6 -1
  102. package/dist/src/hooks/use-signup.d.ts.map +1 -1
  103. package/dist/src/hooks/use-two-factor-enrollment.d.cts +1 -0
  104. package/dist/src/hooks/use-two-factor-enrollment.d.ts +1 -0
  105. package/dist/src/hooks/use-two-factor-enrollment.d.ts.map +1 -1
  106. package/dist/src/hooks/use-two-factor-setup.d.cts +4 -1
  107. package/dist/src/hooks/use-two-factor-setup.d.ts +4 -1
  108. package/dist/src/hooks/use-two-factor-setup.d.ts.map +1 -1
  109. package/dist/src/hooks/use-user.d.cts +10 -0
  110. package/dist/src/hooks/use-user.d.ts +10 -0
  111. package/dist/src/hooks/use-user.d.ts.map +1 -0
  112. package/dist/src/index.d.cts +1 -0
  113. package/dist/src/index.d.ts +1 -0
  114. package/dist/src/index.d.ts.map +1 -1
  115. package/dist/src/utils/auth-error.d.cts +6 -0
  116. package/dist/src/utils/auth-error.d.ts +6 -0
  117. package/dist/src/utils/auth-error.d.ts.map +1 -1
  118. package/dist/src/utils/index.d.cts +1 -1
  119. package/dist/src/utils/index.d.ts +1 -1
  120. package/dist/src/utils/index.d.ts.map +1 -1
  121. package/dist/src/utils/url-params.d.ts.map +1 -1
  122. package/dist/src/utils/validation-schemas/index.d.ts.map +1 -1
  123. package/dist/style.css +1 -1
  124. package/package.json +2 -2
package/CHANGELOG.md CHANGED
@@ -2,6 +2,48 @@
2
2
 
3
3
  All notable changes to `@passflow/react` are documented in this file.
4
4
 
5
+ ## [0.26.0] - 2026-11-19
6
+
7
+ ### Added
8
+
9
+ - `<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.
10
+ - 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.
11
+ - A function child receives `{ isSignedIn, label, user, busy }`. While signing out, a wrapped element also gets `disabled` and `aria-busy`.
12
+ - `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`).
13
+ - The visual part is the `LoginButtonView` primitive.
14
+ - `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.
15
+ - `useUser()` returns the signed-in user's `{ id, email, phone }`, after hosted and embedded sign-in.
16
+ - `useAuth().isSignedIn`, a boolean that updates on sign-in, sign-up, sign-out and session expiry.
17
+ - `UpgradeProvider` option `onSignInRequired` (default: the flow's sign-in route, else `authRedirect()`), and `failures: 'dialog' | 'caller'`.
18
+ - Exports `usePassflowStore`, `TwoFactorSetupFlow` and `TwoFactorVerifyFlow`.
19
+ - `TwoFactorSetupForm` shows an enrolled state for a user with two-factor authentication on.
20
+
21
+ ### Changed
22
+
23
+ - 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.
24
+ - `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.
25
+ - `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.
26
+ - 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.
27
+ - 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".
28
+ - The hosted login page reads an exchange refusal with core's `parseUpgradeChallenge`, keeping the server's factor list and sign-in flag.
29
+ - 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.
30
+ - Recovery codes are described in their current format, `XXXX-XXXX-XXXX`.
31
+ - Sign-in and sign-up subtitles use the application name from Login Appearance.
32
+ - The sign-in and sign-up email error shows once the user leaves the field or submits.
33
+ - "Trust this device" is hidden when the server says `device_trust_available: false`.
34
+
35
+ ### Fixed
36
+
37
+ - 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.
38
+ - Cancelling a step-up dialog no longer hides the next step-up's outcome.
39
+ - 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.
40
+ - A refused hosted sign-in return shows "Sign-in failed. Start again." instead of nothing.
41
+ - A confirm answer with no recovery codes skips the codes screen instead of showing an empty list or crashing.
42
+ - Forgot-password fills in the identifier from the URL, sends only the identifier on screen, and refuses an empty field.
43
+ - `FieldPhone` shows a value it is given and reports country changes.
44
+ - `useUrlParams` undoes only `&amp;` in the query, so a key such as `notice` is no longer decoded as an HTML entity.
45
+ - `LoginAlert` loads its own stylesheet, so its `info` tone is styled outside the login screen.
46
+
5
47
  ## [0.25.3] - 2026-11-15
6
48
 
7
49
  ### 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
@@ -301,6 +308,76 @@ function AuthPage() {
301
308
 
302
309
  `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
310
 
311
+ ### Login button
312
+
313
+ One button that signs in and out, inside `PassflowProvider`:
314
+
315
+ ```tsx
316
+ import { LoginButton } from '@passflow/react';
317
+
318
+ <LoginButton />;
319
+ ```
320
+
321
+ 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.
322
+
323
+ 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`:
324
+
325
+ ```tsx
326
+ <LoginButton>
327
+ <MyButton>Account</MyButton>
328
+ </LoginButton>
329
+
330
+ <LoginButton>{({ isSignedIn, label, user, busy }) => <MyButton disabled={busy}>{label}</MyButton>}</LoginButton>
331
+ ```
332
+
333
+ 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`.
334
+
335
+ ```tsx
336
+ <LoginButton appearance='shadcn' />
337
+ ```
338
+
339
+ ### Two-factor setup for a signed-in user
340
+
341
+ 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:
342
+
343
+ ```tsx
344
+ import '@passflow/react/dist/style.css';
345
+ import { TwoFactorSetupForm, useAuth } from '@passflow/react';
346
+
347
+ const Security = () => (useAuth().isSignedIn ? <TwoFactorSetupForm /> : null);
348
+ ```
349
+
350
+ In the admin panel, on the app's two-factor settings, set the policy to **Optional** and turn **TOTP** on.
351
+
352
+ 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.
353
+
354
+ ### Step-up
355
+
356
+ 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:
357
+
358
+ ```tsx
359
+ <PassflowProvider url={url} appId={appId} backend='https://api.example.com' upgrade={{ interceptFetch: true }}>
360
+ <App />
361
+ </PassflowProvider>
362
+ ```
363
+
364
+ 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:
365
+
366
+ | Outcome | What the user sees |
367
+ |---------|--------------------|
368
+ | Sign-in required (`sign_in_required: true`) | The dialog closes and the provider calls `upgrade.onSignInRequired`. The call is not repeated. |
369
+ | 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. |
370
+ | 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. |
371
+ | Insufficient (verified, but the call was challenged again) | The levels reached and required, with **Sign in again**, which calls `upgrade.onSignInRequired`. |
372
+
373
+ `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:
374
+
375
+ ```tsx
376
+ <PassflowProvider {...config} upgrade={{ interceptFetch: true, onSignInRequired: () => router.navigate('/login') }}>
377
+ ```
378
+
379
+ 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.
380
+
304
381
  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
382
 
306
383
  ### React Router
@@ -707,22 +784,71 @@ Component for accepting invitations and joining organizations.
707
784
  Hook for authentication management. Provides methods for checking authentication status, obtaining tokens, and logging out.
708
785
 
709
786
  ```typescript
710
- const { isAuthenticated, getTokens, logout, isLoading } = useAuth();
787
+ const { isSignedIn, isAuthenticated, getTokens, logout, isLoading } = useAuth();
711
788
  ```
712
789
 
713
- | Parameter | Type | Description |
714
- |-----------|------|-------------|
715
- | initialRefresh | `boolean` (optional) | Whether to refresh tokens on mount |
716
-
717
790
  **Returns:**
718
791
 
719
792
  | Property | Type | Description |
720
793
  |----------|------|-------------|
721
- | isAuthenticated | `() => boolean` | Current authentication status |
794
+ | 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 |
795
+ | isAuthenticated | `() => boolean` | Checks the session at the moment you call it |
796
+ | isSessionExpired | `boolean` | `true` after the session has ended on its own, until the next sign-in |
722
797
  | getTokens | `(doRefresh: boolean) => Promise<{ tokens: Tokens \| undefined; parsedTokens: ParsedTokens \| undefined; }>` | Function to get authentication tokens |
723
- | logout | `() => void` | Function to log out user |
798
+ | 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
799
  | isLoading | `boolean` | Loading state indicator |
725
800
 
801
+ Render on `isSignedIn`:
802
+
803
+ ```tsx
804
+ const AccountMenu = () => {
805
+ const { isSignedIn, logout } = useAuth();
806
+ if (!isSignedIn) return <a href='/web/signin'>Sign in</a>;
807
+ return <button onClick={() => void logout()}>Sign out</button>;
808
+ };
809
+ ```
810
+
811
+ #### Signing out after a hosted sign-in
812
+
813
+ 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.
814
+
815
+ ```tsx
816
+ const { logout } = useAuth();
817
+
818
+ // Must match one of the app's registered redirect URLs. Default: the address the hosted sign-in landed on.
819
+ await logout({ postLogoutRedirectUri: 'https://app.example.com/signed-out' });
820
+ ```
821
+
822
+ `useLogout().fetch` takes the same options. `LogOutOptions` is exported from `@passflow/react`.
823
+
824
+ ### useUser
825
+ 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.
826
+
827
+ ```typescript
828
+ const { id, email, phone } = useUser();
829
+ ```
830
+
831
+ 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.
832
+
833
+ **Returns:**
834
+
835
+ | Property | Type | Description |
836
+ |----------|------|-------------|
837
+ | id | `string \| undefined` | The user id (`sub`) |
838
+ | email | `string \| undefined` | The user's email |
839
+ | phone | `string \| undefined` | The user's phone number |
840
+
841
+ Every field is `undefined` when no one is signed in.
842
+
843
+ ```tsx
844
+ const AccountMenu = () => {
845
+ const { isSignedIn, logout } = useAuth();
846
+ const { email } = useUser();
847
+ if (!isSignedIn) return null;
848
+ return <button onClick={() => void logout()}>Sign out {email}</button>;
849
+ };
850
+ ```
851
+
726
852
  ### usePassflow
727
853
  Hook for accessing the Passflow SDK instance. Must be used within PassflowProvider.
728
854
 
@@ -758,17 +884,46 @@ Hook for implementing sign-in functionality. Supports password, passkey, and pas
758
884
 
759
885
  ```typescript
760
886
  const { fetch, isLoading, isError, error } = useSignIn();
887
+
888
+ const outcome = await fetch({ email, password }, 'password');
889
+ if (outcome === 'two-factor-required') navigate('/2fa');
890
+ else if (outcome === 'signed-in') navigate('/');
761
891
  ```
762
892
 
893
+ **Options:**
894
+
895
+ | Option | Type | Description |
896
+ |--------|------|-------------|
897
+ | 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`. |
898
+
899
+ ```typescript
900
+ const { fetch } = useSignIn({
901
+ onPasswordChangeRequired: () => navigate('/forgot-password?default_method=email_or_username&reason=password_change_required'),
902
+ });
903
+ ```
904
+
905
+ 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.
906
+
763
907
  **Returns:**
764
908
 
765
909
  | Property | Type | Description |
766
910
  |----------|------|-------------|
767
- | fetch | `(payload: PassflowPasskeyAuthenticateStartPayload \| PassflowSignInPayload \| PassflowPasswordlessSignInPayload, type: 'passkey' \| 'password' \| 'passwordless') => Promise<boolean \| string \| PassflowPasswordlessResponse>` | Sign in function |
911
+ | fetch | `(payload: PassflowPasskeyAuthenticateStartPayload \| PassflowSignInPayload \| PassflowPasswordlessSignInPayload, type: 'passkey' \| 'password' \| 'passwordless') => Promise<false \| SignInOutcome \| PassflowPasswordlessResponse>` | Sign in function |
768
912
  | isLoading | `boolean` | Loading state |
769
913
  | isError | `boolean` | Error state |
770
914
  | error | `string` | Error message |
771
915
 
916
+ `fetch` resolves one of:
917
+
918
+ | Value | Meaning |
919
+ |-------|---------|
920
+ | `'signed-in'` | A password or passkey sign-in finished; the session is ready. |
921
+ | `'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. |
922
+ | `PassflowPasswordlessResponse` | The server's answer to a passwordless request. |
923
+ | `false` | The request failed (see `error`), was superseded, entered the emailed-code step, or was handed to `onPasswordChangeRequired`. |
924
+
925
+ 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.
926
+
772
927
  ### useSignUp
773
928
  Hook for implementing registration functionality. Supports password, passkey, and passwordless registration.
774
929
 
@@ -780,11 +935,13 @@ const { fetch, isLoading, isError, error } = useSignUp();
780
935
 
781
936
  | Property | Type | Description |
782
937
  |----------|------|-------------|
783
- | fetch | `(payload: PassflowPasskeyRegisterStartPayload \| PassflowSignUpPayload \| PassflowPasswordlessSignInPayload, type: 'passkey' \| 'password' \| 'passwordless') => Promise<boolean \| PassflowPasswordlessResponse>` | Sign up function |
938
+ | fetch | `(payload: PassflowPasskeyRegisterStartPayload \| PassflowSignUpPayload \| PassflowPasswordlessSignInPayload, type: 'passkey' \| 'password' \| 'passwordless') => Promise<false \| SignInOutcome \| PassflowPasswordlessResponse>` | Sign up function |
784
939
  | isLoading | `boolean` | Loading state |
785
940
  | isError | `boolean` | Error state |
786
941
  | error | `string` | Error message |
787
942
 
943
+ `fetch` resolves the same outcomes as `useSignIn().fetch`.
944
+
788
945
  ### useNavigation
789
946
  Hook for navigation between pages. Supports various routers (react-router, wouter, tanstack-router).
790
947
 
@@ -929,7 +1086,7 @@ const { fetch, isLoading, isError, error } = useLogout();
929
1086
 
930
1087
  | Property | Type | Description |
931
1088
  |----------|------|-------------|
932
- | fetch | `() => Promise<boolean>` | Logout function |
1089
+ | 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
1090
  | isLoading | `boolean` | Loading state |
934
1091
  | isError | `boolean` | Error state |
935
1092
  | error | `string` | Error message |