@insforge/sdk 0.0.58-dev.3 → 0.0.58-dev.5

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;
@@ -427,62 +449,24 @@ var Auth = class {
427
449
  }
428
450
  }
429
451
  /**
430
- * Get list of available OAuth providers
431
- * Returns the list of OAuth providers configured on the backend
452
+ * Get all public authentication configuration (OAuth + Email)
453
+ * Returns both OAuth providers and email authentication settings in one request
432
454
  * This is a public endpoint that doesn't require authentication
433
455
  *
434
- * @returns Array of configured OAuth providers with their configuration status
456
+ * @returns Complete public authentication configuration including OAuth providers and email auth settings
435
457
  *
436
458
  * @example
437
459
  * ```ts
438
- * const { data, error } = await insforge.auth.getOAuthProviders();
460
+ * const { data, error } = await insforge.auth.getPublicAuthConfig();
439
461
  * if (data) {
440
- * // data is an array of PublicOAuthProvider: [{ provider: 'google', isConfigured: true }, ...]
441
- * data.forEach(p => console.log(`${p.provider}: ${p.isConfigured ? 'configured' : 'not configured'}`));
462
+ * console.log(`OAuth providers: ${data.oauth.data.length}`);
463
+ * console.log(`Password min length: ${data.email.passwordMinLength}`);
442
464
  * }
443
465
  * ```
444
466
  */
445
- async getOAuthProviders() {
467
+ async getPublicAuthConfig() {
446
468
  try {
447
- const response = await this.http.get("/api/auth/oauth/providers");
448
- return {
449
- data: response.data,
450
- error: null
451
- };
452
- } catch (error) {
453
- if (error instanceof InsForgeError) {
454
- return { data: null, error };
455
- }
456
- return {
457
- data: null,
458
- error: new InsForgeError(
459
- "An unexpected error occurred while fetching OAuth providers",
460
- 500,
461
- "UNEXPECTED_ERROR"
462
- )
463
- };
464
- }
465
- }
466
- /**
467
- * Get public email authentication configuration
468
- * Returns email authentication settings configured on the backend
469
- * This is a public endpoint that doesn't require authentication
470
- *
471
- * @returns Email authentication configuration including password requirements and email verification settings
472
- *
473
- * @example
474
- * ```ts
475
- * const { data, error } = await insforge.auth.getEmailAuthConfig();
476
- * if (data) {
477
- * console.log(`Password min length: ${data.passwordMinLength}`);
478
- * console.log(`Requires email verification: ${data.requireEmailVerification}`);
479
- * console.log(`Requires uppercase: ${data.requireUppercase}`);
480
- * }
481
- * ```
482
- */
483
- async getEmailAuthConfig() {
484
- try {
485
- const response = await this.http.get("/api/auth/email/public-config");
469
+ const response = await this.http.get("/api/auth/public-config");
486
470
  return {
487
471
  data: response,
488
472
  error: null
@@ -494,7 +478,7 @@ var Auth = class {
494
478
  return {
495
479
  data: null,
496
480
  error: new InsForgeError(
497
- "An unexpected error occurred while fetching email authentication configuration",
481
+ "An unexpected error occurred while fetching public authentication configuration",
498
482
  500,
499
483
  "UNEXPECTED_ERROR"
500
484
  )
@@ -503,7 +487,7 @@ var Auth = class {
503
487
  }
504
488
  /**
505
489
  * Get the current user with full profile information
506
- * 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)
507
491
  */
508
492
  async getCurrentUser() {
509
493
  try {
@@ -520,7 +504,7 @@ var Auth = class {
520
504
  return {
521
505
  data: {
522
506
  user: authResponse.user,
523
- profile
507
+ profile: profile ? convertDbProfileToCamelCase(profile) : null
524
508
  },
525
509
  error: null
526
510
  };
@@ -544,14 +528,17 @@ var Auth = class {
544
528
  }
545
529
  /**
546
530
  * Get any user's profile by ID
547
- * Returns profile information from the users table (nickname, avatar_url, bio, etc.)
531
+ * Returns profile information from the users table (dynamic fields)
548
532
  */
549
533
  async getProfile(userId) {
550
534
  const { data, error } = await this.database.from("users").select("*").eq("id", userId).single();
551
535
  if (error && error.code === "PGRST116") {
552
536
  return { data: null, error: null };
553
537
  }
554
- return { data, error };
538
+ if (data) {
539
+ return { data: convertDbProfileToCamelCase(data), error: null };
540
+ }
541
+ return { data: null, error };
555
542
  }
556
543
  /**
557
544
  * Get the current session (only session data, no API call)
@@ -581,7 +568,7 @@ var Auth = class {
581
568
  }
582
569
  /**
583
570
  * Set/Update the current user's profile
584
- * Updates profile information in the users table (nickname, avatar_url, bio, etc.)
571
+ * Updates profile information in the users table (supports any dynamic fields)
585
572
  */
586
573
  async setProfile(profile) {
587
574
  const session = this.tokenManager.getSession();
@@ -605,7 +592,7 @@ var Auth = class {
605
592
  id: data2.user.id,
606
593
  email: data2.user.email,
607
594
  name: data2.profile?.nickname || "",
608
- // Not available from API, but required by UserSchema
595
+ // Fallback - profile structure is dynamic
609
596
  emailVerified: false,
610
597
  // Not available from API, but required by UserSchema
611
598
  createdAt: (/* @__PURE__ */ new Date()).toISOString(),
@@ -616,8 +603,105 @@ var Auth = class {
616
603
  this.tokenManager.saveSession(session);
617
604
  }
618
605
  }
619
- const { data, error } = await this.database.from("users").update(profile).eq("id", session.user.id).select().single();
620
- 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 };
612
+ }
613
+ /**
614
+ * Send password reset code to user's email
615
+ * Always returns success to prevent user enumeration
616
+ */
617
+ async sendPasswordResetCode(request) {
618
+ try {
619
+ const response = await this.http.post(
620
+ "/api/auth/email/send-reset-password-code",
621
+ request
622
+ );
623
+ return {
624
+ data: response,
625
+ error: null
626
+ };
627
+ } catch (error) {
628
+ if (error instanceof InsForgeError) {
629
+ return { data: null, error };
630
+ }
631
+ return {
632
+ data: null,
633
+ error: new InsForgeError(
634
+ "An unexpected error occurred while sending password reset code",
635
+ 500,
636
+ "UNEXPECTED_ERROR"
637
+ )
638
+ };
639
+ }
640
+ }
641
+ /**
642
+ * Reset password with OTP token
643
+ * Token can be from magic link or from code verification
644
+ */
645
+ async resetPassword(request) {
646
+ try {
647
+ const response = await this.http.post(
648
+ "/api/auth/reset-password",
649
+ request
650
+ );
651
+ return {
652
+ data: response,
653
+ error: null
654
+ };
655
+ } catch (error) {
656
+ if (error instanceof InsForgeError) {
657
+ return { data: null, error };
658
+ }
659
+ return {
660
+ data: null,
661
+ error: new InsForgeError(
662
+ "An unexpected error occurred while resetting password",
663
+ 500,
664
+ "UNEXPECTED_ERROR"
665
+ )
666
+ };
667
+ }
668
+ }
669
+ /**
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)
673
+ */
674
+ async verifyEmail(request) {
675
+ try {
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
+ }
688
+ return {
689
+ data: response,
690
+ error: null
691
+ };
692
+ } catch (error) {
693
+ if (error instanceof InsForgeError) {
694
+ return { data: null, error };
695
+ }
696
+ return {
697
+ data: null,
698
+ error: new InsForgeError(
699
+ "An unexpected error occurred while verifying email",
700
+ 500,
701
+ "UNEXPECTED_ERROR"
702
+ )
703
+ };
704
+ }
621
705
  }
622
706
  };
623
707