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/index.cjs
CHANGED
|
@@ -247,10 +247,14 @@ function buildSession(payload) {
|
|
|
247
247
|
roles: normalizedRoles,
|
|
248
248
|
permissions
|
|
249
249
|
};
|
|
250
|
+
if (payload?.firstName) session.firstName = payload.firstName;
|
|
251
|
+
if (payload?.lastName) session.lastName = payload.lastName;
|
|
250
252
|
if (payload?.projectId) session.projectId = payload.projectId;
|
|
251
253
|
if (payload?.orgId) session.orgId = payload.orgId;
|
|
252
254
|
if (payload?.org_id) session.org_id = payload.org_id;
|
|
253
255
|
if (payload?.authType) session.authType = payload.authType;
|
|
256
|
+
if (payload?.createdAt) session.createdAt = payload.createdAt;
|
|
257
|
+
if (payload?.metadata) session.metadata = payload.metadata;
|
|
254
258
|
Object.keys(payload || {}).forEach((key) => {
|
|
255
259
|
if (![
|
|
256
260
|
"sub",
|
|
@@ -424,10 +428,14 @@ function requireAuth() {
|
|
|
424
428
|
const session = buildSession({
|
|
425
429
|
sub: user.id.toString(),
|
|
426
430
|
email: user.email,
|
|
431
|
+
firstName: user.firstName,
|
|
432
|
+
lastName: user.lastName,
|
|
433
|
+
metadata: user.metadata || [],
|
|
427
434
|
roles: user.roles || [],
|
|
428
435
|
orgId: user.orgId,
|
|
429
436
|
org_id: user.orgId,
|
|
430
|
-
projectId: user.projectId
|
|
437
|
+
projectId: user.projectId,
|
|
438
|
+
createdAt: user.createdAt
|
|
431
439
|
});
|
|
432
440
|
session.authType = "api-key";
|
|
433
441
|
session.projectId = readProjectId(req) || user.projectId || void 0;
|
|
@@ -539,7 +547,6 @@ var Invite = import_mongoose3.default.model("Invite", InviteSchema);
|
|
|
539
547
|
// src/services/auth-admin.service.ts
|
|
540
548
|
var import_bcrypt = __toESM(require("bcrypt"), 1);
|
|
541
549
|
var import_jsonwebtoken2 = __toESM(require("jsonwebtoken"), 1);
|
|
542
|
-
var import_uuid2 = require("uuid");
|
|
543
550
|
|
|
544
551
|
// src/models/client.model.ts
|
|
545
552
|
var import_mongoose4 = __toESM(require("mongoose"), 1);
|
|
@@ -609,7 +616,7 @@ var AuthAdminService = class {
|
|
|
609
616
|
async createUserInRealm(payload) {
|
|
610
617
|
const hashedPassword = payload.credentials?.[0]?.value ? await import_bcrypt.default.hash(payload.credentials[0].value, 10) : void 0;
|
|
611
618
|
const user = await OrgUser.create({
|
|
612
|
-
id:
|
|
619
|
+
id: crypto.randomUUID(),
|
|
613
620
|
email: payload.email,
|
|
614
621
|
firstName: payload.firstName,
|
|
615
622
|
lastName: payload.lastName,
|
|
@@ -673,24 +680,61 @@ var EmailService = class {
|
|
|
673
680
|
host: process.env.EMAIL_HOST || "smtp.postmarkapp.com",
|
|
674
681
|
port: process.env.EMAIL_PORT ? Number(process.env.EMAIL_PORT) : 587,
|
|
675
682
|
secure: (process.env.EMAIL_SECURE || "false") === "true",
|
|
676
|
-
auth: {
|
|
683
|
+
auth: {
|
|
684
|
+
user: process.env.EMAIL_USER,
|
|
685
|
+
pass: process.env.EMAIL_PASSWORD
|
|
686
|
+
}
|
|
677
687
|
});
|
|
678
688
|
}
|
|
679
689
|
sign(payload, ttlSec = 60 * 60 * 24) {
|
|
680
|
-
return import_jsonwebtoken3.default.sign(payload, process.env.EMAIL_JWT_SECRET, {
|
|
690
|
+
return import_jsonwebtoken3.default.sign(payload, process.env.EMAIL_JWT_SECRET, {
|
|
691
|
+
expiresIn: ttlSec
|
|
692
|
+
});
|
|
681
693
|
}
|
|
682
694
|
verify(token) {
|
|
683
695
|
return import_jsonwebtoken3.default.verify(token, process.env.EMAIL_JWT_SECRET);
|
|
684
696
|
}
|
|
685
697
|
async send(to, subject, html) {
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
698
|
+
console.log("[EmailService] Attempting to send:", { to, subject });
|
|
699
|
+
try {
|
|
700
|
+
const info = await this.transporter.sendMail({
|
|
701
|
+
from: process.env.EMAIL_FROM,
|
|
702
|
+
to,
|
|
703
|
+
subject,
|
|
704
|
+
html
|
|
705
|
+
});
|
|
706
|
+
console.log("[EmailService] \u2705 Email sent successfully:", {
|
|
707
|
+
messageId: info.messageId,
|
|
708
|
+
response: info.response,
|
|
709
|
+
accepted: info.accepted,
|
|
710
|
+
rejected: info.rejected
|
|
711
|
+
});
|
|
712
|
+
return info;
|
|
713
|
+
} catch (error) {
|
|
714
|
+
console.error("[EmailService] \u274C Failed to send email:", {
|
|
715
|
+
message: error.message,
|
|
716
|
+
code: error.code,
|
|
717
|
+
command: error.command,
|
|
718
|
+
responseCode: error.responseCode,
|
|
719
|
+
response: error.response,
|
|
720
|
+
stack: error.stack
|
|
721
|
+
});
|
|
722
|
+
throw error;
|
|
723
|
+
}
|
|
692
724
|
}
|
|
693
725
|
canSend(lastEmailSent) {
|
|
726
|
+
console.log(
|
|
727
|
+
process.env.EMAIL_PASSWORD,
|
|
728
|
+
"pssword",
|
|
729
|
+
process.env.EMAIL_USER,
|
|
730
|
+
"user",
|
|
731
|
+
process.env.EMAIL_SECURE,
|
|
732
|
+
"secure",
|
|
733
|
+
process.env.EMAIL_PORT,
|
|
734
|
+
"porat",
|
|
735
|
+
process.env.EMAIL_HOST,
|
|
736
|
+
"hosat"
|
|
737
|
+
);
|
|
694
738
|
const now = Date.now();
|
|
695
739
|
const windowStart = now - this.WINDOW_MINUTES * 60 * 1e3;
|
|
696
740
|
const emailsInWindow = (lastEmailSent || []).map((d) => new Date(d)).filter((d) => d.getTime() >= windowStart);
|
|
@@ -1082,7 +1126,11 @@ function createAuthRouter(options = {}) {
|
|
|
1082
1126
|
if (!isGoogleEnabled) {
|
|
1083
1127
|
return res.status(500).json({ error: "Google login not configured" });
|
|
1084
1128
|
}
|
|
1085
|
-
const
|
|
1129
|
+
const stateData = {
|
|
1130
|
+
redirectTo: req.query.redirectTo || "",
|
|
1131
|
+
projectId: req.query.projectId || process.env.DEFAULT_PROJECT_ID || ""
|
|
1132
|
+
};
|
|
1133
|
+
const state = encodeURIComponent(JSON.stringify(stateData));
|
|
1086
1134
|
const params = new URLSearchParams({
|
|
1087
1135
|
client_id: googleClientId,
|
|
1088
1136
|
redirect_uri: googleRedirectUri,
|
|
@@ -1093,6 +1141,7 @@ function createAuthRouter(options = {}) {
|
|
|
1093
1141
|
state
|
|
1094
1142
|
});
|
|
1095
1143
|
const url = `https://accounts.google.com/o/oauth2/v2/auth?${params.toString()}`;
|
|
1144
|
+
console.log(url, "url");
|
|
1096
1145
|
res.redirect(url);
|
|
1097
1146
|
});
|
|
1098
1147
|
r.get("/google/callback", async (req, res) => {
|
|
@@ -1100,7 +1149,21 @@ function createAuthRouter(options = {}) {
|
|
|
1100
1149
|
return res.status(500).json({ error: "Google login not configured" });
|
|
1101
1150
|
}
|
|
1102
1151
|
const code = String(req.query.code || "");
|
|
1103
|
-
|
|
1152
|
+
let stateData = { redirectTo: "", projectId: "" };
|
|
1153
|
+
try {
|
|
1154
|
+
if (req.query.state) {
|
|
1155
|
+
stateData = JSON.parse(decodeURIComponent(String(req.query.state)));
|
|
1156
|
+
}
|
|
1157
|
+
} catch (err) {
|
|
1158
|
+
console.error("Failed to parse state:", err);
|
|
1159
|
+
}
|
|
1160
|
+
const { redirectTo, projectId } = stateData;
|
|
1161
|
+
console.log(
|
|
1162
|
+
"Parsed state - redirectTo:",
|
|
1163
|
+
redirectTo,
|
|
1164
|
+
"projectId:",
|
|
1165
|
+
projectId
|
|
1166
|
+
);
|
|
1104
1167
|
if (!code) {
|
|
1105
1168
|
return res.status(400).json({ ok: false, error: "Missing authorization code" });
|
|
1106
1169
|
}
|
|
@@ -1135,13 +1198,19 @@ function createAuthRouter(options = {}) {
|
|
|
1135
1198
|
const lastName = decoded.family_name || "";
|
|
1136
1199
|
let user = await OrgUser.findOne({ email: email2 }).lean();
|
|
1137
1200
|
if (!user) {
|
|
1201
|
+
const finalProjectId = projectId || process.env.DEFAULT_PROJECT_ID;
|
|
1202
|
+
if (!finalProjectId) {
|
|
1203
|
+
console.error("No projectId available for new user");
|
|
1204
|
+
const errorRedirect = (redirectTo || googleDefaultRedirect) + (redirectTo?.includes("?") ? "&" : "?") + "error=missing_project_id";
|
|
1205
|
+
return res.redirect(errorRedirect);
|
|
1206
|
+
}
|
|
1138
1207
|
const created = await OrgUser.create({
|
|
1139
1208
|
email: email2,
|
|
1140
1209
|
firstName,
|
|
1141
1210
|
lastName,
|
|
1142
1211
|
emailVerified,
|
|
1143
1212
|
roles: ["platform_user"],
|
|
1144
|
-
projectId:
|
|
1213
|
+
projectId: finalProjectId,
|
|
1145
1214
|
metadata: []
|
|
1146
1215
|
// you can also store googleId: decoded.sub
|
|
1147
1216
|
});
|
|
@@ -1149,8 +1218,14 @@ function createAuthRouter(options = {}) {
|
|
|
1149
1218
|
}
|
|
1150
1219
|
const tokens = generateTokens(user);
|
|
1151
1220
|
setAuthCookies(res, tokens, cookieConfig);
|
|
1152
|
-
|
|
1153
|
-
|
|
1221
|
+
if (user.projectId) {
|
|
1222
|
+
res.cookie(options.projectCookieName || "projectId", user.projectId, {
|
|
1223
|
+
...baseProjectCookieOptionsFrom(cookieConfig),
|
|
1224
|
+
httpOnly: true
|
|
1225
|
+
});
|
|
1226
|
+
}
|
|
1227
|
+
const finalRedirect = redirectTo || googleDefaultRedirect;
|
|
1228
|
+
res.redirect(finalRedirect);
|
|
1154
1229
|
} catch (err) {
|
|
1155
1230
|
console.error("Google callback error", err);
|
|
1156
1231
|
const redirectError = googleDefaultRedirect.includes("?") ? `${googleDefaultRedirect}&error=google_login_failed` : `${googleDefaultRedirect}?error=google_login_failed`;
|
|
@@ -1322,6 +1397,7 @@ async function sendRateLimitedEmail({
|
|
|
1322
1397
|
if (!can.ok) {
|
|
1323
1398
|
return { rateLimited: true, waitMs: can.waitMs };
|
|
1324
1399
|
}
|
|
1400
|
+
console.log(can, "can");
|
|
1325
1401
|
await emailService.send(user.email, subject, html);
|
|
1326
1402
|
user.lastEmailSent = [...user.lastEmailSent || [], /* @__PURE__ */ new Date()];
|
|
1327
1403
|
await user.save();
|
|
@@ -1335,6 +1411,11 @@ function generateTokens(user) {
|
|
|
1335
1411
|
orgId: user.orgId || null,
|
|
1336
1412
|
org_id: user.orgId || null,
|
|
1337
1413
|
projectId: user.projectId || null,
|
|
1414
|
+
firstName: user.firstName,
|
|
1415
|
+
lastName: user.lastName,
|
|
1416
|
+
emailVerified: user.emailVerified,
|
|
1417
|
+
createdAt: user.createdAt,
|
|
1418
|
+
metadata: user.metadata,
|
|
1338
1419
|
type: "user"
|
|
1339
1420
|
};
|
|
1340
1421
|
const accessToken = import_jsonwebtoken4.default.sign(accessPayload, process.env.JWT_SECRET, {
|