aaspai-authx 0.1.2 → 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.
@@ -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);
@@ -971,7 +1008,11 @@ function createAuthRouter(options = {}) {
971
1008
  if (!isGoogleEnabled) {
972
1009
  return res.status(500).json({ error: "Google login not configured" });
973
1010
  }
974
- const state = req.query.redirectTo ? encodeURIComponent(String(req.query.redirectTo)) : "";
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));
975
1016
  const params = new URLSearchParams({
976
1017
  client_id: googleClientId,
977
1018
  redirect_uri: googleRedirectUri,
@@ -982,6 +1023,7 @@ function createAuthRouter(options = {}) {
982
1023
  state
983
1024
  });
984
1025
  const url = `https://accounts.google.com/o/oauth2/v2/auth?${params.toString()}`;
1026
+ console.log(url, "url");
985
1027
  res.redirect(url);
986
1028
  });
987
1029
  r.get("/google/callback", async (req, res) => {
@@ -989,7 +1031,21 @@ function createAuthRouter(options = {}) {
989
1031
  return res.status(500).json({ error: "Google login not configured" });
990
1032
  }
991
1033
  const code = String(req.query.code || "");
992
- const state = req.query.state ? String(req.query.state) : "";
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
+ );
993
1049
  if (!code) {
994
1050
  return res.status(400).json({ ok: false, error: "Missing authorization code" });
995
1051
  }
@@ -1024,13 +1080,19 @@ function createAuthRouter(options = {}) {
1024
1080
  const lastName = decoded.family_name || "";
1025
1081
  let user = await OrgUser.findOne({ email: email2 }).lean();
1026
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
+ }
1027
1089
  const created = await OrgUser.create({
1028
1090
  email: email2,
1029
1091
  firstName,
1030
1092
  lastName,
1031
1093
  emailVerified,
1032
1094
  roles: ["platform_user"],
1033
- projectId: null,
1095
+ projectId: finalProjectId,
1034
1096
  metadata: []
1035
1097
  // you can also store googleId: decoded.sub
1036
1098
  });
@@ -1038,8 +1100,14 @@ function createAuthRouter(options = {}) {
1038
1100
  }
1039
1101
  const tokens = generateTokens(user);
1040
1102
  setAuthCookies(res, tokens, cookieConfig);
1041
- const redirectTo = state ? decodeURIComponent(state) : googleDefaultRedirect;
1042
- res.redirect(redirectTo);
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);
1043
1111
  } catch (err) {
1044
1112
  console.error("Google callback error", err);
1045
1113
  const redirectError = googleDefaultRedirect.includes("?") ? `${googleDefaultRedirect}&error=google_login_failed` : `${googleDefaultRedirect}?error=google_login_failed`;
@@ -1211,6 +1279,7 @@ async function sendRateLimitedEmail({
1211
1279
  if (!can.ok) {
1212
1280
  return { rateLimited: true, waitMs: can.waitMs };
1213
1281
  }
1282
+ console.log(can, "can");
1214
1283
  await emailService.send(user.email, subject, html);
1215
1284
  user.lastEmailSent = [...user.lastEmailSent || [], /* @__PURE__ */ new Date()];
1216
1285
  await user.save();