@baasix/sdk 0.2.0 → 0.2.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.
package/README.md CHANGED
@@ -202,6 +202,9 @@ const { user, token } = await baasix.auth.verifyMagicLink('verification-token');
202
202
 
203
203
  // Session type / tenant, so the tenant's web/mobile session limits apply
204
204
  await baasix.auth.verifyMagicLink(code, { authType: 'web', tenant_Id: 'tenant-uuid' });
205
+
206
+ // Emailed code + email: wrong codes are counted and the code is burned after 5 tries
207
+ await baasix.auth.verifyMagicLink(code, { email: 'user@example.com', authType: 'web' });
205
208
  ```
206
209
 
207
210
  ### Password Reset
@@ -221,6 +224,9 @@ await baasix.auth.forgotPassword({
221
224
 
222
225
  // Reset password with the token or emailed code
223
226
  await baasix.auth.resetPassword('reset-token-or-code', 'newpassword123');
227
+
228
+ // With an emailed code, pass the email too (5-try limit per email)
229
+ await baasix.auth.resetPassword('K7M2PQ9X', 'newSecurePassword', { email: 'user@example.com' });
224
230
  ```
225
231
 
226
232
  ### Change Password
@@ -1601,7 +1607,7 @@ try {
1601
1607
  }
1602
1608
  ```
1603
1609
 
1604
- `error.message` is the server's reason, read from `error.message`, a top-level `message`, or a plain-string `error` (`{ "error": "Authentication required" }`), and falls back to the HTTP status text. `error.code` comes from `error.code` or a top-level `code`. `error.details` is set only when the server sends an array of details.
1610
+ `error.message` is the server's reason, read from `error.message`, a top-level `message`, or a plain-string `error` (`{ "error": "Authentication required" }`), and falls back to the HTTP status text. `error.code` comes from `error.code` or a top-level `code`. `error.details` is always an array: a details array from the server is passed through, and a string or object detail is wrapped (`[{ message: "…" }]` or `[{ code, errors }]`).
1605
1611
 
1606
1612
  A **401** with code `INVALID_SESSION` means the session is gone (logged out, expired, revoked or suspended user). With `autoRefresh` on, the SDK tries one token refresh and retries the request; if that fails, sign the user in again. A **503** `AUTH_UNAVAILABLE` is temporary — retry.
1607
1613
 
package/dist/index.cjs CHANGED
@@ -195,7 +195,7 @@ var HttpClient = class {
195
195
  const message = errorObj?.message || errorData.message || (typeof errorData.error === "string" ? errorData.error : "") || response.statusText || "Request failed";
196
196
  const code = errorObj?.code ?? errorData.code;
197
197
  const rawDetails = errorObj?.details ?? errorData.details;
198
- const details = Array.isArray(rawDetails) ? rawDetails : void 0;
198
+ const details = rawDetails == null ? void 0 : Array.isArray(rawDetails) ? rawDetails : typeof rawDetails === "object" ? [rawDetails] : [{ message: String(rawDetails) }];
199
199
  return new BaasixError(message, response.status, code, details);
200
200
  }
201
201
  /**
@@ -904,12 +904,20 @@ var AuthModule = class {
904
904
  *
905
905
  * // Enforce the tenant's web/mobile session limits for this sign-in
906
906
  * await baasix.auth.verifyMagicLink(code, { authType: 'web', tenant_Id });
907
+ *
908
+ * // Emailed code + email: wrong codes are counted and the code is burned after 5
909
+ * await baasix.auth.verifyMagicLink(code, { email: 'user@example.com', authType: 'web' });
907
910
  * ```
908
911
  */
909
912
  async verifyMagicLink(token, opts) {
910
- const response = await this.client.get(
913
+ const { email, ...params } = opts || {};
914
+ const response = email ? await this.client.post(
915
+ "/auth/magiclink/verify",
916
+ { email, code: token, ...params },
917
+ { skipAuth: true }
918
+ ) : await this.client.get(
911
919
  `/auth/magiclink/${encodeURIComponent(token)}`,
912
- { skipAuth: true, params: opts }
920
+ { skipAuth: true, params }
913
921
  );
914
922
  await this.storeTokens(response);
915
923
  this.emitAuthStateChange("SIGNED_IN", response.user);
@@ -950,9 +958,20 @@ var AuthModule = class {
950
958
  * @example
951
959
  * ```typescript
952
960
  * await baasix.auth.resetPassword('reset-token-or-code', 'newpassword123');
961
+ *
962
+ * // Emailed code + email: wrong codes are counted and the code is burned after 5
963
+ * await baasix.auth.resetPassword('K7M2PQ9X', 'newpassword123', { email: 'user@example.com' });
953
964
  * ```
954
965
  */
955
- async resetPassword(token, newPassword) {
966
+ async resetPassword(token, newPassword, opts) {
967
+ if (opts?.email) {
968
+ await this.client.post(
969
+ "/auth/password/reset/verify",
970
+ { email: opts.email, code: token, password: newPassword },
971
+ { skipAuth: true }
972
+ );
973
+ return;
974
+ }
956
975
  await this.client.post(
957
976
  `/auth/password/reset/${encodeURIComponent(token)}`,
958
977
  { password: newPassword },