@absolutejs/auth 0.14.8 → 0.15.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/dist/index.js CHANGED
@@ -2108,21 +2108,21 @@ var authorize = ({
2108
2108
  onAuthorizeSuccess,
2109
2109
  onAuthorizeError
2110
2110
  }) => new Elysia().get(authorizeRoute, async ({
2111
- error,
2111
+ status,
2112
2112
  redirect,
2113
2113
  cookie: { state, code_verifier, auth_provider, origin_url },
2114
2114
  params: { provider },
2115
2115
  headers
2116
2116
  }) => {
2117
2117
  if (auth_provider === undefined || origin_url === undefined || state === undefined || code_verifier === undefined)
2118
- return error("Bad Request", "Cookies are missing");
2118
+ return status("Bad Request", "Cookies are missing");
2119
2119
  if (provider === undefined)
2120
- return error("Bad Request", "Provider is required");
2120
+ return status("Bad Request", "Provider is required");
2121
2121
  if (!isValidProviderOption(provider))
2122
- return error("Bad Request", "Invalid provider");
2122
+ return status("Bad Request", "Invalid provider");
2123
2123
  const providerConfig = clientProviders[provider];
2124
2124
  if (!providerConfig)
2125
- return error("Unauthorized", "Client provider not found");
2125
+ return status("Unauthorized", "Client provider not found");
2126
2126
  const { providerInstance, scope, searchParams } = providerConfig;
2127
2127
  const referer = headers["referer"] ?? "/";
2128
2128
  origin_url.set({
@@ -2175,9 +2175,9 @@ var authorize = ({
2175
2175
  error: err
2176
2176
  });
2177
2177
  if (err instanceof Error) {
2178
- return error("Internal Server Error", `${err.message} - ${err.stack ?? ""}`);
2178
+ return status("Internal Server Error", `${err.message} - ${err.stack ?? ""}`);
2179
2179
  }
2180
- return error("Internal Server Error", `Unknown error: ${err}`);
2180
+ return status("Internal Server Error", `Unknown status: ${err}`);
2181
2181
  }
2182
2182
  });
2183
2183
 
@@ -2204,7 +2204,7 @@ var callback = ({
2204
2204
  onCallbackSuccess,
2205
2205
  onCallbackError
2206
2206
  }) => new Elysia3().use(sessionStore()).get(callbackRoute, async ({
2207
- error,
2207
+ status,
2208
2208
  redirect,
2209
2209
  store: { session },
2210
2210
  cookie: {
@@ -2217,23 +2217,23 @@ var callback = ({
2217
2217
  query: { code, state: callback_state }
2218
2218
  }) => {
2219
2219
  if (stored_state === undefined || code_verifier === undefined || auth_provider === undefined || user_session_id === undefined) {
2220
- return error("Bad Request", "Cookies are missing");
2220
+ return status("Bad Request", "Cookies are missing");
2221
2221
  }
2222
2222
  if (!isNonEmptyString(code) || stored_state.value === undefined) {
2223
- return error("Bad Request", "Invalid callback request");
2223
+ return status("Bad Request", "Invalid callback request");
2224
2224
  }
2225
2225
  if (callback_state !== stored_state.value) {
2226
- return error("Bad Request", "Invalid state mismatch");
2226
+ return status("Bad Request", "Invalid state mismatch");
2227
2227
  }
2228
2228
  if (auth_provider.value === undefined) {
2229
- return error("Unauthorized", "No auth provider found");
2229
+ return status("Unauthorized", "No auth provider found");
2230
2230
  }
2231
2231
  if (!isValidProviderOption(auth_provider.value)) {
2232
- return error("Unauthorized", "Invalid provider");
2232
+ return status("Unauthorized", "Invalid provider");
2233
2233
  }
2234
2234
  const providerConfig = clientProviders[auth_provider.value];
2235
2235
  if (!providerConfig) {
2236
- return error("Unauthorized", "Client provider not found");
2236
+ return status("Unauthorized", "Client provider not found");
2237
2237
  }
2238
2238
  const { providerInstance } = providerConfig;
2239
2239
  stored_state.remove();
@@ -2241,7 +2241,7 @@ var callback = ({
2241
2241
  const requiresPKCE = isPKCEProviderOption(authProvider);
2242
2242
  const verifier = requiresPKCE ? code_verifier.value : undefined;
2243
2243
  if (requiresPKCE && verifier === undefined) {
2244
- return error("Bad Request", "Code verifier not found and is required");
2244
+ return status("Bad Request", "Code verifier not found and is required");
2245
2245
  }
2246
2246
  const originUrl = origin_url?.value ?? "/";
2247
2247
  let tokenResponse;
@@ -2253,7 +2253,7 @@ var callback = ({
2253
2253
  error: err,
2254
2254
  originUrl
2255
2255
  });
2256
- return err instanceof Error ? error("Internal Server Error", `${err.message} - ${err.stack ?? ""}`) : error("Internal Server Error", `Failed to validate authorization code: Unknown error: ${err}`);
2256
+ return err instanceof Error ? status("Internal Server Error", `${err.message} - ${err.stack ?? ""}`) : status("Internal Server Error", `Failed to validate authorization code: Unknown status: ${err}`);
2257
2257
  }
2258
2258
  const existingId = user_session_id?.value;
2259
2259
  const userSessionId = isNonEmptyString(existingId) ? existingId : crypto.randomUUID();
@@ -2288,29 +2288,29 @@ var profile = ({
2288
2288
  onProfileSuccess,
2289
2289
  onProfileError
2290
2290
  }) => new Elysia4().use(sessionStore()).get(profileRoute, async ({
2291
- error,
2291
+ status,
2292
2292
  store: { session },
2293
2293
  cookie: { user_session_id, auth_provider }
2294
2294
  }) => {
2295
2295
  if (auth_provider === undefined || user_session_id === undefined)
2296
- return error("Bad Request", "Cookies are missing");
2296
+ return status("Bad Request", "Cookies are missing");
2297
2297
  if (auth_provider.value === undefined) {
2298
- return error("Unauthorized", "No auth provider found");
2298
+ return status("Unauthorized", "No auth provider found");
2299
2299
  }
2300
2300
  if (!isValidProviderOption(auth_provider.value)) {
2301
- return error("Unauthorized", "Invalid provider");
2301
+ return status("Unauthorized", "Invalid provider");
2302
2302
  }
2303
2303
  if (user_session_id.value === undefined) {
2304
- return error("Unauthorized", "No user session found");
2304
+ return status("Unauthorized", "No user session found");
2305
2305
  }
2306
2306
  const providerConfig = clientProviders[auth_provider.value];
2307
2307
  if (!providerConfig) {
2308
- return error("Unauthorized", "Client provider not found");
2308
+ return status("Unauthorized", "Client provider not found");
2309
2309
  }
2310
2310
  const { providerInstance } = providerConfig;
2311
2311
  const userSession = session[user_session_id.value];
2312
2312
  if (userSession === undefined) {
2313
- return error("Unauthorized", "No user session found");
2313
+ return status("Unauthorized", "No user session found");
2314
2314
  }
2315
2315
  const { accessToken } = userSession;
2316
2316
  try {
@@ -2325,22 +2325,22 @@ var profile = ({
2325
2325
  authProvider: auth_provider.value,
2326
2326
  error: err
2327
2327
  });
2328
- return err instanceof Error ? error("Internal Server Error", `${err.message} - ${err.stack ?? ""}`) : error("Internal Server Error", `Failed to validate authorization code: Unknown error: ${err}`);
2328
+ return err instanceof Error ? status("Internal Server Error", `${err.message} - ${err.stack ?? ""}`) : status("Internal Server Error", `Failed to validate authorization code: Unknown status: ${err}`);
2329
2329
  }
2330
2330
  });
2331
2331
 
2332
2332
  // src/protectRoute.ts
2333
2333
  import { Elysia as Elysia5 } from "elysia";
2334
- var protectRoute = () => new Elysia5().use(sessionStore()).derive(({ store: { session }, cookie: { user_session_id }, error }) => ({
2334
+ var protectRoute = () => new Elysia5().use(sessionStore()).derive(({ store: { session }, cookie: { user_session_id }, status }) => ({
2335
2335
  protectRoute: async (handleAuth, handleAuthFail) => {
2336
2336
  if (user_session_id === undefined)
2337
- return error("Bad Request", "Cookies are missing");
2337
+ return status("Bad Request", "Cookies are missing");
2338
2338
  if (user_session_id.value === undefined) {
2339
- return handleAuthFail?.() ?? error("Unauthorized", "No session ID found");
2339
+ return handleAuthFail?.() ?? status("Unauthorized", "No session ID found");
2340
2340
  }
2341
2341
  const userSession = session[user_session_id.value];
2342
2342
  if (userSession === undefined) {
2343
- return handleAuthFail?.() ?? error("Unauthorized", "No session found");
2343
+ return handleAuthFail?.() ?? status("Unauthorized", "No session found");
2344
2344
  }
2345
2345
  return handleAuth();
2346
2346
  }
@@ -2354,36 +2354,36 @@ var refresh = ({
2354
2354
  onRefreshSuccess,
2355
2355
  onRefreshError
2356
2356
  }) => new Elysia6().use(sessionStore()).post(refreshRoute, async ({
2357
- error,
2357
+ status,
2358
2358
  store: { session },
2359
2359
  cookie: { user_session_id, auth_provider }
2360
2360
  }) => {
2361
2361
  if (auth_provider === undefined || user_session_id === undefined)
2362
- return error("Bad Request", "Cookies are missing");
2362
+ return status("Bad Request", "Cookies are missing");
2363
2363
  if (auth_provider.value === undefined) {
2364
- return error("Unauthorized", "No auth provider found");
2364
+ return status("Unauthorized", "No auth provider found");
2365
2365
  }
2366
2366
  if (!isValidProviderOption(auth_provider.value)) {
2367
- return error("Bad Request", "Invalid provider");
2367
+ return status("Bad Request", "Invalid provider");
2368
2368
  }
2369
2369
  if (user_session_id.value === undefined) {
2370
- return error("Unauthorized", "No user session found");
2370
+ return status("Unauthorized", "No user session found");
2371
2371
  }
2372
2372
  const providerConfig = clientProviders[auth_provider.value];
2373
2373
  if (!providerConfig) {
2374
- return error("Unauthorized", "Client provider not found");
2374
+ return status("Unauthorized", "Client provider not found");
2375
2375
  }
2376
2376
  const { providerInstance } = providerConfig;
2377
2377
  const userSession = session[user_session_id.value];
2378
2378
  if (userSession === undefined) {
2379
- return error("Unauthorized", "No user session found");
2379
+ return status("Unauthorized", "No user session found");
2380
2380
  }
2381
2381
  const { refreshToken } = userSession;
2382
2382
  if (!isRefreshableOAuth2Client(auth_provider.value, providerInstance)) {
2383
- return error("Not Implemented", "Provider is not refreshable");
2383
+ return status("Not Implemented", "Provider is not refreshable");
2384
2384
  }
2385
2385
  if (refreshToken === undefined) {
2386
- return error("Bad Request", "No refresh token found");
2386
+ return status("Bad Request", "No refresh token found");
2387
2387
  }
2388
2388
  try {
2389
2389
  const tokenResponse = await providerInstance.refreshAccessToken(refreshToken);
@@ -2400,9 +2400,9 @@ var refresh = ({
2400
2400
  error: err
2401
2401
  });
2402
2402
  if (err instanceof Error) {
2403
- return error("Internal Server Error", `Failed to refresh token: ${err.message}`);
2403
+ return status("Internal Server Error", `Failed to refresh token: ${err.message}`);
2404
2404
  }
2405
- return error("Internal Server Error", `Failed to refresh token: Unknown error: ${err}`);
2405
+ return status("Internal Server Error", `Failed to refresh token: Unknown status: ${err}`);
2406
2406
  }
2407
2407
  });
2408
2408
 
@@ -2414,32 +2414,32 @@ var revoke = ({
2414
2414
  onRevocationSuccess,
2415
2415
  onRevocationError
2416
2416
  }) => new Elysia7().use(sessionStore()).post(revokeRoute, async ({
2417
- error,
2417
+ status,
2418
2418
  store: { session },
2419
2419
  cookie: { user_session_id, auth_provider }
2420
2420
  }) => {
2421
2421
  if (auth_provider === undefined || user_session_id === undefined)
2422
- return error("Bad Request", "Cookies are missing");
2422
+ return status("Bad Request", "Cookies are missing");
2423
2423
  if (auth_provider.value === undefined) {
2424
- return error("Unauthorized", "No auth provider found");
2424
+ return status("Unauthorized", "No auth provider found");
2425
2425
  }
2426
2426
  if (!isValidProviderOption(auth_provider.value)) {
2427
- return error("Bad Request", "Invalid provider");
2427
+ return status("Bad Request", "Invalid provider");
2428
2428
  }
2429
2429
  if (user_session_id.value === undefined) {
2430
- return error("Unauthorized", "No user session found");
2430
+ return status("Unauthorized", "No user session found");
2431
2431
  }
2432
2432
  const providerConfig = clientProviders[auth_provider.value];
2433
2433
  if (!providerConfig) {
2434
- return error("Unauthorized", "Client provider not found");
2434
+ return status("Unauthorized", "Client provider not found");
2435
2435
  }
2436
2436
  const { providerInstance } = providerConfig;
2437
2437
  if (!isRevocableOAuth2Client(auth_provider.value, providerInstance)) {
2438
- return error("Not Implemented", "Provider does not support revocation");
2438
+ return status("Not Implemented", "Provider does not support revocation");
2439
2439
  }
2440
2440
  const userSession = session[user_session_id.value];
2441
2441
  if (userSession === undefined) {
2442
- return error("Unauthorized", "No user session found");
2442
+ return status("Unauthorized", "No user session found");
2443
2443
  }
2444
2444
  const { accessToken } = userSession;
2445
2445
  try {
@@ -2457,9 +2457,9 @@ var revoke = ({
2457
2457
  error: err
2458
2458
  });
2459
2459
  if (err instanceof Error) {
2460
- return error("Internal Server Error", `Failed to revoke token: ${err.message}`);
2460
+ return status("Internal Server Error", `Failed to revoke token: ${err.message}`);
2461
2461
  }
2462
- return error("Internal Server Error", `Failed to revoke token: Unknown error: ${err}`);
2462
+ return status("Internal Server Error", `Failed to revoke token: Unknown status: ${err}`);
2463
2463
  }
2464
2464
  });
2465
2465
 
@@ -2469,18 +2469,18 @@ var signout = ({
2469
2469
  signoutRoute = "/oauth2/signout",
2470
2470
  onSignOut
2471
2471
  }) => new Elysia8().use(sessionStore()).delete(signoutRoute, async ({
2472
- error,
2472
+ status,
2473
2473
  store: { session },
2474
2474
  cookie: { user_session_id, auth_provider }
2475
2475
  }) => {
2476
2476
  if (auth_provider === undefined || user_session_id === undefined) {
2477
- return error("Bad Request", "Cookies are missing");
2477
+ return status("Bad Request", "Cookies are missing");
2478
2478
  }
2479
2479
  if (auth_provider.value === undefined) {
2480
- return error("Unauthorized", "No auth provider found");
2480
+ return status("Unauthorized", "No auth provider found");
2481
2481
  }
2482
2482
  if (user_session_id.value === undefined) {
2483
- return error("Unauthorized", "No user session id found");
2483
+ return status("Unauthorized", "No user session id found");
2484
2484
  }
2485
2485
  try {
2486
2486
  await onSignOut?.({
@@ -2490,9 +2490,9 @@ var signout = ({
2490
2490
  });
2491
2491
  } catch (err) {
2492
2492
  if (err instanceof Error) {
2493
- return error("Internal Server Error", `Error: ${err.message} - ${err.stack ?? ""}`);
2493
+ return status("Internal Server Error", `Error: ${err.message} - ${err.stack ?? ""}`);
2494
2494
  }
2495
- return error("Internal Server Error", `Unknown Error: ${err}`);
2495
+ return status("Internal Server Error", `Unknown Error: ${err}`);
2496
2496
  }
2497
2497
  user_session_id.remove();
2498
2498
  auth_provider.remove();
@@ -2503,31 +2503,9 @@ var signout = ({
2503
2503
  })
2504
2504
  });
2505
2505
 
2506
- // src/status.ts
2506
+ // src/userStatus.ts
2507
2507
  import { Elysia as Elysia9 } from "elysia";
2508
- var status = ({
2509
- statusRoute = "/oauth2/status",
2510
- onStatus
2511
- }) => new Elysia9().use(sessionStore()).get(statusRoute, async ({
2512
- error,
2513
- cookie: { user_session_id },
2514
- store: { session }
2515
- }) => {
2516
- if (user_session_id === undefined) {
2517
- return error("Bad Request", "Cookies are missing");
2518
- }
2519
- const sessionId = user_session_id.value;
2520
- const user = sessionId !== undefined && session[sessionId] ? session[sessionId].user : null;
2521
- try {
2522
- await onStatus?.({ user });
2523
- } catch (err) {
2524
- if (err instanceof Error) {
2525
- return error("Internal Server Error", `Error: ${err.message} - ${err.stack ?? ""}`);
2526
- }
2527
- return error("Internal Server Error", `Unknown Error: ${err}`);
2528
- }
2529
- return new Response(JSON.stringify({ isLoggedIn: user !== null, user }), { headers: { "Content-Type": "application/json" } });
2530
- });
2508
+
2531
2509
  // src/utils.ts
2532
2510
  var instantiateUserSession = async ({
2533
2511
  userSessionId,
@@ -2563,6 +2541,44 @@ var instantiateUserSession = async ({
2563
2541
  };
2564
2542
  var createAuthConfiguration = (configuration) => configuration;
2565
2543
  var createProvidersConfiguration = (providersConfiguration) => providersConfiguration;
2544
+ var getStatus = async ({
2545
+ user_session_id,
2546
+ status,
2547
+ session,
2548
+ onStatus
2549
+ }) => {
2550
+ if (user_session_id === undefined) {
2551
+ return status("Bad Request", "Cookies are missing");
2552
+ }
2553
+ const sessionId = user_session_id.value;
2554
+ const user = sessionId !== undefined && session[sessionId] ? session[sessionId].user : null;
2555
+ try {
2556
+ await onStatus?.({ user });
2557
+ } catch (err) {
2558
+ if (err instanceof Error) {
2559
+ return status("Internal Server Error", `Error: ${err.message} - ${err.stack ?? ""}`);
2560
+ }
2561
+ return status("Internal Server Error", `Unknown Error: ${err}`);
2562
+ }
2563
+ return new Response(JSON.stringify({ isLoggedIn: user !== null, user }), {
2564
+ headers: { "Content-Type": "application/json" }
2565
+ });
2566
+ };
2567
+
2568
+ // src/userStatus.ts
2569
+ var userStatus = ({
2570
+ statusRoute = "/oauth2/status",
2571
+ onStatus
2572
+ }) => new Elysia9().use(sessionStore()).get(statusRoute, async ({
2573
+ status,
2574
+ cookie: { user_session_id },
2575
+ store: { session }
2576
+ }) => getStatus({
2577
+ onStatus,
2578
+ session,
2579
+ status,
2580
+ user_session_id
2581
+ }));
2566
2582
 
2567
2583
  // src/index.ts
2568
2584
  var absoluteAuth = async ({
@@ -2606,7 +2622,7 @@ var absoluteAuth = async ({
2606
2622
  onRevocationError,
2607
2623
  onRevocationSuccess,
2608
2624
  revokeRoute
2609
- })).use(status({ onStatus, statusRoute })).use(refresh({
2625
+ })).use(userStatus({ onStatus, statusRoute })).use(refresh({
2610
2626
  clientProviders,
2611
2627
  onRefreshError,
2612
2628
  onRefreshSuccess,
@@ -2645,6 +2661,7 @@ export {
2645
2661
  isPKCEProviderOption,
2646
2662
  isOIDCProviderOption,
2647
2663
  instantiateUserSession,
2664
+ getStatus,
2648
2665
  extractPropFromIdentity,
2649
2666
  decodeJWT,
2650
2667
  createProvidersConfiguration,
@@ -2652,5 +2669,5 @@ export {
2652
2669
  absoluteAuth
2653
2670
  };
2654
2671
 
2655
- //# debugId=C7AD0BDB47E761F064756E2164756E21
2672
+ //# debugId=C4B7E8E89D66B28464756E2164756E21
2656
2673
  //# sourceMappingURL=index.js.map