@korajs/auth 0.3.3 → 0.4.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.js CHANGED
@@ -23,7 +23,7 @@ import {
23
23
  signChallenge,
24
24
  toBase64Url,
25
25
  verifyChallenge
26
- } from "./chunk-FSU4SK32.js";
26
+ } from "./chunk-IO2MCCG2.js";
27
27
 
28
28
  // src/client/auth-client.ts
29
29
  import { KoraError } from "@korajs/core";
@@ -49,18 +49,18 @@ function decodeJwtPayload(token) {
49
49
  }
50
50
  function isTokenExpired(token) {
51
51
  const payload = decodeJwtPayload(token);
52
- if (!payload || typeof payload["exp"] !== "number") {
52
+ if (!payload || typeof payload.exp !== "number") {
53
53
  return true;
54
54
  }
55
55
  const nowSeconds = Math.floor(Date.now() / 1e3);
56
- return payload["exp"] <= nowSeconds + EXPIRY_BUFFER_SECONDS;
56
+ return payload.exp <= nowSeconds + EXPIRY_BUFFER_SECONDS;
57
57
  }
58
58
  function getUserIdFromToken(token) {
59
59
  const payload = decodeJwtPayload(token);
60
- if (!payload || typeof payload["sub"] !== "string") {
60
+ if (!payload || typeof payload.sub !== "string") {
61
61
  return null;
62
62
  }
63
- return payload["sub"];
63
+ return payload.sub;
64
64
  }
65
65
  function createTokenStorage(prefix) {
66
66
  let useLocalStorage = false;
@@ -408,7 +408,7 @@ var AuthClient = class {
408
408
  headers["Content-Type"] = "application/json";
409
409
  }
410
410
  if (options.token) {
411
- headers["Authorization"] = `Bearer ${options.token}`;
411
+ headers.Authorization = `Bearer ${options.token}`;
412
412
  }
413
413
  let response;
414
414
  try {
@@ -429,23 +429,23 @@ var AuthClient = class {
429
429
  let serverError;
430
430
  try {
431
431
  const body = await response.json();
432
- if (typeof body["error"] === "string") {
433
- errorMessage = body["error"];
432
+ if (typeof body.error === "string") {
433
+ errorMessage = body.error;
434
434
  serverError = errorMessage;
435
- } else if (typeof body["message"] === "string") {
436
- errorMessage = body["message"];
435
+ } else if (typeof body.message === "string") {
436
+ errorMessage = body.message;
437
437
  serverError = errorMessage;
438
438
  }
439
439
  } catch {
440
440
  }
441
- throw new AuthError(
442
- errorMessage,
443
- "AUTH_SERVER_ERROR",
444
- { path, status: response.status, serverError }
445
- );
441
+ throw new AuthError(errorMessage, "AUTH_SERVER_ERROR", {
442
+ path,
443
+ status: response.status,
444
+ serverError
445
+ });
446
446
  }
447
447
  const json = await response.json();
448
- const data = json["data"] !== void 0 ? json["data"] : json;
448
+ const data = json.data !== void 0 ? json.data : json;
449
449
  return data;
450
450
  }
451
451
  };
@@ -631,10 +631,9 @@ var OrgClient = class {
631
631
  * List pending invitations for the current user's email.
632
632
  */
633
633
  async listMyInvitations(email) {
634
- return this.request(
635
- `/invitations?email=${encodeURIComponent(email)}`,
636
- { method: "GET" }
637
- );
634
+ return this.request(`/invitations?email=${encodeURIComponent(email)}`, {
635
+ method: "GET"
636
+ });
638
637
  }
639
638
  // --- Subscriptions ---
640
639
  /**
@@ -679,18 +678,17 @@ var OrgClient = class {
679
678
  body: options.body ? JSON.stringify(options.body) : void 0
680
679
  });
681
680
  } catch (cause) {
682
- throw new OrgClientError(
683
- `Network request to ${path} failed.`,
684
- "ORG_NETWORK_ERROR",
685
- { path, cause: cause instanceof Error ? cause.message : String(cause) }
686
- );
681
+ throw new OrgClientError(`Network request to ${path} failed.`, "ORG_NETWORK_ERROR", {
682
+ path,
683
+ cause: cause instanceof Error ? cause.message : String(cause)
684
+ });
687
685
  }
688
686
  if (!response.ok) {
689
687
  let errorMessage = `Server returned HTTP ${response.status}`;
690
688
  try {
691
689
  const body = await response.json();
692
- if (typeof body["error"] === "string") {
693
- errorMessage = body["error"];
690
+ if (typeof body.error === "string") {
691
+ errorMessage = body.error;
694
692
  }
695
693
  } catch {
696
694
  }
@@ -763,10 +761,9 @@ var IndexedDBDeviceKeyStore = class {
763
761
  request.onerror = () => {
764
762
  this.dbPromise = null;
765
763
  reject(
766
- new DeviceKeyStoreError(
767
- "Failed to open IndexedDB database for device key storage.",
768
- { error: request.error?.message }
769
- )
764
+ new DeviceKeyStoreError("Failed to open IndexedDB database for device key storage.", {
765
+ error: request.error?.message
766
+ })
770
767
  );
771
768
  };
772
769
  request.onblocked = () => {
@@ -793,21 +790,18 @@ var IndexedDBDeviceKeyStore = class {
793
790
  };
794
791
  tx.onerror = () => {
795
792
  reject(
796
- new DeviceKeyStoreError(
797
- `Failed to save key pair for device "${deviceId}".`,
798
- { deviceId, error: tx.error?.message }
799
- )
793
+ new DeviceKeyStoreError(`Failed to save key pair for device "${deviceId}".`, {
794
+ deviceId,
795
+ error: tx.error?.message
796
+ })
800
797
  );
801
798
  };
802
799
  } catch (cause) {
803
800
  reject(
804
- new DeviceKeyStoreError(
805
- `Failed to save key pair for device "${deviceId}".`,
806
- {
807
- deviceId,
808
- cause: cause instanceof Error ? cause.message : String(cause)
809
- }
810
- )
801
+ new DeviceKeyStoreError(`Failed to save key pair for device "${deviceId}".`, {
802
+ deviceId,
803
+ cause: cause instanceof Error ? cause.message : String(cause)
804
+ })
811
805
  );
812
806
  }
813
807
  });
@@ -826,21 +820,18 @@ var IndexedDBDeviceKeyStore = class {
826
820
  };
827
821
  request.onerror = () => {
828
822
  reject(
829
- new DeviceKeyStoreError(
830
- `Failed to load key pair for device "${deviceId}".`,
831
- { deviceId, error: request.error?.message }
832
- )
823
+ new DeviceKeyStoreError(`Failed to load key pair for device "${deviceId}".`, {
824
+ deviceId,
825
+ error: request.error?.message
826
+ })
833
827
  );
834
828
  };
835
829
  } catch (cause) {
836
830
  reject(
837
- new DeviceKeyStoreError(
838
- `Failed to load key pair for device "${deviceId}".`,
839
- {
840
- deviceId,
841
- cause: cause instanceof Error ? cause.message : String(cause)
842
- }
843
- )
831
+ new DeviceKeyStoreError(`Failed to load key pair for device "${deviceId}".`, {
832
+ deviceId,
833
+ cause: cause instanceof Error ? cause.message : String(cause)
834
+ })
844
835
  );
845
836
  }
846
837
  });
@@ -858,21 +849,18 @@ var IndexedDBDeviceKeyStore = class {
858
849
  };
859
850
  tx.onerror = () => {
860
851
  reject(
861
- new DeviceKeyStoreError(
862
- `Failed to delete key pair for device "${deviceId}".`,
863
- { deviceId, error: tx.error?.message }
864
- )
852
+ new DeviceKeyStoreError(`Failed to delete key pair for device "${deviceId}".`, {
853
+ deviceId,
854
+ error: tx.error?.message
855
+ })
865
856
  );
866
857
  };
867
858
  } catch (cause) {
868
859
  reject(
869
- new DeviceKeyStoreError(
870
- `Failed to delete key pair for device "${deviceId}".`,
871
- {
872
- deviceId,
873
- cause: cause instanceof Error ? cause.message : String(cause)
874
- }
875
- )
860
+ new DeviceKeyStoreError(`Failed to delete key pair for device "${deviceId}".`, {
861
+ deviceId,
862
+ cause: cause instanceof Error ? cause.message : String(cause)
863
+ })
876
864
  );
877
865
  }
878
866
  });
@@ -898,13 +886,10 @@ var IndexedDBDeviceKeyStore = class {
898
886
  };
899
887
  } catch (cause) {
900
888
  reject(
901
- new DeviceKeyStoreError(
902
- `Failed to check if key pair exists for device "${deviceId}".`,
903
- {
904
- deviceId,
905
- cause: cause instanceof Error ? cause.message : String(cause)
906
- }
907
- )
889
+ new DeviceKeyStoreError(`Failed to check if key pair exists for device "${deviceId}".`, {
890
+ deviceId,
891
+ cause: cause instanceof Error ? cause.message : String(cause)
892
+ })
908
893
  );
909
894
  }
910
895
  });
@@ -1011,15 +996,15 @@ var TokenStore = class {
1011
996
  return null;
1012
997
  }
1013
998
  const record = parsed;
1014
- if (typeof record["accessToken"] !== "string" || typeof record["refreshToken"] !== "string") {
999
+ if (typeof record.accessToken !== "string" || typeof record.refreshToken !== "string") {
1015
1000
  return null;
1016
1001
  }
1017
1002
  const tokens = {
1018
- accessToken: record["accessToken"],
1019
- refreshToken: record["refreshToken"]
1003
+ accessToken: record.accessToken,
1004
+ refreshToken: record.refreshToken
1020
1005
  };
1021
- if (typeof record["deviceCredential"] === "string") {
1022
- tokens.deviceCredential = record["deviceCredential"];
1006
+ if (typeof record.deviceCredential === "string") {
1007
+ tokens.deviceCredential = record.deviceCredential;
1023
1008
  }
1024
1009
  return tokens;
1025
1010
  } catch {
@@ -1191,11 +1176,11 @@ var EncryptedTokenStore = class {
1191
1176
  return null;
1192
1177
  }
1193
1178
  const record = parsed;
1194
- if (typeof record["iv"] !== "string" || typeof record["data"] !== "string") {
1179
+ if (typeof record.iv !== "string" || typeof record.data !== "string") {
1195
1180
  return null;
1196
1181
  }
1197
- const iv = fromBase64Url2(record["iv"]);
1198
- const ciphertext = fromBase64Url2(record["data"]);
1182
+ const iv = fromBase64Url2(record.iv);
1183
+ const ciphertext = fromBase64Url2(record.data);
1199
1184
  const plaintextBytes = await decryptData(this.key, ciphertext, iv);
1200
1185
  const json = new TextDecoder().decode(plaintextBytes);
1201
1186
  const tokenData = JSON.parse(json);
@@ -1203,15 +1188,15 @@ var EncryptedTokenStore = class {
1203
1188
  return null;
1204
1189
  }
1205
1190
  const tokenRecord = tokenData;
1206
- if (typeof tokenRecord["accessToken"] !== "string" || typeof tokenRecord["refreshToken"] !== "string") {
1191
+ if (typeof tokenRecord.accessToken !== "string" || typeof tokenRecord.refreshToken !== "string") {
1207
1192
  return null;
1208
1193
  }
1209
1194
  const tokens = {
1210
- accessToken: tokenRecord["accessToken"],
1211
- refreshToken: tokenRecord["refreshToken"]
1195
+ accessToken: tokenRecord.accessToken,
1196
+ refreshToken: tokenRecord.refreshToken
1212
1197
  };
1213
- if (typeof tokenRecord["deviceCredential"] === "string") {
1214
- tokens.deviceCredential = tokenRecord["deviceCredential"];
1198
+ if (typeof tokenRecord.deviceCredential === "string") {
1199
+ tokens.deviceCredential = tokenRecord.deviceCredential;
1215
1200
  }
1216
1201
  return tokens;
1217
1202
  } catch {