aaspai-authx 0.1.2 → 0.1.4

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.
@@ -600,24 +600,61 @@ var EmailService = class {
600
600
  host: process.env.EMAIL_HOST || "smtp.postmarkapp.com",
601
601
  port: process.env.EMAIL_PORT ? Number(process.env.EMAIL_PORT) : 587,
602
602
  secure: (process.env.EMAIL_SECURE || "false") === "true",
603
- auth: { user: process.env.EMAIL_USER, pass: process.env.EMAIL_PASSWORD }
603
+ auth: {
604
+ user: process.env.EMAIL_USER,
605
+ pass: process.env.EMAIL_PASSWORD
606
+ }
604
607
  });
605
608
  }
606
609
  sign(payload, ttlSec = 60 * 60 * 24) {
607
- return import_jsonwebtoken3.default.sign(payload, process.env.EMAIL_JWT_SECRET, { expiresIn: ttlSec });
610
+ return import_jsonwebtoken3.default.sign(payload, process.env.EMAIL_JWT_SECRET, {
611
+ expiresIn: ttlSec
612
+ });
608
613
  }
609
614
  verify(token) {
610
615
  return import_jsonwebtoken3.default.verify(token, process.env.EMAIL_JWT_SECRET);
611
616
  }
612
617
  async send(to, subject, html) {
613
- await this.transporter.sendMail({
614
- from: process.env.EMAIL_FROM,
615
- to,
616
- subject,
617
- html
618
- });
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
+ }
619
644
  }
620
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
+ );
621
658
  const now = Date.now();
622
659
  const windowStart = now - this.WINDOW_MINUTES * 60 * 1e3;
623
660
  const emailsInWindow = (lastEmailSent || []).map((d) => new Date(d)).filter((d) => d.getTime() >= windowStart);
@@ -662,10 +699,8 @@ function createAuthRouter(options = {}) {
662
699
  );
663
700
  r.post("/login", validateLogin, async (req, res) => {
664
701
  const { email: emailAddress, password } = req.body || {};
665
- console.log(emailAddress, password, "body");
666
702
  try {
667
703
  const user = await OrgUser.findOne({ email: emailAddress }).select("+password").lean();
668
- console.log(user, "user");
669
704
  if (!user) {
670
705
  return res.status(400).json({
671
706
  error: "Invalid email or password",
@@ -1009,7 +1044,11 @@ function createAuthRouter(options = {}) {
1009
1044
  if (!isGoogleEnabled) {
1010
1045
  return res.status(500).json({ error: "Google login not configured" });
1011
1046
  }
1012
- const state = req.query.redirectTo ? encodeURIComponent(String(req.query.redirectTo)) : "";
1047
+ const stateData = {
1048
+ redirectTo: req.query.redirectTo || "",
1049
+ projectId: req.query.projectId || process.env.DEFAULT_PROJECT_ID || ""
1050
+ };
1051
+ const state = encodeURIComponent(JSON.stringify(stateData));
1013
1052
  const params = new URLSearchParams({
1014
1053
  client_id: googleClientId,
1015
1054
  redirect_uri: googleRedirectUri,
@@ -1020,6 +1059,7 @@ function createAuthRouter(options = {}) {
1020
1059
  state
1021
1060
  });
1022
1061
  const url = `https://accounts.google.com/o/oauth2/v2/auth?${params.toString()}`;
1062
+ console.log(url, "url");
1023
1063
  res.redirect(url);
1024
1064
  });
1025
1065
  r.get("/google/callback", async (req, res) => {
@@ -1027,7 +1067,21 @@ function createAuthRouter(options = {}) {
1027
1067
  return res.status(500).json({ error: "Google login not configured" });
1028
1068
  }
1029
1069
  const code = String(req.query.code || "");
1030
- const state = req.query.state ? String(req.query.state) : "";
1070
+ let stateData = { redirectTo: "", projectId: "" };
1071
+ try {
1072
+ if (req.query.state) {
1073
+ stateData = JSON.parse(decodeURIComponent(String(req.query.state)));
1074
+ }
1075
+ } catch (err) {
1076
+ console.error("Failed to parse state:", err);
1077
+ }
1078
+ const { redirectTo, projectId } = stateData;
1079
+ console.log(
1080
+ "Parsed state - redirectTo:",
1081
+ redirectTo,
1082
+ "projectId:",
1083
+ projectId
1084
+ );
1031
1085
  if (!code) {
1032
1086
  return res.status(400).json({ ok: false, error: "Missing authorization code" });
1033
1087
  }
@@ -1062,13 +1116,19 @@ function createAuthRouter(options = {}) {
1062
1116
  const lastName = decoded.family_name || "";
1063
1117
  let user = await OrgUser.findOne({ email: email2 }).lean();
1064
1118
  if (!user) {
1119
+ const finalProjectId = projectId || process.env.DEFAULT_PROJECT_ID;
1120
+ if (!finalProjectId) {
1121
+ console.error("No projectId available for new user");
1122
+ const errorRedirect = (redirectTo || googleDefaultRedirect) + (redirectTo?.includes("?") ? "&" : "?") + "error=missing_project_id";
1123
+ return res.redirect(errorRedirect);
1124
+ }
1065
1125
  const created = await OrgUser.create({
1066
1126
  email: email2,
1067
1127
  firstName,
1068
1128
  lastName,
1069
1129
  emailVerified,
1070
1130
  roles: ["platform_user"],
1071
- projectId: null,
1131
+ projectId: finalProjectId,
1072
1132
  metadata: []
1073
1133
  // you can also store googleId: decoded.sub
1074
1134
  });
@@ -1076,8 +1136,14 @@ function createAuthRouter(options = {}) {
1076
1136
  }
1077
1137
  const tokens = generateTokens(user);
1078
1138
  setAuthCookies(res, tokens, cookieConfig);
1079
- const redirectTo = state ? decodeURIComponent(state) : googleDefaultRedirect;
1080
- res.redirect(redirectTo);
1139
+ if (user.projectId) {
1140
+ res.cookie(options.projectCookieName || "projectId", user.projectId, {
1141
+ ...baseProjectCookieOptionsFrom(cookieConfig),
1142
+ httpOnly: true
1143
+ });
1144
+ }
1145
+ const finalRedirect = redirectTo || googleDefaultRedirect;
1146
+ res.redirect(finalRedirect);
1081
1147
  } catch (err) {
1082
1148
  console.error("Google callback error", err);
1083
1149
  const redirectError = googleDefaultRedirect.includes("?") ? `${googleDefaultRedirect}&error=google_login_failed` : `${googleDefaultRedirect}?error=google_login_failed`;
@@ -1203,6 +1269,8 @@ function setAuthCookies(res, tokens, cookie) {
1203
1269
  if (cookie.domain) {
1204
1270
  base.domain = cookie.domain;
1205
1271
  }
1272
+ console.log(cookie, "cookie");
1273
+ console.log(base, "base");
1206
1274
  if (tokens?.access_token) {
1207
1275
  res.cookie("access_token", tokens.access_token, base);
1208
1276
  }