@passflow/core 0.21.0 → 0.22.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.
package/README.md CHANGED
@@ -33,6 +33,7 @@
33
33
  - [Federated Authentication](#federated-authentication)
34
34
  - [Passkey Authentication](#passkey-authentication)
35
35
  - [Password Reset](#password-reset)
36
+ - [Email verification](#email-verification)
36
37
  - [Token Management](#token-management)
37
38
  - [Tenant Management](#tenant-management)
38
39
  - [Invitation Management](#invitation-management)
@@ -463,7 +464,8 @@ const sendResetEmail = async () => {
463
464
  // Reset password (after receiving reset token)
464
465
  const resetPassword = async (newPassword) => {
465
466
  try {
466
- // The token should be in the URL query parameters
467
+ // The token is read from the URL query, or from the query of a hash route
468
+ // (#/reset?token=...). Pass it as the third argument when it lives elsewhere.
467
469
  const response = await passflow.resetPassword(newPassword);
468
470
  console.log("Password reset successful", response);
469
471
  } catch (error) {
@@ -472,6 +474,59 @@ const resetPassword = async (newPassword) => {
472
474
  };
473
475
  ```
474
476
 
477
+ ### Email verification
478
+
479
+ When the server needs the user's email proven first, `signIn`, `signUp` and `resetPassword` answer
480
+ with an email verification step instead of a session. They do not throw: the step comes back as the
481
+ response, no tokens are stored, and the SDK emits `PassflowEvent.EmailVerificationRequired`.
482
+
483
+ ```typescript
484
+ type PassflowEmailVerificationStep = {
485
+ email_verification_required: true;
486
+ message: string; // e.g. "We sent a code to a***@example.com."
487
+ challenge_id?: string; // present when a code was emailed
488
+ };
489
+ ```
490
+
491
+ Check the answer with `isEmailVerificationStep`. With a `challenge_id`, ask the user for the code
492
+ from the email and finish with `verifyEmailCode({ challenge_id, otp })`:
493
+
494
+ ```typescript
495
+ import { isEmailVerificationStep } from "@passflow/core";
496
+
497
+ const response = await passflow.signUp({
498
+ user: { email: "user@example.com", password: "password123" },
499
+ });
500
+
501
+ if (isEmailVerificationStep(response)) {
502
+ showMessage(response.message);
503
+ if (response.challenge_id) {
504
+ const otp = await askUserForCode();
505
+ const session = await passflow.verifyEmailCode({
506
+ challenge_id: response.challenge_id,
507
+ otp,
508
+ });
509
+ // session holds a new token pair, or a tfa_token when a second factor is owed
510
+ }
511
+ }
512
+ ```
513
+
514
+ `verifyEmailCode` answers like `signIn`: a new token pair, or a `tfa_token` when a second factor is
515
+ owed (`isTwoFactorVerificationRequired()` is then true and `TwoFactorRequired` is emitted). A wrong,
516
+ expired or spent code throws a `PassflowError` with an `error.otp.*` id, and an unknown challenge
517
+ throws `error.not.found`. To send a fresh code, call the original method again; the new step has a
518
+ new `challenge_id`.
519
+
520
+ - **Register or SignIn.** `verifyEmailCode` emits `Register` for a challenge this `Passflow`
521
+ instance got from `signUp`, and `SignIn` for one from `signIn` or `resetPassword`. Challenges are
522
+ remembered in memory only, so a code entered in another tab, after a reload, or on a recreated
523
+ instance emits `SignIn`, even for a sign-up. It emits no `SignInStart` or `RegisterStart`.
524
+ - **No `challenge_id`.** The code budget is spent and the server emailed a link instead. There is
525
+ nothing to call: show `message` and let the user follow the link.
526
+ - **Password reset.** `resetPassword(newPassword, scopes?, resetToken?)` takes the reset token as
527
+ an optional third argument. Without it, the SDK reads `token` from the URL query, then from the
528
+ query of a hash route (`#/reset?token=...`).
529
+
475
530
  ## Token Management
476
531
 
477
532
  ```javascript
@@ -658,6 +713,12 @@ const subscriber = {
658
713
  case "refresh":
659
714
  // Handle token refresh
660
715
  break;
716
+ case "email-verification:required":
717
+ // signIn, signUp or resetPassword is waiting for the email to be proven.
718
+ // source: { email?, challengeId?, operation: "sign-up" | "sign-in" | "reset-password" }
719
+ // challengeId is set when a code was emailed (finish with verifyEmailCode),
720
+ // and absent when a link was emailed instead. email is set when the SDK knows it.
721
+ break;
661
722
  }
662
723
  },
663
724
  };
@@ -736,7 +797,8 @@ Configuration options:
736
797
  | `passkeyRegister(payload)` | Register a new passkey |
737
798
  | `passkeyAuthenticate(payload)` | Authenticate with a passkey |
738
799
  | `sendPasswordResetEmail(payload)` | Send password reset email |
739
- | `resetPassword(newPassword, scopes)` | Reset password |
800
+ | `resetPassword(newPassword, scopes?, resetToken?)` | Reset password |
801
+ | `verifyEmailCode({ challenge_id, otp })` | Finish an email verification step |
740
802
 
741
803
  **Token Methods**
742
804
 
@@ -889,6 +951,7 @@ enum PassflowEvent {
889
951
  RefreshRecovered = "refresh:recovered",
890
952
  SessionExpired = "session:expired",
891
953
  TokenCacheExpired = "token-cache-expired",
954
+ EmailVerificationRequired = "email-verification:required",
892
955
  // ...and the sign-in, 2FA and session events; see lib/store.ts
893
956
  }
894
957
  ```