@insforge/sdk 0.0.58-dev.1 → 0.0.58-dev.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.d.mts +80 -46
- package/dist/index.d.ts +80 -46
- package/dist/index.js +158 -68
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +158 -68
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
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;
|
|
@@ -311,19 +333,14 @@ var Auth = class {
|
|
|
311
333
|
async signUp(request) {
|
|
312
334
|
try {
|
|
313
335
|
const response = await this.http.post("/api/auth/users", request);
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
updatedAt: ""
|
|
323
|
-
}
|
|
324
|
-
};
|
|
325
|
-
this.tokenManager.saveSession(session);
|
|
326
|
-
this.http.setAuthToken(response.accessToken || "");
|
|
336
|
+
if (response.accessToken && response.user) {
|
|
337
|
+
const session = {
|
|
338
|
+
accessToken: response.accessToken,
|
|
339
|
+
user: response.user
|
|
340
|
+
};
|
|
341
|
+
this.tokenManager.saveSession(session);
|
|
342
|
+
this.http.setAuthToken(response.accessToken);
|
|
343
|
+
}
|
|
327
344
|
return {
|
|
328
345
|
data: response,
|
|
329
346
|
error: null
|
|
@@ -432,62 +449,24 @@ var Auth = class {
|
|
|
432
449
|
}
|
|
433
450
|
}
|
|
434
451
|
/**
|
|
435
|
-
* Get
|
|
436
|
-
* Returns
|
|
452
|
+
* Get all public authentication configuration (OAuth + Email)
|
|
453
|
+
* Returns both OAuth providers and email authentication settings in one request
|
|
437
454
|
* This is a public endpoint that doesn't require authentication
|
|
438
455
|
*
|
|
439
|
-
* @returns
|
|
456
|
+
* @returns Complete public authentication configuration including OAuth providers and email auth settings
|
|
440
457
|
*
|
|
441
458
|
* @example
|
|
442
459
|
* ```ts
|
|
443
|
-
* const { data, error } = await insforge.auth.
|
|
460
|
+
* const { data, error } = await insforge.auth.getPublicAuthConfig();
|
|
444
461
|
* if (data) {
|
|
445
|
-
*
|
|
446
|
-
*
|
|
462
|
+
* console.log(`OAuth providers: ${data.oauth.data.length}`);
|
|
463
|
+
* console.log(`Password min length: ${data.email.passwordMinLength}`);
|
|
447
464
|
* }
|
|
448
465
|
* ```
|
|
449
466
|
*/
|
|
450
|
-
async
|
|
467
|
+
async getPublicAuthConfig() {
|
|
451
468
|
try {
|
|
452
|
-
const response = await this.http.get("/api/auth/
|
|
453
|
-
return {
|
|
454
|
-
data: response.data,
|
|
455
|
-
error: null
|
|
456
|
-
};
|
|
457
|
-
} catch (error) {
|
|
458
|
-
if (error instanceof InsForgeError) {
|
|
459
|
-
return { data: null, error };
|
|
460
|
-
}
|
|
461
|
-
return {
|
|
462
|
-
data: null,
|
|
463
|
-
error: new InsForgeError(
|
|
464
|
-
"An unexpected error occurred while fetching OAuth providers",
|
|
465
|
-
500,
|
|
466
|
-
"UNEXPECTED_ERROR"
|
|
467
|
-
)
|
|
468
|
-
};
|
|
469
|
-
}
|
|
470
|
-
}
|
|
471
|
-
/**
|
|
472
|
-
* Get public email authentication configuration
|
|
473
|
-
* Returns email authentication settings configured on the backend
|
|
474
|
-
* This is a public endpoint that doesn't require authentication
|
|
475
|
-
*
|
|
476
|
-
* @returns Email authentication configuration including password requirements and email verification settings
|
|
477
|
-
*
|
|
478
|
-
* @example
|
|
479
|
-
* ```ts
|
|
480
|
-
* const { data, error } = await insforge.auth.getEmailAuthConfig();
|
|
481
|
-
* if (data) {
|
|
482
|
-
* console.log(`Password min length: ${data.passwordMinLength}`);
|
|
483
|
-
* console.log(`Requires email verification: ${data.requireEmailVerification}`);
|
|
484
|
-
* console.log(`Requires uppercase: ${data.requireUppercase}`);
|
|
485
|
-
* }
|
|
486
|
-
* ```
|
|
487
|
-
*/
|
|
488
|
-
async getEmailAuthConfig() {
|
|
489
|
-
try {
|
|
490
|
-
const response = await this.http.get("/api/auth/email/public-config");
|
|
469
|
+
const response = await this.http.get("/api/auth/public-config");
|
|
491
470
|
return {
|
|
492
471
|
data: response,
|
|
493
472
|
error: null
|
|
@@ -499,7 +478,7 @@ var Auth = class {
|
|
|
499
478
|
return {
|
|
500
479
|
data: null,
|
|
501
480
|
error: new InsForgeError(
|
|
502
|
-
"An unexpected error occurred while fetching
|
|
481
|
+
"An unexpected error occurred while fetching public authentication configuration",
|
|
503
482
|
500,
|
|
504
483
|
"UNEXPECTED_ERROR"
|
|
505
484
|
)
|
|
@@ -508,7 +487,7 @@ var Auth = class {
|
|
|
508
487
|
}
|
|
509
488
|
/**
|
|
510
489
|
* Get the current user with full profile information
|
|
511
|
-
* Returns both auth info (id, email, role) and profile data (
|
|
490
|
+
* Returns both auth info (id, email, role) and profile data (dynamic fields from users table)
|
|
512
491
|
*/
|
|
513
492
|
async getCurrentUser() {
|
|
514
493
|
try {
|
|
@@ -525,7 +504,7 @@ var Auth = class {
|
|
|
525
504
|
return {
|
|
526
505
|
data: {
|
|
527
506
|
user: authResponse.user,
|
|
528
|
-
profile
|
|
507
|
+
profile: profile ? convertDbProfileToCamelCase(profile) : null
|
|
529
508
|
},
|
|
530
509
|
error: null
|
|
531
510
|
};
|
|
@@ -549,14 +528,17 @@ var Auth = class {
|
|
|
549
528
|
}
|
|
550
529
|
/**
|
|
551
530
|
* Get any user's profile by ID
|
|
552
|
-
* Returns profile information from the users table (
|
|
531
|
+
* Returns profile information from the users table (dynamic fields)
|
|
553
532
|
*/
|
|
554
533
|
async getProfile(userId) {
|
|
555
534
|
const { data, error } = await this.database.from("users").select("*").eq("id", userId).single();
|
|
556
535
|
if (error && error.code === "PGRST116") {
|
|
557
536
|
return { data: null, error: null };
|
|
558
537
|
}
|
|
559
|
-
|
|
538
|
+
if (data) {
|
|
539
|
+
return { data: convertDbProfileToCamelCase(data), error: null };
|
|
540
|
+
}
|
|
541
|
+
return { data: null, error };
|
|
560
542
|
}
|
|
561
543
|
/**
|
|
562
544
|
* Get the current session (only session data, no API call)
|
|
@@ -586,7 +568,7 @@ var Auth = class {
|
|
|
586
568
|
}
|
|
587
569
|
/**
|
|
588
570
|
* Set/Update the current user's profile
|
|
589
|
-
* Updates profile information in the users table (
|
|
571
|
+
* Updates profile information in the users table (supports any dynamic fields)
|
|
590
572
|
*/
|
|
591
573
|
async setProfile(profile) {
|
|
592
574
|
const session = this.tokenManager.getSession();
|
|
@@ -606,12 +588,120 @@ var Auth = class {
|
|
|
606
588
|
return { data: null, error: error2 };
|
|
607
589
|
}
|
|
608
590
|
if (data2?.user) {
|
|
609
|
-
session.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
|
+
};
|
|
603
|
+
this.tokenManager.saveSession(session);
|
|
604
|
+
}
|
|
605
|
+
}
|
|
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
|
+
};
|
|
610
685
|
this.tokenManager.saveSession(session);
|
|
686
|
+
this.http.setAuthToken(response.accessToken);
|
|
611
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
|
+
};
|
|
612
704
|
}
|
|
613
|
-
const { data, error } = await this.database.from("users").update(profile).eq("id", session.user.id).select().single();
|
|
614
|
-
return { data, error };
|
|
615
705
|
}
|
|
616
706
|
};
|
|
617
707
|
|