@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.cjs CHANGED
@@ -1207,13 +1207,26 @@ function readDeviceIdFromClaims(claims) {
1207
1207
  return typeof dev === "string" && dev.length > 0 ? dev : void 0;
1208
1208
  }
1209
1209
  function createKoraAuthSync(options) {
1210
- const { authClient, schema, scopeFromClaims } = options;
1210
+ const { authClient, schema, scopeFromClaims, anonymous = "suspend" } = options;
1211
1211
  const binding = {
1212
1212
  auth: async () => {
1213
1213
  const token = await authClient.getAccessToken();
1214
1214
  return { token: token ?? "" };
1215
1215
  }
1216
1216
  };
1217
+ binding.resolveSyncState = async () => {
1218
+ if (authClient.state === "loading") return { state: "loading" };
1219
+ const token = await authClient.getAccessToken();
1220
+ if (!token) {
1221
+ return anonymous === "allow" ? { state: "anonymous", mayConnectAnonymously: true } : { state: "signed-out", mayConnectAnonymously: false };
1222
+ }
1223
+ const claims = decodeJwtPayload2(token);
1224
+ const userId = typeof claims?.sub === "string" ? claims.sub : "";
1225
+ if (!userId) {
1226
+ return anonymous === "allow" ? { state: "anonymous", mayConnectAnonymously: true } : { state: "signed-out", mayConnectAnonymously: false };
1227
+ }
1228
+ return { state: "authenticated", userId, token };
1229
+ };
1217
1230
  if (schema) {
1218
1231
  binding.resolveScopeMap = async () => {
1219
1232
  const token = await authClient.getAccessToken();
@@ -1239,6 +1252,17 @@ function createKoraAuthSync(options) {
1239
1252
  }
1240
1253
  return readDeviceIdFromClaims(claims);
1241
1254
  };
1255
+ binding.resolveUserId = async () => {
1256
+ const token = await authClient.getAccessToken();
1257
+ if (!token) {
1258
+ return void 0;
1259
+ }
1260
+ const claims = decodeJwtPayload2(token);
1261
+ if (!claims || typeof claims.sub !== "string" || claims.sub.length === 0) {
1262
+ return void 0;
1263
+ }
1264
+ return claims.sub;
1265
+ };
1242
1266
  if (authClient.onAuthChange) {
1243
1267
  binding.subscribe = (listener) => authClient.onAuthChange?.(() => listener()) ?? (() => {
1244
1268
  });