aaspai-authx 0.1.1 → 0.1.3
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/express/index.cjs +97 -16
- package/dist/express/index.cjs.map +1 -1
- package/dist/express/index.js +97 -16
- package/dist/express/index.js.map +1 -1
- package/dist/index.cjs +97 -16
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +71 -70
- package/dist/index.d.ts +71 -70
- package/dist/index.js +97 -16
- package/dist/index.js.map +1 -1
- package/dist/nest/index.cjs +97 -16
- package/dist/nest/index.cjs.map +1 -1
- package/dist/nest/index.js +97 -16
- package/dist/nest/index.js.map +1 -1
- package/package.json +2 -2
package/dist/express/index.cjs
CHANGED
|
@@ -182,10 +182,14 @@ function buildSession(payload) {
|
|
|
182
182
|
roles: normalizedRoles,
|
|
183
183
|
permissions
|
|
184
184
|
};
|
|
185
|
+
if (payload?.firstName) session.firstName = payload.firstName;
|
|
186
|
+
if (payload?.lastName) session.lastName = payload.lastName;
|
|
185
187
|
if (payload?.projectId) session.projectId = payload.projectId;
|
|
186
188
|
if (payload?.orgId) session.orgId = payload.orgId;
|
|
187
189
|
if (payload?.org_id) session.org_id = payload.org_id;
|
|
188
190
|
if (payload?.authType) session.authType = payload.authType;
|
|
191
|
+
if (payload?.createdAt) session.createdAt = payload.createdAt;
|
|
192
|
+
if (payload?.metadata) session.metadata = payload.metadata;
|
|
189
193
|
Object.keys(payload || {}).forEach((key) => {
|
|
190
194
|
if (![
|
|
191
195
|
"sub",
|
|
@@ -359,10 +363,14 @@ function requireAuth() {
|
|
|
359
363
|
const session = buildSession({
|
|
360
364
|
sub: user.id.toString(),
|
|
361
365
|
email: user.email,
|
|
366
|
+
firstName: user.firstName,
|
|
367
|
+
lastName: user.lastName,
|
|
368
|
+
metadata: user.metadata || [],
|
|
362
369
|
roles: user.roles || [],
|
|
363
370
|
orgId: user.orgId,
|
|
364
371
|
org_id: user.orgId,
|
|
365
|
-
projectId: user.projectId
|
|
372
|
+
projectId: user.projectId,
|
|
373
|
+
createdAt: user.createdAt
|
|
366
374
|
});
|
|
367
375
|
session.authType = "api-key";
|
|
368
376
|
session.projectId = readProjectId(req) || user.projectId || void 0;
|
|
@@ -459,7 +467,6 @@ var Invite = import_mongoose3.default.model("Invite", InviteSchema);
|
|
|
459
467
|
// src/services/auth-admin.service.ts
|
|
460
468
|
var import_bcrypt = __toESM(require("bcrypt"), 1);
|
|
461
469
|
var import_jsonwebtoken2 = __toESM(require("jsonwebtoken"), 1);
|
|
462
|
-
var import_uuid2 = require("uuid");
|
|
463
470
|
|
|
464
471
|
// src/models/client.model.ts
|
|
465
472
|
var import_mongoose4 = __toESM(require("mongoose"), 1);
|
|
@@ -529,7 +536,7 @@ var AuthAdminService = class {
|
|
|
529
536
|
async createUserInRealm(payload) {
|
|
530
537
|
const hashedPassword = payload.credentials?.[0]?.value ? await import_bcrypt.default.hash(payload.credentials[0].value, 10) : void 0;
|
|
531
538
|
const user = await OrgUser.create({
|
|
532
|
-
id:
|
|
539
|
+
id: crypto.randomUUID(),
|
|
533
540
|
email: payload.email,
|
|
534
541
|
firstName: payload.firstName,
|
|
535
542
|
lastName: payload.lastName,
|
|
@@ -593,24 +600,61 @@ var EmailService = class {
|
|
|
593
600
|
host: process.env.EMAIL_HOST || "smtp.postmarkapp.com",
|
|
594
601
|
port: process.env.EMAIL_PORT ? Number(process.env.EMAIL_PORT) : 587,
|
|
595
602
|
secure: (process.env.EMAIL_SECURE || "false") === "true",
|
|
596
|
-
auth: {
|
|
603
|
+
auth: {
|
|
604
|
+
user: process.env.EMAIL_USER,
|
|
605
|
+
pass: process.env.EMAIL_PASSWORD
|
|
606
|
+
}
|
|
597
607
|
});
|
|
598
608
|
}
|
|
599
609
|
sign(payload, ttlSec = 60 * 60 * 24) {
|
|
600
|
-
return import_jsonwebtoken3.default.sign(payload, process.env.EMAIL_JWT_SECRET, {
|
|
610
|
+
return import_jsonwebtoken3.default.sign(payload, process.env.EMAIL_JWT_SECRET, {
|
|
611
|
+
expiresIn: ttlSec
|
|
612
|
+
});
|
|
601
613
|
}
|
|
602
614
|
verify(token) {
|
|
603
615
|
return import_jsonwebtoken3.default.verify(token, process.env.EMAIL_JWT_SECRET);
|
|
604
616
|
}
|
|
605
617
|
async send(to, subject, html) {
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
618
|
+
console.log("[EmailService] Attempting to send:", { to, subject });
|
|
619
|
+
try {
|
|
620
|
+
const info = await this.transporter.sendMail({
|
|
621
|
+
from: process.env.EMAIL_FROM,
|
|
622
|
+
to,
|
|
623
|
+
subject,
|
|
624
|
+
html
|
|
625
|
+
});
|
|
626
|
+
console.log("[EmailService] \u2705 Email sent successfully:", {
|
|
627
|
+
messageId: info.messageId,
|
|
628
|
+
response: info.response,
|
|
629
|
+
accepted: info.accepted,
|
|
630
|
+
rejected: info.rejected
|
|
631
|
+
});
|
|
632
|
+
return info;
|
|
633
|
+
} catch (error) {
|
|
634
|
+
console.error("[EmailService] \u274C Failed to send email:", {
|
|
635
|
+
message: error.message,
|
|
636
|
+
code: error.code,
|
|
637
|
+
command: error.command,
|
|
638
|
+
responseCode: error.responseCode,
|
|
639
|
+
response: error.response,
|
|
640
|
+
stack: error.stack
|
|
641
|
+
});
|
|
642
|
+
throw error;
|
|
643
|
+
}
|
|
612
644
|
}
|
|
613
645
|
canSend(lastEmailSent) {
|
|
646
|
+
console.log(
|
|
647
|
+
process.env.EMAIL_PASSWORD,
|
|
648
|
+
"pssword",
|
|
649
|
+
process.env.EMAIL_USER,
|
|
650
|
+
"user",
|
|
651
|
+
process.env.EMAIL_SECURE,
|
|
652
|
+
"secure",
|
|
653
|
+
process.env.EMAIL_PORT,
|
|
654
|
+
"porat",
|
|
655
|
+
process.env.EMAIL_HOST,
|
|
656
|
+
"hosat"
|
|
657
|
+
);
|
|
614
658
|
const now = Date.now();
|
|
615
659
|
const windowStart = now - this.WINDOW_MINUTES * 60 * 1e3;
|
|
616
660
|
const emailsInWindow = (lastEmailSent || []).map((d) => new Date(d)).filter((d) => d.getTime() >= windowStart);
|
|
@@ -1002,7 +1046,11 @@ function createAuthRouter(options = {}) {
|
|
|
1002
1046
|
if (!isGoogleEnabled) {
|
|
1003
1047
|
return res.status(500).json({ error: "Google login not configured" });
|
|
1004
1048
|
}
|
|
1005
|
-
const
|
|
1049
|
+
const stateData = {
|
|
1050
|
+
redirectTo: req.query.redirectTo || "",
|
|
1051
|
+
projectId: req.query.projectId || process.env.DEFAULT_PROJECT_ID || ""
|
|
1052
|
+
};
|
|
1053
|
+
const state = encodeURIComponent(JSON.stringify(stateData));
|
|
1006
1054
|
const params = new URLSearchParams({
|
|
1007
1055
|
client_id: googleClientId,
|
|
1008
1056
|
redirect_uri: googleRedirectUri,
|
|
@@ -1013,6 +1061,7 @@ function createAuthRouter(options = {}) {
|
|
|
1013
1061
|
state
|
|
1014
1062
|
});
|
|
1015
1063
|
const url = `https://accounts.google.com/o/oauth2/v2/auth?${params.toString()}`;
|
|
1064
|
+
console.log(url, "url");
|
|
1016
1065
|
res.redirect(url);
|
|
1017
1066
|
});
|
|
1018
1067
|
r.get("/google/callback", async (req, res) => {
|
|
@@ -1020,7 +1069,21 @@ function createAuthRouter(options = {}) {
|
|
|
1020
1069
|
return res.status(500).json({ error: "Google login not configured" });
|
|
1021
1070
|
}
|
|
1022
1071
|
const code = String(req.query.code || "");
|
|
1023
|
-
|
|
1072
|
+
let stateData = { redirectTo: "", projectId: "" };
|
|
1073
|
+
try {
|
|
1074
|
+
if (req.query.state) {
|
|
1075
|
+
stateData = JSON.parse(decodeURIComponent(String(req.query.state)));
|
|
1076
|
+
}
|
|
1077
|
+
} catch (err) {
|
|
1078
|
+
console.error("Failed to parse state:", err);
|
|
1079
|
+
}
|
|
1080
|
+
const { redirectTo, projectId } = stateData;
|
|
1081
|
+
console.log(
|
|
1082
|
+
"Parsed state - redirectTo:",
|
|
1083
|
+
redirectTo,
|
|
1084
|
+
"projectId:",
|
|
1085
|
+
projectId
|
|
1086
|
+
);
|
|
1024
1087
|
if (!code) {
|
|
1025
1088
|
return res.status(400).json({ ok: false, error: "Missing authorization code" });
|
|
1026
1089
|
}
|
|
@@ -1055,13 +1118,19 @@ function createAuthRouter(options = {}) {
|
|
|
1055
1118
|
const lastName = decoded.family_name || "";
|
|
1056
1119
|
let user = await OrgUser.findOne({ email: email2 }).lean();
|
|
1057
1120
|
if (!user) {
|
|
1121
|
+
const finalProjectId = projectId || process.env.DEFAULT_PROJECT_ID;
|
|
1122
|
+
if (!finalProjectId) {
|
|
1123
|
+
console.error("No projectId available for new user");
|
|
1124
|
+
const errorRedirect = (redirectTo || googleDefaultRedirect) + (redirectTo?.includes("?") ? "&" : "?") + "error=missing_project_id";
|
|
1125
|
+
return res.redirect(errorRedirect);
|
|
1126
|
+
}
|
|
1058
1127
|
const created = await OrgUser.create({
|
|
1059
1128
|
email: email2,
|
|
1060
1129
|
firstName,
|
|
1061
1130
|
lastName,
|
|
1062
1131
|
emailVerified,
|
|
1063
1132
|
roles: ["platform_user"],
|
|
1064
|
-
projectId:
|
|
1133
|
+
projectId: finalProjectId,
|
|
1065
1134
|
metadata: []
|
|
1066
1135
|
// you can also store googleId: decoded.sub
|
|
1067
1136
|
});
|
|
@@ -1069,8 +1138,14 @@ function createAuthRouter(options = {}) {
|
|
|
1069
1138
|
}
|
|
1070
1139
|
const tokens = generateTokens(user);
|
|
1071
1140
|
setAuthCookies(res, tokens, cookieConfig);
|
|
1072
|
-
|
|
1073
|
-
|
|
1141
|
+
if (user.projectId) {
|
|
1142
|
+
res.cookie(options.projectCookieName || "projectId", user.projectId, {
|
|
1143
|
+
...baseProjectCookieOptionsFrom(cookieConfig),
|
|
1144
|
+
httpOnly: true
|
|
1145
|
+
});
|
|
1146
|
+
}
|
|
1147
|
+
const finalRedirect = redirectTo || googleDefaultRedirect;
|
|
1148
|
+
res.redirect(finalRedirect);
|
|
1074
1149
|
} catch (err) {
|
|
1075
1150
|
console.error("Google callback error", err);
|
|
1076
1151
|
const redirectError = googleDefaultRedirect.includes("?") ? `${googleDefaultRedirect}&error=google_login_failed` : `${googleDefaultRedirect}?error=google_login_failed`;
|
|
@@ -1242,6 +1317,7 @@ async function sendRateLimitedEmail({
|
|
|
1242
1317
|
if (!can.ok) {
|
|
1243
1318
|
return { rateLimited: true, waitMs: can.waitMs };
|
|
1244
1319
|
}
|
|
1320
|
+
console.log(can, "can");
|
|
1245
1321
|
await emailService.send(user.email, subject, html);
|
|
1246
1322
|
user.lastEmailSent = [...user.lastEmailSent || [], /* @__PURE__ */ new Date()];
|
|
1247
1323
|
await user.save();
|
|
@@ -1255,6 +1331,11 @@ function generateTokens(user) {
|
|
|
1255
1331
|
orgId: user.orgId || null,
|
|
1256
1332
|
org_id: user.orgId || null,
|
|
1257
1333
|
projectId: user.projectId || null,
|
|
1334
|
+
firstName: user.firstName,
|
|
1335
|
+
lastName: user.lastName,
|
|
1336
|
+
emailVerified: user.emailVerified,
|
|
1337
|
+
createdAt: user.createdAt,
|
|
1338
|
+
metadata: user.metadata,
|
|
1258
1339
|
type: "user"
|
|
1259
1340
|
};
|
|
1260
1341
|
const accessToken = import_jsonwebtoken4.default.sign(accessPayload, process.env.JWT_SECRET, {
|