@korajs/auth 1.0.0-beta.1 → 1.0.0-beta.11

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.d.cts CHANGED
@@ -43,6 +43,7 @@ declare function createKoraAuth(options: CreateKoraAuthOptions): AuthClient;
43
43
  */
44
44
  interface AuthSyncClient {
45
45
  getAccessToken(): Promise<string | null>;
46
+ readonly state?: AuthState;
46
47
  onAuthChange?(callback: (state: AuthState) => void): () => void;
47
48
  }
48
49
  /**
@@ -68,6 +69,8 @@ interface CreateKoraAuthSyncOptions {
68
69
  * Defaults to {@link extractScopeValuesFromClaims}.
69
70
  */
70
71
  scopeFromClaims?: (claims: Record<string, unknown>) => Record<string, unknown>;
72
+ /** Signed-out behavior. Authenticated-only sync is suspended by default. */
73
+ anonymous?: 'suspend' | 'allow';
71
74
  }
72
75
  /**
73
76
  * Creates a sync auth binding for `createApp({ sync: { authClient: binding } })`.
package/dist/index.d.ts CHANGED
@@ -43,6 +43,7 @@ declare function createKoraAuth(options: CreateKoraAuthOptions): AuthClient;
43
43
  */
44
44
  interface AuthSyncClient {
45
45
  getAccessToken(): Promise<string | null>;
46
+ readonly state?: AuthState;
46
47
  onAuthChange?(callback: (state: AuthState) => void): () => void;
47
48
  }
48
49
  /**
@@ -68,6 +69,8 @@ interface CreateKoraAuthSyncOptions {
68
69
  * Defaults to {@link extractScopeValuesFromClaims}.
69
70
  */
70
71
  scopeFromClaims?: (claims: Record<string, unknown>) => Record<string, unknown>;
72
+ /** Signed-out behavior. Authenticated-only sync is suspended by default. */
73
+ anonymous?: 'suspend' | 'allow';
71
74
  }
72
75
  /**
73
76
  * Creates a sync auth binding for `createApp({ sync: { authClient: binding } })`.
package/dist/index.js CHANGED
@@ -1007,13 +1007,26 @@ function readDeviceIdFromClaims(claims) {
1007
1007
  return typeof dev === "string" && dev.length > 0 ? dev : void 0;
1008
1008
  }
1009
1009
  function createKoraAuthSync(options) {
1010
- const { authClient, schema, scopeFromClaims } = options;
1010
+ const { authClient, schema, scopeFromClaims, anonymous = "suspend" } = options;
1011
1011
  const binding = {
1012
1012
  auth: async () => {
1013
1013
  const token = await authClient.getAccessToken();
1014
1014
  return { token: token ?? "" };
1015
1015
  }
1016
1016
  };
1017
+ binding.resolveSyncState = async () => {
1018
+ if (authClient.state === "loading") return { state: "loading" };
1019
+ const token = await authClient.getAccessToken();
1020
+ if (!token) {
1021
+ return anonymous === "allow" ? { state: "anonymous", mayConnectAnonymously: true } : { state: "signed-out", mayConnectAnonymously: false };
1022
+ }
1023
+ const claims = decodeJwtPayload2(token);
1024
+ const userId = typeof claims?.sub === "string" ? claims.sub : "";
1025
+ if (!userId) {
1026
+ return anonymous === "allow" ? { state: "anonymous", mayConnectAnonymously: true } : { state: "signed-out", mayConnectAnonymously: false };
1027
+ }
1028
+ return { state: "authenticated", userId, token };
1029
+ };
1017
1030
  if (schema) {
1018
1031
  binding.resolveScopeMap = async () => {
1019
1032
  const token = await authClient.getAccessToken();
@@ -1039,6 +1052,17 @@ function createKoraAuthSync(options) {
1039
1052
  }
1040
1053
  return readDeviceIdFromClaims(claims);
1041
1054
  };
1055
+ binding.resolveUserId = async () => {
1056
+ const token = await authClient.getAccessToken();
1057
+ if (!token) {
1058
+ return void 0;
1059
+ }
1060
+ const claims = decodeJwtPayload2(token);
1061
+ if (!claims || typeof claims.sub !== "string" || claims.sub.length === 0) {
1062
+ return void 0;
1063
+ }
1064
+ return claims.sub;
1065
+ };
1042
1066
  if (authClient.onAuthChange) {
1043
1067
  binding.subscribe = (listener) => authClient.onAuthChange?.(() => listener()) ?? (() => {
1044
1068
  });