@polaris-auth/client 0.7.0 → 0.9.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
@@ -3,7 +3,8 @@
3
3
  The TypeScript client for [Polaris for PHP](https://github.com/univeros/polaris-core), generated from
4
4
  the route manifest: `openapi-fetch` typed by the 52 core endpoints and the routes of the `polaris/audit`,
5
5
  `polaris/admin`, `polaris/sentinel`, `polaris/sso`, `polaris/scim`, `polaris/passwordless`,
6
- `polaris/username`, `polaris/anonymous` and `polaris/multi-session` plugins, so a request body, its
6
+ `polaris/username`, `polaris/anonymous`, `polaris/multi-session`, `polaris/social` and `polaris/passkey`
7
+ plugins, so a request body, its
7
8
  `data` and its `error` are checked at compile time against the same contract the PHP hosts serve.
8
9
 
9
10
  ```sh
@@ -50,8 +51,10 @@ connections, the operators' list, and the SCIM server's own routes for a directo
50
51
  `client.passwordless` (`magicLinkSend`, `magicLinkVerify`, `magicLinkExchange`, `emailOtpSend`,
51
52
  `emailOtpVerify`, `emailOtpVerifyEmail`, `emailOtpResetPassword`, `phoneSend`, `phoneVerify`, `phoneAdd`,
52
53
  `phoneConfirm`, `oneTimeTokenGenerate`, `oneTimeTokenVerify`), `client.username` (`signIn`, `update`),
53
- `client.anonymous` (`signIn`, `convert`) and `client.multiSession` (`list`, `switch`, `revoke`,
54
- `lastMethod`) are generated into `src/<plugin id>.ts` by `scripts/generate-namespaces.mjs` from the
54
+ `client.anonymous` (`signIn`, `convert`), `client.multiSession` (`list`, `switch`, `revoke`,
55
+ `lastMethod`), `client.social` (`start`, `exchange`, `oneTap`, `accounts`, `link`, `unlink`, `token`) and
56
+ `client.passkey` (`registerOptions`, `registerVerify`, `authenticateOptions`, `authenticateVerify`, `list`,
57
+ `rename`, `delete`) are generated into `src/<plugin id>.ts` by `scripts/generate-namespaces.mjs` from the
55
58
  `x-polaris-plugin` marker of the OpenAPI document; a later plugin gets its namespace the same way, its id
56
59
  camel-cased (`multi-session` is `client.multiSession`). Their errors are RFC 9457 problem documents,
57
60
  `ProblemBody` (`{ type, title, status, detail, error, message, errors? }`), served as
@@ -61,6 +64,38 @@ Response types come from each endpoint's `output.example` in the manifest (JSON
61
64
  `docs/adapters/spec.md` §7): a documented approximation, exact for the shapes the specs show. Error
62
65
  bodies are `ErrorBody` (`{ error, message }`) and `ValidationErrorBody` (`{ errors }`).
63
66
 
67
+ ## Social sign-in
68
+
69
+ `client.social.start()` answers the provider's URL and a `state`; keep the state (`sessionStorage`), send
70
+ the browser to the URL, and on the page the sign-in ends on compare the `state` in the query with it
71
+ before exchanging the `code`:
72
+
73
+ ```ts
74
+ const { data } = await polaris.social.start({ params: { path: { provider: "google" } }, body: {} });
75
+ sessionStorage.setItem("polaris.social.state", data!.data.state);
76
+ location.assign(data!.data.url);
77
+ // ...on /signed-in:
78
+ const query = new URLSearchParams(location.search);
79
+ if (query.get("state") !== sessionStorage.getItem("polaris.social.state")) throw new Error("not the flow this browser started");
80
+ const session = await polaris.social.exchange({ body: { code: query.get("code")! } });
81
+ ```
82
+
83
+ ## Passkeys
84
+
85
+ `registerPasskey()`, `signInWithPasskey()` and `passkeyAssertion()` run the WebAuthn ceremonies of
86
+ `polaris/passkey` in the browser (`navigator.credentials`, a secure context) over the client's routes;
87
+ they are the only hand-written code besides the sentinel retry.
88
+
89
+ ```ts
90
+ import { createClient, registerPasskey, signInWithPasskey, passkeyAssertion } from "@polaris-auth/client";
91
+
92
+ const { passkey, recovery_codes } = await registerPasskey(polaris.withToken(accessToken), "MacBook");
93
+ const session = await signInWithPasskey(polaris); // a prompt
94
+ const autofilled = await signInWithPasskey(polaris, { conditional: true }); // the form's autofill (autocomplete="username webauthn")
95
+ // The passkey as the second factor of a password login that answered mfa_required:
96
+ await polaris.withToken(mfaToken).POST("/auth/mfa/verify", { body: { factor_id, code: await passkeyAssertion(polaris) } });
97
+ ```
98
+
64
99
  ## Several accounts on one device
65
100
 
66
101
  With `polaris/multi-session`, every response that carries a token pair also carries the device id in