@spacelr/sdk 0.12.0 → 0.13.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/dist/index.mjs CHANGED
@@ -1824,13 +1824,14 @@ function mapProfile(u) {
1824
1824
  const name = u.name ?? ([u.firstName, u.lastName].filter(Boolean).join(" ") || void 0) ?? u.displayName;
1825
1825
  return {
1826
1826
  id: u.id ?? u._id ?? u.userId ?? u.sub ?? "",
1827
- email: u.email ?? "",
1827
+ // Honest null for email-less (passkey-only) accounts (#780), not a '' coercion.
1828
+ email: u.email ?? null,
1828
1829
  username: u.username,
1829
1830
  name,
1830
1831
  roles: u.roles ?? []
1831
1832
  };
1832
1833
  }
1833
- var AuthModule = class {
1834
+ var AuthModule = class _AuthModule {
1834
1835
  constructor(http, tokenManager, config) {
1835
1836
  this.stateListeners = /* @__PURE__ */ new Set();
1836
1837
  this.lastEmittedState = null;
@@ -2146,6 +2147,82 @@ var AuthModule = class {
2146
2147
  await this.storeTokensFromLogin(result);
2147
2148
  return result;
2148
2149
  }
2150
+ /**
2151
+ * Fetch WebAuthn attestation options to begin an email-less passkey SIGNUP
2152
+ * (#780) — creating a brand-new account with no email and no password. The
2153
+ * project must have passkeys AND public registration enabled. No account is
2154
+ * created until {@link verifyPasskeySignup} succeeds.
2155
+ *
2156
+ * IMPORTANT (identity routing, B8): a signup ceremony always mints a NEW
2157
+ * account. Before offering signup, the app MUST first attempt a discoverable
2158
+ * passkey login ({@link beginPasskeyLogin} with no email); only offer signup
2159
+ * when the user has no existing passkey. Skipping this creates duplicate,
2160
+ * fragmented accounts. {@link createUserWithPasskey} bundles that fallback.
2161
+ */
2162
+ async beginPasskeySignup(displayName) {
2163
+ return this.http.request({
2164
+ method: "POST",
2165
+ path: "/auth/passkey/signup/options",
2166
+ body: displayName ? { displayName } : {}
2167
+ });
2168
+ }
2169
+ /**
2170
+ * Verify a WebAuthn attestation produced by `navigator.credentials.create()`
2171
+ * against the options from {@link beginPasskeySignup}, materializing the
2172
+ * email-less account and returning JWT tokens (the caller is now logged in).
2173
+ */
2174
+ async verifyPasskeySignup(response) {
2175
+ const result = await this.http.request({
2176
+ method: "POST",
2177
+ path: "/auth/passkey/signup/verify",
2178
+ body: { response }
2179
+ });
2180
+ await this.storeTokensFromLogin(result);
2181
+ return result;
2182
+ }
2183
+ /**
2184
+ * One-call email-less passkey signup with the B1 idempotency fallback baked
2185
+ * in. The browser WebAuthn functions are injected (the SDK stays transport-
2186
+ * only and does not depend on `@simplewebauthn/browser`):
2187
+ *
2188
+ * 1. `beginPasskeySignup` → `startRegistration` → `verifyPasskeySignup`.
2189
+ * 2. If verify fails with `challenge_expired`, a network/timeout error, or
2190
+ * `passkey_already_registered` (the account was in fact created but the
2191
+ * response was lost, or the passkey already exists), fall back to a fresh
2192
+ * discoverable LOGIN ceremony and return that session. That fallback IS the
2193
+ * idempotency mechanism — the server never mints a session from a bare
2194
+ * credential lookup.
2195
+ * 3. If the login fallback also fails (genuinely no credential), the original
2196
+ * error is surfaced so the app can restart signup.
2197
+ */
2198
+ async createUserWithPasskey(ceremony) {
2199
+ const options = await this.beginPasskeySignup(ceremony.displayName);
2200
+ const attestation = await ceremony.startRegistration(options);
2201
+ try {
2202
+ return await this.verifyPasskeySignup(attestation);
2203
+ } catch (error) {
2204
+ if (!_AuthModule.isSignupFallbackError(error)) {
2205
+ throw error;
2206
+ }
2207
+ try {
2208
+ const loginOptions = await this.beginPasskeyLogin();
2209
+ const assertion = await ceremony.startAuthentication(loginOptions);
2210
+ return await this.verifyPasskeyLogin(assertion);
2211
+ } catch {
2212
+ throw error;
2213
+ }
2214
+ }
2215
+ }
2216
+ static isSignupFallbackError(error) {
2217
+ if (error instanceof SpacelrNetworkError || error instanceof SpacelrTimeoutError) {
2218
+ return true;
2219
+ }
2220
+ if (error instanceof SpacelrError) {
2221
+ const message = (error.message || "").toLowerCase();
2222
+ return error.statusCode === 409 || message.includes("challenge_expired") || message.includes("already_registered");
2223
+ }
2224
+ return false;
2225
+ }
2149
2226
  /**
2150
2227
  * Fetch WebAuthn attestation options to register a new passkey for the
2151
2228
  * currently authenticated user.