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.js
CHANGED
|
@@ -195,10 +195,14 @@ function buildSession(payload) {
|
|
|
195
195
|
roles: normalizedRoles,
|
|
196
196
|
permissions
|
|
197
197
|
};
|
|
198
|
+
if (payload?.firstName) session.firstName = payload.firstName;
|
|
199
|
+
if (payload?.lastName) session.lastName = payload.lastName;
|
|
198
200
|
if (payload?.projectId) session.projectId = payload.projectId;
|
|
199
201
|
if (payload?.orgId) session.orgId = payload.orgId;
|
|
200
202
|
if (payload?.org_id) session.org_id = payload.org_id;
|
|
201
203
|
if (payload?.authType) session.authType = payload.authType;
|
|
204
|
+
if (payload?.createdAt) session.createdAt = payload.createdAt;
|
|
205
|
+
if (payload?.metadata) session.metadata = payload.metadata;
|
|
202
206
|
Object.keys(payload || {}).forEach((key) => {
|
|
203
207
|
if (![
|
|
204
208
|
"sub",
|
|
@@ -372,10 +376,14 @@ function requireAuth() {
|
|
|
372
376
|
const session = buildSession({
|
|
373
377
|
sub: user.id.toString(),
|
|
374
378
|
email: user.email,
|
|
379
|
+
firstName: user.firstName,
|
|
380
|
+
lastName: user.lastName,
|
|
381
|
+
metadata: user.metadata || [],
|
|
375
382
|
roles: user.roles || [],
|
|
376
383
|
orgId: user.orgId,
|
|
377
384
|
org_id: user.orgId,
|
|
378
|
-
projectId: user.projectId
|
|
385
|
+
projectId: user.projectId,
|
|
386
|
+
createdAt: user.createdAt
|
|
379
387
|
});
|
|
380
388
|
session.authType = "api-key";
|
|
381
389
|
session.projectId = readProjectId(req) || user.projectId || void 0;
|
|
@@ -487,7 +495,6 @@ var Invite = mongoose3.model("Invite", InviteSchema);
|
|
|
487
495
|
// src/services/auth-admin.service.ts
|
|
488
496
|
import bcrypt from "bcrypt";
|
|
489
497
|
import jwt2 from "jsonwebtoken";
|
|
490
|
-
import { v4 as uuid2 } from "uuid";
|
|
491
498
|
|
|
492
499
|
// src/models/client.model.ts
|
|
493
500
|
import mongoose4, { Schema as Schema2 } from "mongoose";
|
|
@@ -557,7 +564,7 @@ var AuthAdminService = class {
|
|
|
557
564
|
async createUserInRealm(payload) {
|
|
558
565
|
const hashedPassword = payload.credentials?.[0]?.value ? await bcrypt.hash(payload.credentials[0].value, 10) : void 0;
|
|
559
566
|
const user = await OrgUser.create({
|
|
560
|
-
id:
|
|
567
|
+
id: crypto.randomUUID(),
|
|
561
568
|
email: payload.email,
|
|
562
569
|
firstName: payload.firstName,
|
|
563
570
|
lastName: payload.lastName,
|
|
@@ -621,24 +628,61 @@ var EmailService = class {
|
|
|
621
628
|
host: process.env.EMAIL_HOST || "smtp.postmarkapp.com",
|
|
622
629
|
port: process.env.EMAIL_PORT ? Number(process.env.EMAIL_PORT) : 587,
|
|
623
630
|
secure: (process.env.EMAIL_SECURE || "false") === "true",
|
|
624
|
-
auth: {
|
|
631
|
+
auth: {
|
|
632
|
+
user: process.env.EMAIL_USER,
|
|
633
|
+
pass: process.env.EMAIL_PASSWORD
|
|
634
|
+
}
|
|
625
635
|
});
|
|
626
636
|
}
|
|
627
637
|
sign(payload, ttlSec = 60 * 60 * 24) {
|
|
628
|
-
return jwt3.sign(payload, process.env.EMAIL_JWT_SECRET, {
|
|
638
|
+
return jwt3.sign(payload, process.env.EMAIL_JWT_SECRET, {
|
|
639
|
+
expiresIn: ttlSec
|
|
640
|
+
});
|
|
629
641
|
}
|
|
630
642
|
verify(token) {
|
|
631
643
|
return jwt3.verify(token, process.env.EMAIL_JWT_SECRET);
|
|
632
644
|
}
|
|
633
645
|
async send(to, subject, html) {
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
646
|
+
console.log("[EmailService] Attempting to send:", { to, subject });
|
|
647
|
+
try {
|
|
648
|
+
const info = await this.transporter.sendMail({
|
|
649
|
+
from: process.env.EMAIL_FROM,
|
|
650
|
+
to,
|
|
651
|
+
subject,
|
|
652
|
+
html
|
|
653
|
+
});
|
|
654
|
+
console.log("[EmailService] \u2705 Email sent successfully:", {
|
|
655
|
+
messageId: info.messageId,
|
|
656
|
+
response: info.response,
|
|
657
|
+
accepted: info.accepted,
|
|
658
|
+
rejected: info.rejected
|
|
659
|
+
});
|
|
660
|
+
return info;
|
|
661
|
+
} catch (error) {
|
|
662
|
+
console.error("[EmailService] \u274C Failed to send email:", {
|
|
663
|
+
message: error.message,
|
|
664
|
+
code: error.code,
|
|
665
|
+
command: error.command,
|
|
666
|
+
responseCode: error.responseCode,
|
|
667
|
+
response: error.response,
|
|
668
|
+
stack: error.stack
|
|
669
|
+
});
|
|
670
|
+
throw error;
|
|
671
|
+
}
|
|
640
672
|
}
|
|
641
673
|
canSend(lastEmailSent) {
|
|
674
|
+
console.log(
|
|
675
|
+
process.env.EMAIL_PASSWORD,
|
|
676
|
+
"pssword",
|
|
677
|
+
process.env.EMAIL_USER,
|
|
678
|
+
"user",
|
|
679
|
+
process.env.EMAIL_SECURE,
|
|
680
|
+
"secure",
|
|
681
|
+
process.env.EMAIL_PORT,
|
|
682
|
+
"porat",
|
|
683
|
+
process.env.EMAIL_HOST,
|
|
684
|
+
"hosat"
|
|
685
|
+
);
|
|
642
686
|
const now = Date.now();
|
|
643
687
|
const windowStart = now - this.WINDOW_MINUTES * 60 * 1e3;
|
|
644
688
|
const emailsInWindow = (lastEmailSent || []).map((d) => new Date(d)).filter((d) => d.getTime() >= windowStart);
|
|
@@ -1030,7 +1074,11 @@ function createAuthRouter(options = {}) {
|
|
|
1030
1074
|
if (!isGoogleEnabled) {
|
|
1031
1075
|
return res.status(500).json({ error: "Google login not configured" });
|
|
1032
1076
|
}
|
|
1033
|
-
const
|
|
1077
|
+
const stateData = {
|
|
1078
|
+
redirectTo: req.query.redirectTo || "",
|
|
1079
|
+
projectId: req.query.projectId || process.env.DEFAULT_PROJECT_ID || ""
|
|
1080
|
+
};
|
|
1081
|
+
const state = encodeURIComponent(JSON.stringify(stateData));
|
|
1034
1082
|
const params = new URLSearchParams({
|
|
1035
1083
|
client_id: googleClientId,
|
|
1036
1084
|
redirect_uri: googleRedirectUri,
|
|
@@ -1041,6 +1089,7 @@ function createAuthRouter(options = {}) {
|
|
|
1041
1089
|
state
|
|
1042
1090
|
});
|
|
1043
1091
|
const url = `https://accounts.google.com/o/oauth2/v2/auth?${params.toString()}`;
|
|
1092
|
+
console.log(url, "url");
|
|
1044
1093
|
res.redirect(url);
|
|
1045
1094
|
});
|
|
1046
1095
|
r.get("/google/callback", async (req, res) => {
|
|
@@ -1048,7 +1097,21 @@ function createAuthRouter(options = {}) {
|
|
|
1048
1097
|
return res.status(500).json({ error: "Google login not configured" });
|
|
1049
1098
|
}
|
|
1050
1099
|
const code = String(req.query.code || "");
|
|
1051
|
-
|
|
1100
|
+
let stateData = { redirectTo: "", projectId: "" };
|
|
1101
|
+
try {
|
|
1102
|
+
if (req.query.state) {
|
|
1103
|
+
stateData = JSON.parse(decodeURIComponent(String(req.query.state)));
|
|
1104
|
+
}
|
|
1105
|
+
} catch (err) {
|
|
1106
|
+
console.error("Failed to parse state:", err);
|
|
1107
|
+
}
|
|
1108
|
+
const { redirectTo, projectId } = stateData;
|
|
1109
|
+
console.log(
|
|
1110
|
+
"Parsed state - redirectTo:",
|
|
1111
|
+
redirectTo,
|
|
1112
|
+
"projectId:",
|
|
1113
|
+
projectId
|
|
1114
|
+
);
|
|
1052
1115
|
if (!code) {
|
|
1053
1116
|
return res.status(400).json({ ok: false, error: "Missing authorization code" });
|
|
1054
1117
|
}
|
|
@@ -1083,13 +1146,19 @@ function createAuthRouter(options = {}) {
|
|
|
1083
1146
|
const lastName = decoded.family_name || "";
|
|
1084
1147
|
let user = await OrgUser.findOne({ email: email2 }).lean();
|
|
1085
1148
|
if (!user) {
|
|
1149
|
+
const finalProjectId = projectId || process.env.DEFAULT_PROJECT_ID;
|
|
1150
|
+
if (!finalProjectId) {
|
|
1151
|
+
console.error("No projectId available for new user");
|
|
1152
|
+
const errorRedirect = (redirectTo || googleDefaultRedirect) + (redirectTo?.includes("?") ? "&" : "?") + "error=missing_project_id";
|
|
1153
|
+
return res.redirect(errorRedirect);
|
|
1154
|
+
}
|
|
1086
1155
|
const created = await OrgUser.create({
|
|
1087
1156
|
email: email2,
|
|
1088
1157
|
firstName,
|
|
1089
1158
|
lastName,
|
|
1090
1159
|
emailVerified,
|
|
1091
1160
|
roles: ["platform_user"],
|
|
1092
|
-
projectId:
|
|
1161
|
+
projectId: finalProjectId,
|
|
1093
1162
|
metadata: []
|
|
1094
1163
|
// you can also store googleId: decoded.sub
|
|
1095
1164
|
});
|
|
@@ -1097,8 +1166,14 @@ function createAuthRouter(options = {}) {
|
|
|
1097
1166
|
}
|
|
1098
1167
|
const tokens = generateTokens(user);
|
|
1099
1168
|
setAuthCookies(res, tokens, cookieConfig);
|
|
1100
|
-
|
|
1101
|
-
|
|
1169
|
+
if (user.projectId) {
|
|
1170
|
+
res.cookie(options.projectCookieName || "projectId", user.projectId, {
|
|
1171
|
+
...baseProjectCookieOptionsFrom(cookieConfig),
|
|
1172
|
+
httpOnly: true
|
|
1173
|
+
});
|
|
1174
|
+
}
|
|
1175
|
+
const finalRedirect = redirectTo || googleDefaultRedirect;
|
|
1176
|
+
res.redirect(finalRedirect);
|
|
1102
1177
|
} catch (err) {
|
|
1103
1178
|
console.error("Google callback error", err);
|
|
1104
1179
|
const redirectError = googleDefaultRedirect.includes("?") ? `${googleDefaultRedirect}&error=google_login_failed` : `${googleDefaultRedirect}?error=google_login_failed`;
|
|
@@ -1270,6 +1345,7 @@ async function sendRateLimitedEmail({
|
|
|
1270
1345
|
if (!can.ok) {
|
|
1271
1346
|
return { rateLimited: true, waitMs: can.waitMs };
|
|
1272
1347
|
}
|
|
1348
|
+
console.log(can, "can");
|
|
1273
1349
|
await emailService.send(user.email, subject, html);
|
|
1274
1350
|
user.lastEmailSent = [...user.lastEmailSent || [], /* @__PURE__ */ new Date()];
|
|
1275
1351
|
await user.save();
|
|
@@ -1283,6 +1359,11 @@ function generateTokens(user) {
|
|
|
1283
1359
|
orgId: user.orgId || null,
|
|
1284
1360
|
org_id: user.orgId || null,
|
|
1285
1361
|
projectId: user.projectId || null,
|
|
1362
|
+
firstName: user.firstName,
|
|
1363
|
+
lastName: user.lastName,
|
|
1364
|
+
emailVerified: user.emailVerified,
|
|
1365
|
+
createdAt: user.createdAt,
|
|
1366
|
+
metadata: user.metadata,
|
|
1286
1367
|
type: "user"
|
|
1287
1368
|
};
|
|
1288
1369
|
const accessToken = jwt4.sign(accessPayload, process.env.JWT_SECRET, {
|