@insforge/sdk 1.4.1 → 1.4.2

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/ssr.mjs CHANGED
@@ -833,19 +833,32 @@ var HttpClient = class {
833
833
 
834
834
  // src/modules/auth/helpers.ts
835
835
  var PKCE_VERIFIER_KEY = "insforge_pkce_verifier";
836
+ async function getWebCrypto() {
837
+ const webCrypto = globalThis.crypto;
838
+ if (typeof webCrypto?.getRandomValues === "function" && webCrypto.subtle) {
839
+ return webCrypto;
840
+ }
841
+ if (typeof process !== "undefined" && process.versions?.node) {
842
+ const { webcrypto } = await import("crypto");
843
+ return webcrypto;
844
+ }
845
+ throw new Error("Web Crypto API is not available in this environment");
846
+ }
836
847
  function base64UrlEncode(buffer) {
837
848
  const base64 = btoa(String.fromCharCode(...buffer));
838
849
  return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
839
850
  }
840
- function generateCodeVerifier() {
851
+ async function generateCodeVerifier() {
852
+ const webCrypto = await getWebCrypto();
841
853
  const array = new Uint8Array(32);
842
- crypto.getRandomValues(array);
854
+ webCrypto.getRandomValues(array);
843
855
  return base64UrlEncode(array);
844
856
  }
845
857
  async function generateCodeChallenge(verifier) {
858
+ const webCrypto = await getWebCrypto();
846
859
  const encoder = new TextEncoder();
847
860
  const data = encoder.encode(verifier);
848
- const hash = await crypto.subtle.digest("SHA-256", data);
861
+ const hash = await webCrypto.subtle.digest("SHA-256", data);
849
862
  return base64UrlEncode(new Uint8Array(hash));
850
863
  }
851
864
  function storePkceVerifier(verifier) {
@@ -892,7 +905,7 @@ var Auth = class {
892
905
  this.http = http;
893
906
  this.tokenManager = tokenManager;
894
907
  this.options = options;
895
- this.authCallbackHandled = this.detectAuthCallback();
908
+ this.authCallbackHandled = options.detectOAuthCallback === false ? Promise.resolve() : this.detectAuthCallback();
896
909
  }
897
910
  isServerMode() {
898
911
  return !!this.options.isServerMode;
@@ -1038,7 +1051,7 @@ var Auth = class {
1038
1051
  }
1039
1052
  const { provider } = signInOptions;
1040
1053
  const providerKey = encodeURIComponent(provider.toLowerCase());
1041
- const codeVerifier = generateCodeVerifier();
1054
+ const codeVerifier = await generateCodeVerifier();
1042
1055
  const codeChallenge = await generateCodeChallenge(codeVerifier);
1043
1056
  storePkceVerifier(codeVerifier);
1044
1057
  const params = {
@@ -2760,7 +2773,8 @@ var InsForgeClient = class {
2760
2773
  this.tokenManager.setAccessToken(accessToken);
2761
2774
  }
2762
2775
  this.auth = new Auth(this.http, this.tokenManager, {
2763
- isServerMode: config.isServerMode ?? !!accessToken
2776
+ isServerMode: config.isServerMode ?? !!accessToken,
2777
+ detectOAuthCallback: config.auth?.detectOAuthCallback
2764
2778
  });
2765
2779
  this.database = new Database(this.http);
2766
2780
  this.storage = new Storage(this.http);
@@ -3169,7 +3183,10 @@ function createBrowserClient(options = {}) {
3169
3183
  fetch: ssrFetch,
3170
3184
  // Browser clients manage tokens via the refresh route, not a static
3171
3185
  // config token; shadow any untyped accessToken in the options spread.
3172
- accessToken: void 0
3186
+ accessToken: void 0,
3187
+ auth: {
3188
+ detectOAuthCallback: false
3189
+ }
3173
3190
  });
3174
3191
  const setAccessToken = client.setAccessToken.bind(client);
3175
3192
  client.setAccessToken = (token) => {
@@ -3374,6 +3391,109 @@ function createRefreshAuthRouter(options = {}) {
3374
3391
  };
3375
3392
  }
3376
3393
 
3394
+ // src/ssr/auth-actions.ts
3395
+ function persistSessionCookies(cookies, data, settings) {
3396
+ if (!data?.accessToken) return;
3397
+ setAuthCookies(
3398
+ cookies,
3399
+ {
3400
+ accessToken: data.accessToken,
3401
+ refreshToken: data.refreshToken
3402
+ },
3403
+ settings
3404
+ );
3405
+ }
3406
+ function sanitizeAuthData(data) {
3407
+ if (!data) return null;
3408
+ const {
3409
+ accessToken: _accessToken,
3410
+ refreshToken: _refreshToken,
3411
+ csrfToken: _csrfToken,
3412
+ ...safeData
3413
+ } = data;
3414
+ return safeData;
3415
+ }
3416
+ function toSafeAuthResult(result) {
3417
+ return {
3418
+ data: sanitizeAuthData(result.data),
3419
+ error: result.error
3420
+ };
3421
+ }
3422
+ function createAuthActions(options = {}) {
3423
+ const {
3424
+ cookies,
3425
+ requestCookies,
3426
+ responseCookies,
3427
+ names,
3428
+ options: cookieOptions,
3429
+ ...clientOptions
3430
+ } = options;
3431
+ const readCookies = requestCookies ?? cookies;
3432
+ const writeCookies = responseCookies ?? cookies;
3433
+ if (!writeCookies?.set) {
3434
+ throw new Error(
3435
+ "createAuthActions() requires a writable cookie store. Pass cookies in Server Actions or responseCookies in Route Handlers."
3436
+ );
3437
+ }
3438
+ const cookieSettings = {
3439
+ names,
3440
+ options: cookieOptions
3441
+ };
3442
+ const createClient = () => createServerClient({
3443
+ ...clientOptions,
3444
+ names,
3445
+ options: cookieOptions,
3446
+ cookies: readCookies
3447
+ });
3448
+ return {
3449
+ signUp: async (request) => {
3450
+ const result = await createClient().auth.signUp(request);
3451
+ persistSessionCookies(writeCookies, result.data, cookieSettings);
3452
+ return toSafeAuthResult(result);
3453
+ },
3454
+ signInWithPassword: async (request) => {
3455
+ const result = await createClient().auth.signInWithPassword(request);
3456
+ persistSessionCookies(writeCookies, result.data, cookieSettings);
3457
+ return toSafeAuthResult(
3458
+ result
3459
+ );
3460
+ },
3461
+ signInWithOAuth: async (providerOrOptions, signInOptions) => {
3462
+ return createClient().auth.signInWithOAuth(
3463
+ providerOrOptions,
3464
+ signInOptions
3465
+ );
3466
+ },
3467
+ signInWithIdToken: async (credentials) => {
3468
+ const result = await createClient().auth.signInWithIdToken(credentials);
3469
+ persistSessionCookies(writeCookies, result.data, cookieSettings);
3470
+ return toSafeAuthResult(
3471
+ result
3472
+ );
3473
+ },
3474
+ exchangeOAuthCode: async (code, codeVerifier) => {
3475
+ const result = await createClient().auth.exchangeOAuthCode(
3476
+ code,
3477
+ codeVerifier
3478
+ );
3479
+ persistSessionCookies(writeCookies, result.data, cookieSettings);
3480
+ return toSafeAuthResult(
3481
+ result
3482
+ );
3483
+ },
3484
+ verifyEmail: async (request) => {
3485
+ const result = await createClient().auth.verifyEmail(request);
3486
+ persistSessionCookies(writeCookies, result.data, cookieSettings);
3487
+ return toSafeAuthResult(result);
3488
+ },
3489
+ signOut: async () => {
3490
+ const result = await createClient().auth.signOut();
3491
+ clearAuthCookies(writeCookies, cookieSettings);
3492
+ return result;
3493
+ }
3494
+ };
3495
+ }
3496
+
3377
3497
  // src/ssr/update-session.ts
3378
3498
  async function updateSession(options) {
3379
3499
  const accessCookieName = getAccessTokenCookieName(options.names);
@@ -3434,6 +3554,7 @@ export {
3434
3554
  DEFAULT_REFRESH_TOKEN_COOKIE,
3435
3555
  accessTokenCookieOptions,
3436
3556
  clearAuthCookies,
3557
+ createAuthActions,
3437
3558
  createBrowserClient,
3438
3559
  createRefreshAuthRouter,
3439
3560
  createServerClient,