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/nest/index.js
CHANGED
|
@@ -144,10 +144,14 @@ function buildSession(payload) {
|
|
|
144
144
|
roles: normalizedRoles,
|
|
145
145
|
permissions
|
|
146
146
|
};
|
|
147
|
+
if (payload?.firstName) session.firstName = payload.firstName;
|
|
148
|
+
if (payload?.lastName) session.lastName = payload.lastName;
|
|
147
149
|
if (payload?.projectId) session.projectId = payload.projectId;
|
|
148
150
|
if (payload?.orgId) session.orgId = payload.orgId;
|
|
149
151
|
if (payload?.org_id) session.org_id = payload.org_id;
|
|
150
152
|
if (payload?.authType) session.authType = payload.authType;
|
|
153
|
+
if (payload?.createdAt) session.createdAt = payload.createdAt;
|
|
154
|
+
if (payload?.metadata) session.metadata = payload.metadata;
|
|
151
155
|
Object.keys(payload || {}).forEach((key) => {
|
|
152
156
|
if (![
|
|
153
157
|
"sub",
|
|
@@ -321,10 +325,14 @@ function requireAuth() {
|
|
|
321
325
|
const session = buildSession({
|
|
322
326
|
sub: user.id.toString(),
|
|
323
327
|
email: user.email,
|
|
328
|
+
firstName: user.firstName,
|
|
329
|
+
lastName: user.lastName,
|
|
330
|
+
metadata: user.metadata || [],
|
|
324
331
|
roles: user.roles || [],
|
|
325
332
|
orgId: user.orgId,
|
|
326
333
|
org_id: user.orgId,
|
|
327
|
-
projectId: user.projectId
|
|
334
|
+
projectId: user.projectId,
|
|
335
|
+
createdAt: user.createdAt
|
|
328
336
|
});
|
|
329
337
|
session.authType = "api-key";
|
|
330
338
|
session.projectId = readProjectId(req) || user.projectId || void 0;
|
|
@@ -421,7 +429,6 @@ var Invite = mongoose3.model("Invite", InviteSchema);
|
|
|
421
429
|
// src/services/auth-admin.service.ts
|
|
422
430
|
import bcrypt from "bcrypt";
|
|
423
431
|
import jwt2 from "jsonwebtoken";
|
|
424
|
-
import { v4 as uuid2 } from "uuid";
|
|
425
432
|
|
|
426
433
|
// src/models/client.model.ts
|
|
427
434
|
import mongoose4, { Schema as Schema2 } from "mongoose";
|
|
@@ -491,7 +498,7 @@ var AuthAdminService = class {
|
|
|
491
498
|
async createUserInRealm(payload) {
|
|
492
499
|
const hashedPassword = payload.credentials?.[0]?.value ? await bcrypt.hash(payload.credentials[0].value, 10) : void 0;
|
|
493
500
|
const user = await OrgUser.create({
|
|
494
|
-
id:
|
|
501
|
+
id: crypto.randomUUID(),
|
|
495
502
|
email: payload.email,
|
|
496
503
|
firstName: payload.firstName,
|
|
497
504
|
lastName: payload.lastName,
|
|
@@ -555,24 +562,61 @@ var EmailService = class {
|
|
|
555
562
|
host: process.env.EMAIL_HOST || "smtp.postmarkapp.com",
|
|
556
563
|
port: process.env.EMAIL_PORT ? Number(process.env.EMAIL_PORT) : 587,
|
|
557
564
|
secure: (process.env.EMAIL_SECURE || "false") === "true",
|
|
558
|
-
auth: {
|
|
565
|
+
auth: {
|
|
566
|
+
user: process.env.EMAIL_USER,
|
|
567
|
+
pass: process.env.EMAIL_PASSWORD
|
|
568
|
+
}
|
|
559
569
|
});
|
|
560
570
|
}
|
|
561
571
|
sign(payload, ttlSec = 60 * 60 * 24) {
|
|
562
|
-
return jwt3.sign(payload, process.env.EMAIL_JWT_SECRET, {
|
|
572
|
+
return jwt3.sign(payload, process.env.EMAIL_JWT_SECRET, {
|
|
573
|
+
expiresIn: ttlSec
|
|
574
|
+
});
|
|
563
575
|
}
|
|
564
576
|
verify(token) {
|
|
565
577
|
return jwt3.verify(token, process.env.EMAIL_JWT_SECRET);
|
|
566
578
|
}
|
|
567
579
|
async send(to, subject, html) {
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
580
|
+
console.log("[EmailService] Attempting to send:", { to, subject });
|
|
581
|
+
try {
|
|
582
|
+
const info = await this.transporter.sendMail({
|
|
583
|
+
from: process.env.EMAIL_FROM,
|
|
584
|
+
to,
|
|
585
|
+
subject,
|
|
586
|
+
html
|
|
587
|
+
});
|
|
588
|
+
console.log("[EmailService] \u2705 Email sent successfully:", {
|
|
589
|
+
messageId: info.messageId,
|
|
590
|
+
response: info.response,
|
|
591
|
+
accepted: info.accepted,
|
|
592
|
+
rejected: info.rejected
|
|
593
|
+
});
|
|
594
|
+
return info;
|
|
595
|
+
} catch (error) {
|
|
596
|
+
console.error("[EmailService] \u274C Failed to send email:", {
|
|
597
|
+
message: error.message,
|
|
598
|
+
code: error.code,
|
|
599
|
+
command: error.command,
|
|
600
|
+
responseCode: error.responseCode,
|
|
601
|
+
response: error.response,
|
|
602
|
+
stack: error.stack
|
|
603
|
+
});
|
|
604
|
+
throw error;
|
|
605
|
+
}
|
|
574
606
|
}
|
|
575
607
|
canSend(lastEmailSent) {
|
|
608
|
+
console.log(
|
|
609
|
+
process.env.EMAIL_PASSWORD,
|
|
610
|
+
"pssword",
|
|
611
|
+
process.env.EMAIL_USER,
|
|
612
|
+
"user",
|
|
613
|
+
process.env.EMAIL_SECURE,
|
|
614
|
+
"secure",
|
|
615
|
+
process.env.EMAIL_PORT,
|
|
616
|
+
"porat",
|
|
617
|
+
process.env.EMAIL_HOST,
|
|
618
|
+
"hosat"
|
|
619
|
+
);
|
|
576
620
|
const now = Date.now();
|
|
577
621
|
const windowStart = now - this.WINDOW_MINUTES * 60 * 1e3;
|
|
578
622
|
const emailsInWindow = (lastEmailSent || []).map((d) => new Date(d)).filter((d) => d.getTime() >= windowStart);
|
|
@@ -964,7 +1008,11 @@ function createAuthRouter(options = {}) {
|
|
|
964
1008
|
if (!isGoogleEnabled) {
|
|
965
1009
|
return res.status(500).json({ error: "Google login not configured" });
|
|
966
1010
|
}
|
|
967
|
-
const
|
|
1011
|
+
const stateData = {
|
|
1012
|
+
redirectTo: req.query.redirectTo || "",
|
|
1013
|
+
projectId: req.query.projectId || process.env.DEFAULT_PROJECT_ID || ""
|
|
1014
|
+
};
|
|
1015
|
+
const state = encodeURIComponent(JSON.stringify(stateData));
|
|
968
1016
|
const params = new URLSearchParams({
|
|
969
1017
|
client_id: googleClientId,
|
|
970
1018
|
redirect_uri: googleRedirectUri,
|
|
@@ -975,6 +1023,7 @@ function createAuthRouter(options = {}) {
|
|
|
975
1023
|
state
|
|
976
1024
|
});
|
|
977
1025
|
const url = `https://accounts.google.com/o/oauth2/v2/auth?${params.toString()}`;
|
|
1026
|
+
console.log(url, "url");
|
|
978
1027
|
res.redirect(url);
|
|
979
1028
|
});
|
|
980
1029
|
r.get("/google/callback", async (req, res) => {
|
|
@@ -982,7 +1031,21 @@ function createAuthRouter(options = {}) {
|
|
|
982
1031
|
return res.status(500).json({ error: "Google login not configured" });
|
|
983
1032
|
}
|
|
984
1033
|
const code = String(req.query.code || "");
|
|
985
|
-
|
|
1034
|
+
let stateData = { redirectTo: "", projectId: "" };
|
|
1035
|
+
try {
|
|
1036
|
+
if (req.query.state) {
|
|
1037
|
+
stateData = JSON.parse(decodeURIComponent(String(req.query.state)));
|
|
1038
|
+
}
|
|
1039
|
+
} catch (err) {
|
|
1040
|
+
console.error("Failed to parse state:", err);
|
|
1041
|
+
}
|
|
1042
|
+
const { redirectTo, projectId } = stateData;
|
|
1043
|
+
console.log(
|
|
1044
|
+
"Parsed state - redirectTo:",
|
|
1045
|
+
redirectTo,
|
|
1046
|
+
"projectId:",
|
|
1047
|
+
projectId
|
|
1048
|
+
);
|
|
986
1049
|
if (!code) {
|
|
987
1050
|
return res.status(400).json({ ok: false, error: "Missing authorization code" });
|
|
988
1051
|
}
|
|
@@ -1017,13 +1080,19 @@ function createAuthRouter(options = {}) {
|
|
|
1017
1080
|
const lastName = decoded.family_name || "";
|
|
1018
1081
|
let user = await OrgUser.findOne({ email: email2 }).lean();
|
|
1019
1082
|
if (!user) {
|
|
1083
|
+
const finalProjectId = projectId || process.env.DEFAULT_PROJECT_ID;
|
|
1084
|
+
if (!finalProjectId) {
|
|
1085
|
+
console.error("No projectId available for new user");
|
|
1086
|
+
const errorRedirect = (redirectTo || googleDefaultRedirect) + (redirectTo?.includes("?") ? "&" : "?") + "error=missing_project_id";
|
|
1087
|
+
return res.redirect(errorRedirect);
|
|
1088
|
+
}
|
|
1020
1089
|
const created = await OrgUser.create({
|
|
1021
1090
|
email: email2,
|
|
1022
1091
|
firstName,
|
|
1023
1092
|
lastName,
|
|
1024
1093
|
emailVerified,
|
|
1025
1094
|
roles: ["platform_user"],
|
|
1026
|
-
projectId:
|
|
1095
|
+
projectId: finalProjectId,
|
|
1027
1096
|
metadata: []
|
|
1028
1097
|
// you can also store googleId: decoded.sub
|
|
1029
1098
|
});
|
|
@@ -1031,8 +1100,14 @@ function createAuthRouter(options = {}) {
|
|
|
1031
1100
|
}
|
|
1032
1101
|
const tokens = generateTokens(user);
|
|
1033
1102
|
setAuthCookies(res, tokens, cookieConfig);
|
|
1034
|
-
|
|
1035
|
-
|
|
1103
|
+
if (user.projectId) {
|
|
1104
|
+
res.cookie(options.projectCookieName || "projectId", user.projectId, {
|
|
1105
|
+
...baseProjectCookieOptionsFrom(cookieConfig),
|
|
1106
|
+
httpOnly: true
|
|
1107
|
+
});
|
|
1108
|
+
}
|
|
1109
|
+
const finalRedirect = redirectTo || googleDefaultRedirect;
|
|
1110
|
+
res.redirect(finalRedirect);
|
|
1036
1111
|
} catch (err) {
|
|
1037
1112
|
console.error("Google callback error", err);
|
|
1038
1113
|
const redirectError = googleDefaultRedirect.includes("?") ? `${googleDefaultRedirect}&error=google_login_failed` : `${googleDefaultRedirect}?error=google_login_failed`;
|
|
@@ -1204,6 +1279,7 @@ async function sendRateLimitedEmail({
|
|
|
1204
1279
|
if (!can.ok) {
|
|
1205
1280
|
return { rateLimited: true, waitMs: can.waitMs };
|
|
1206
1281
|
}
|
|
1282
|
+
console.log(can, "can");
|
|
1207
1283
|
await emailService.send(user.email, subject, html);
|
|
1208
1284
|
user.lastEmailSent = [...user.lastEmailSent || [], /* @__PURE__ */ new Date()];
|
|
1209
1285
|
await user.save();
|
|
@@ -1217,6 +1293,11 @@ function generateTokens(user) {
|
|
|
1217
1293
|
orgId: user.orgId || null,
|
|
1218
1294
|
org_id: user.orgId || null,
|
|
1219
1295
|
projectId: user.projectId || null,
|
|
1296
|
+
firstName: user.firstName,
|
|
1297
|
+
lastName: user.lastName,
|
|
1298
|
+
emailVerified: user.emailVerified,
|
|
1299
|
+
createdAt: user.createdAt,
|
|
1300
|
+
metadata: user.metadata,
|
|
1220
1301
|
type: "user"
|
|
1221
1302
|
};
|
|
1222
1303
|
const accessToken = jwt4.sign(accessPayload, process.env.JWT_SECRET, {
|