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.
@@ -562,24 +562,61 @@ var EmailService = class {
562
562
  host: process.env.EMAIL_HOST || "smtp.postmarkapp.com",
563
563
  port: process.env.EMAIL_PORT ? Number(process.env.EMAIL_PORT) : 587,
564
564
  secure: (process.env.EMAIL_SECURE || "false") === "true",
565
- auth: { user: process.env.EMAIL_USER, pass: process.env.EMAIL_PASSWORD }
565
+ auth: {
566
+ user: process.env.EMAIL_USER,
567
+ pass: process.env.EMAIL_PASSWORD
568
+ }
566
569
  });
567
570
  }
568
571
  sign(payload, ttlSec = 60 * 60 * 24) {
569
- return jwt3.sign(payload, process.env.EMAIL_JWT_SECRET, { expiresIn: ttlSec });
572
+ return jwt3.sign(payload, process.env.EMAIL_JWT_SECRET, {
573
+ expiresIn: ttlSec
574
+ });
570
575
  }
571
576
  verify(token) {
572
577
  return jwt3.verify(token, process.env.EMAIL_JWT_SECRET);
573
578
  }
574
579
  async send(to, subject, html) {
575
- await this.transporter.sendMail({
576
- from: process.env.EMAIL_FROM,
577
- to,
578
- subject,
579
- html
580
- });
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
+ }
581
606
  }
582
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
+ );
583
620
  const now = Date.now();
584
621
  const windowStart = now - this.WINDOW_MINUTES * 60 * 1e3;
585
622
  const emailsInWindow = (lastEmailSent || []).map((d) => new Date(d)).filter((d) => d.getTime() >= windowStart);
@@ -624,10 +661,8 @@ function createAuthRouter(options = {}) {
624
661
  );
625
662
  r.post("/login", validateLogin, async (req, res) => {
626
663
  const { email: emailAddress, password } = req.body || {};
627
- console.log(emailAddress, password, "body");
628
664
  try {
629
665
  const user = await OrgUser.findOne({ email: emailAddress }).select("+password").lean();
630
- console.log(user, "user");
631
666
  if (!user) {
632
667
  return res.status(400).json({
633
668
  error: "Invalid email or password",
@@ -971,7 +1006,11 @@ function createAuthRouter(options = {}) {
971
1006
  if (!isGoogleEnabled) {
972
1007
  return res.status(500).json({ error: "Google login not configured" });
973
1008
  }
974
- const state = req.query.redirectTo ? encodeURIComponent(String(req.query.redirectTo)) : "";
1009
+ const stateData = {
1010
+ redirectTo: req.query.redirectTo || "",
1011
+ projectId: req.query.projectId || process.env.DEFAULT_PROJECT_ID || ""
1012
+ };
1013
+ const state = encodeURIComponent(JSON.stringify(stateData));
975
1014
  const params = new URLSearchParams({
976
1015
  client_id: googleClientId,
977
1016
  redirect_uri: googleRedirectUri,
@@ -982,6 +1021,7 @@ function createAuthRouter(options = {}) {
982
1021
  state
983
1022
  });
984
1023
  const url = `https://accounts.google.com/o/oauth2/v2/auth?${params.toString()}`;
1024
+ console.log(url, "url");
985
1025
  res.redirect(url);
986
1026
  });
987
1027
  r.get("/google/callback", async (req, res) => {
@@ -989,7 +1029,21 @@ function createAuthRouter(options = {}) {
989
1029
  return res.status(500).json({ error: "Google login not configured" });
990
1030
  }
991
1031
  const code = String(req.query.code || "");
992
- const state = req.query.state ? String(req.query.state) : "";
1032
+ let stateData = { redirectTo: "", projectId: "" };
1033
+ try {
1034
+ if (req.query.state) {
1035
+ stateData = JSON.parse(decodeURIComponent(String(req.query.state)));
1036
+ }
1037
+ } catch (err) {
1038
+ console.error("Failed to parse state:", err);
1039
+ }
1040
+ const { redirectTo, projectId } = stateData;
1041
+ console.log(
1042
+ "Parsed state - redirectTo:",
1043
+ redirectTo,
1044
+ "projectId:",
1045
+ projectId
1046
+ );
993
1047
  if (!code) {
994
1048
  return res.status(400).json({ ok: false, error: "Missing authorization code" });
995
1049
  }
@@ -1024,13 +1078,19 @@ function createAuthRouter(options = {}) {
1024
1078
  const lastName = decoded.family_name || "";
1025
1079
  let user = await OrgUser.findOne({ email: email2 }).lean();
1026
1080
  if (!user) {
1081
+ const finalProjectId = projectId || process.env.DEFAULT_PROJECT_ID;
1082
+ if (!finalProjectId) {
1083
+ console.error("No projectId available for new user");
1084
+ const errorRedirect = (redirectTo || googleDefaultRedirect) + (redirectTo?.includes("?") ? "&" : "?") + "error=missing_project_id";
1085
+ return res.redirect(errorRedirect);
1086
+ }
1027
1087
  const created = await OrgUser.create({
1028
1088
  email: email2,
1029
1089
  firstName,
1030
1090
  lastName,
1031
1091
  emailVerified,
1032
1092
  roles: ["platform_user"],
1033
- projectId: null,
1093
+ projectId: finalProjectId,
1034
1094
  metadata: []
1035
1095
  // you can also store googleId: decoded.sub
1036
1096
  });
@@ -1038,8 +1098,14 @@ function createAuthRouter(options = {}) {
1038
1098
  }
1039
1099
  const tokens = generateTokens(user);
1040
1100
  setAuthCookies(res, tokens, cookieConfig);
1041
- const redirectTo = state ? decodeURIComponent(state) : googleDefaultRedirect;
1042
- res.redirect(redirectTo);
1101
+ if (user.projectId) {
1102
+ res.cookie(options.projectCookieName || "projectId", user.projectId, {
1103
+ ...baseProjectCookieOptionsFrom(cookieConfig),
1104
+ httpOnly: true
1105
+ });
1106
+ }
1107
+ const finalRedirect = redirectTo || googleDefaultRedirect;
1108
+ res.redirect(finalRedirect);
1043
1109
  } catch (err) {
1044
1110
  console.error("Google callback error", err);
1045
1111
  const redirectError = googleDefaultRedirect.includes("?") ? `${googleDefaultRedirect}&error=google_login_failed` : `${googleDefaultRedirect}?error=google_login_failed`;
@@ -1165,6 +1231,8 @@ function setAuthCookies(res, tokens, cookie) {
1165
1231
  if (cookie.domain) {
1166
1232
  base.domain = cookie.domain;
1167
1233
  }
1234
+ console.log(cookie, "cookie");
1235
+ console.log(base, "base");
1168
1236
  if (tokens?.access_token) {
1169
1237
  res.cookie("access_token", tokens.access_token, base);
1170
1238
  }