@insforge/sdk 0.0.58-dev.0 → 0.0.58-dev.10

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.mjs CHANGED
@@ -255,6 +255,28 @@ var Database = class {
255
255
  };
256
256
 
257
257
  // src/modules/auth.ts
258
+ function convertDbProfileToCamelCase(dbProfile) {
259
+ const result = {
260
+ id: dbProfile.id
261
+ };
262
+ if (dbProfile.created_at !== void 0) result.createdAt = dbProfile.created_at;
263
+ if (dbProfile.updated_at !== void 0) result.updatedAt = dbProfile.updated_at;
264
+ Object.keys(dbProfile).forEach((key) => {
265
+ if (key === "id" || key === "created_at" || key === "updated_at") return;
266
+ const camelKey = key.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
267
+ result[camelKey] = dbProfile[key];
268
+ });
269
+ return result;
270
+ }
271
+ function convertCamelCaseToDbProfile(profile) {
272
+ const dbProfile = {};
273
+ Object.keys(profile).forEach((key) => {
274
+ if (profile[key] === void 0) return;
275
+ const snakeKey = key.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
276
+ dbProfile[snakeKey] = profile[key];
277
+ });
278
+ return dbProfile;
279
+ }
258
280
  var Auth = class {
259
281
  constructor(http, tokenManager) {
260
282
  this.http = http;
@@ -344,11 +366,18 @@ var Auth = class {
344
366
  try {
345
367
  const response = await this.http.post("/api/auth/sessions", request);
346
368
  const session = {
347
- accessToken: response.accessToken,
348
- user: response.user
369
+ accessToken: response.accessToken || "",
370
+ user: response.user || {
371
+ id: "",
372
+ email: "",
373
+ name: "",
374
+ emailVerified: false,
375
+ createdAt: "",
376
+ updatedAt: ""
377
+ }
349
378
  };
350
379
  this.tokenManager.saveSession(session);
351
- this.http.setAuthToken(response.accessToken);
380
+ this.http.setAuthToken(response.accessToken || "");
352
381
  return {
353
382
  data: response,
354
383
  error: null
@@ -419,9 +448,46 @@ var Auth = class {
419
448
  };
420
449
  }
421
450
  }
451
+ /**
452
+ * Get all public authentication configuration (OAuth + Email)
453
+ * Returns both OAuth providers and email authentication settings in one request
454
+ * This is a public endpoint that doesn't require authentication
455
+ *
456
+ * @returns Complete public authentication configuration including OAuth providers and email auth settings
457
+ *
458
+ * @example
459
+ * ```ts
460
+ * const { data, error } = await insforge.auth.getPublicAuthConfig();
461
+ * if (data) {
462
+ * console.log(`OAuth providers: ${data.oauth.data.length}`);
463
+ * console.log(`Password min length: ${data.email.passwordMinLength}`);
464
+ * }
465
+ * ```
466
+ */
467
+ async getPublicAuthConfig() {
468
+ try {
469
+ const response = await this.http.get("/api/auth/public-config");
470
+ return {
471
+ data: response,
472
+ error: null
473
+ };
474
+ } catch (error) {
475
+ if (error instanceof InsForgeError) {
476
+ return { data: null, error };
477
+ }
478
+ return {
479
+ data: null,
480
+ error: new InsForgeError(
481
+ "An unexpected error occurred while fetching public authentication configuration",
482
+ 500,
483
+ "UNEXPECTED_ERROR"
484
+ )
485
+ };
486
+ }
487
+ }
422
488
  /**
423
489
  * Get the current user with full profile information
424
- * Returns both auth info (id, email, role) and profile data (nickname, avatar_url, bio, etc.)
490
+ * Returns both auth info (id, email, role) and profile data (dynamic fields from users table)
425
491
  */
426
492
  async getCurrentUser() {
427
493
  try {
@@ -438,7 +504,7 @@ var Auth = class {
438
504
  return {
439
505
  data: {
440
506
  user: authResponse.user,
441
- profile
507
+ profile: profile ? convertDbProfileToCamelCase(profile) : null
442
508
  },
443
509
  error: null
444
510
  };
@@ -462,14 +528,17 @@ var Auth = class {
462
528
  }
463
529
  /**
464
530
  * Get any user's profile by ID
465
- * Returns profile information from the users table (nickname, avatar_url, bio, etc.)
531
+ * Returns profile information from the users table (dynamic fields)
466
532
  */
467
533
  async getProfile(userId) {
468
534
  const { data, error } = await this.database.from("users").select("*").eq("id", userId).single();
469
535
  if (error && error.code === "PGRST116") {
470
536
  return { data: null, error: null };
471
537
  }
472
- return { data, error };
538
+ if (data) {
539
+ return { data: convertDbProfileToCamelCase(data), error: null };
540
+ }
541
+ return { data: null, error };
473
542
  }
474
543
  /**
475
544
  * Get the current session (only session data, no API call)
@@ -499,7 +568,7 @@ var Auth = class {
499
568
  }
500
569
  /**
501
570
  * Set/Update the current user's profile
502
- * Updates profile information in the users table (nickname, avatar_url, bio, etc.)
571
+ * Updates profile information in the users table (supports any dynamic fields)
503
572
  */
504
573
  async setProfile(profile) {
505
574
  const session = this.tokenManager.getSession();
@@ -519,23 +588,38 @@ var Auth = class {
519
588
  return { data: null, error: error2 };
520
589
  }
521
590
  if (data2?.user) {
522
- session.user = data2.user;
591
+ session.user = {
592
+ id: data2.user.id,
593
+ email: data2.user.email,
594
+ name: data2.profile?.nickname || "",
595
+ // Fallback - profile structure is dynamic
596
+ emailVerified: false,
597
+ // Not available from API, but required by UserSchema
598
+ createdAt: (/* @__PURE__ */ new Date()).toISOString(),
599
+ // Fallback
600
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString()
601
+ // Fallback
602
+ };
523
603
  this.tokenManager.saveSession(session);
524
604
  }
525
605
  }
526
- const { data, error } = await this.database.from("users").update(profile).eq("id", session.user.id).select().single();
527
- return { data, error };
606
+ const dbProfile = convertCamelCaseToDbProfile(profile);
607
+ const { data, error } = await this.database.from("users").update(dbProfile).eq("id", session.user.id).select().single();
608
+ if (data) {
609
+ return { data: convertDbProfileToCamelCase(data), error: null };
610
+ }
611
+ return { data: null, error };
528
612
  }
529
613
  /**
530
- * Send email verification code or link
531
- * @param type - 'code' for numeric OTP (6 digits) or 'link' for verification link
532
- * @param email - Email address to send verification to
614
+ * Send password reset code to user's email
615
+ * Always returns success to prevent user enumeration
533
616
  */
534
- async sendEmailVerification(type, email) {
617
+ async sendPasswordResetCode(request) {
535
618
  try {
536
- const request = { email };
537
- const endpoint = type === "code" ? "/api/auth/email/send-verification-code" : "/api/auth/email/send-verification-link";
538
- const response = await this.http.post(endpoint, request);
619
+ const response = await this.http.post(
620
+ "/api/auth/email/send-reset-password-code",
621
+ request
622
+ );
539
623
  return {
540
624
  data: response,
541
625
  error: null
@@ -547,7 +631,7 @@ var Auth = class {
547
631
  return {
548
632
  data: null,
549
633
  error: new InsForgeError(
550
- "An unexpected error occurred while sending verification email",
634
+ "An unexpected error occurred while sending password reset code",
551
635
  500,
552
636
  "UNEXPECTED_ERROR"
553
637
  )
@@ -555,20 +639,15 @@ var Auth = class {
555
639
  }
556
640
  }
557
641
  /**
558
- * Verify email with OTP
559
- * @param otp - 6-digit numeric code (with email) or 64-char hex token (without email)
560
- * @param email - Optional email address (required for code verification)
642
+ * Reset password with OTP token
643
+ * Token can be from magic link or from code verification
561
644
  */
562
- async verifyEmail(otp, email) {
645
+ async resetPassword(request) {
563
646
  try {
564
- const request = { otp, email };
565
- const response = await this.http.post("/api/auth/verify-email", request);
566
- const session = {
567
- accessToken: response.accessToken,
568
- user: response.user
569
- };
570
- this.tokenManager.saveSession(session);
571
- this.http.setAuthToken(response.accessToken);
647
+ const response = await this.http.post(
648
+ "/api/auth/reset-password",
649
+ request
650
+ );
572
651
  return {
573
652
  data: response,
574
653
  error: null
@@ -580,7 +659,7 @@ var Auth = class {
580
659
  return {
581
660
  data: null,
582
661
  error: new InsForgeError(
583
- "An unexpected error occurred while verifying email",
662
+ "An unexpected error occurred while resetting password",
584
663
  500,
585
664
  "UNEXPECTED_ERROR"
586
665
  )
@@ -588,15 +667,24 @@ var Auth = class {
588
667
  }
589
668
  }
590
669
  /**
591
- * Send password reset code or link
592
- * @param type - 'code' for numeric OTP (6 digits) or 'link' for reset link
593
- * @param email - Email address to send reset instructions to
670
+ * Verify email with OTP token
671
+ * If email is provided: uses numeric OTP verification (6-digit code)
672
+ * If email is NOT provided: uses link OTP verification (64-char token)
594
673
  */
595
- async sendResetPasswordEmail(type, email) {
674
+ async verifyEmail(request) {
596
675
  try {
597
- const request = { email };
598
- const endpoint = type === "code" ? "/api/auth/email/send-reset-password-code" : "/api/auth/email/send-reset-password-link";
599
- const response = await this.http.post(endpoint, request);
676
+ const response = await this.http.post(
677
+ "/api/auth/verify-email",
678
+ request
679
+ );
680
+ if (response.accessToken) {
681
+ const session = {
682
+ accessToken: response.accessToken,
683
+ user: response.user || {}
684
+ };
685
+ this.tokenManager.saveSession(session);
686
+ this.http.setAuthToken(response.accessToken);
687
+ }
600
688
  return {
601
689
  data: response,
602
690
  error: null
@@ -608,7 +696,7 @@ var Auth = class {
608
696
  return {
609
697
  data: null,
610
698
  error: new InsForgeError(
611
- "An unexpected error occurred while sending reset password email",
699
+ "An unexpected error occurred while verifying email",
612
700
  500,
613
701
  "UNEXPECTED_ERROR"
614
702
  )
@@ -616,31 +704,19 @@ var Auth = class {
616
704
  }
617
705
  }
618
706
  /**
619
- * Reset password with OTP
620
- * @param otp - 6-digit numeric code (with email) or 64-char hex token (without email)
621
- * @param newPassword - New password to set
622
- * @param email - Optional email address (required for code-based reset)
707
+ * Set the current session
708
+ * This is used to set the session from the OAuth callback
623
709
  */
624
- async resetPassword(otp, newPassword, email) {
710
+ async setSession(session) {
625
711
  try {
626
- const request = { otp, newPassword, email };
627
- const response = await this.http.post("/api/auth/reset-password", request);
628
- return {
629
- data: response,
630
- error: null
631
- };
712
+ this.tokenManager.saveSession(session);
713
+ this.http.setAuthToken(session.accessToken);
632
714
  } catch (error) {
633
- if (error instanceof InsForgeError) {
634
- return { data: null, error };
635
- }
636
- return {
637
- data: null,
638
- error: new InsForgeError(
639
- "An unexpected error occurred while resetting password",
640
- 500,
641
- "UNEXPECTED_ERROR"
642
- )
643
- };
715
+ throw new InsForgeError(
716
+ "An unexpected error occurred while setting session",
717
+ 500,
718
+ "UNEXPECTED_ERROR"
719
+ );
644
720
  }
645
721
  }
646
722
  };