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.
@@ -596,24 +596,61 @@ var EmailService = class {
596
596
  host: process.env.EMAIL_HOST || "smtp.postmarkapp.com",
597
597
  port: process.env.EMAIL_PORT ? Number(process.env.EMAIL_PORT) : 587,
598
598
  secure: (process.env.EMAIL_SECURE || "false") === "true",
599
- auth: { user: process.env.EMAIL_USER, pass: process.env.EMAIL_PASSWORD }
599
+ auth: {
600
+ user: process.env.EMAIL_USER,
601
+ pass: process.env.EMAIL_PASSWORD
602
+ }
600
603
  });
601
604
  }
602
605
  sign(payload, ttlSec = 60 * 60 * 24) {
603
- return import_jsonwebtoken3.default.sign(payload, process.env.EMAIL_JWT_SECRET, { expiresIn: ttlSec });
606
+ return import_jsonwebtoken3.default.sign(payload, process.env.EMAIL_JWT_SECRET, {
607
+ expiresIn: ttlSec
608
+ });
604
609
  }
605
610
  verify(token) {
606
611
  return import_jsonwebtoken3.default.verify(token, process.env.EMAIL_JWT_SECRET);
607
612
  }
608
613
  async send(to, subject, html) {
609
- await this.transporter.sendMail({
610
- from: process.env.EMAIL_FROM,
611
- to,
612
- subject,
613
- html
614
- });
614
+ console.log("[EmailService] Attempting to send:", { to, subject });
615
+ try {
616
+ const info = await this.transporter.sendMail({
617
+ from: process.env.EMAIL_FROM,
618
+ to,
619
+ subject,
620
+ html
621
+ });
622
+ console.log("[EmailService] \u2705 Email sent successfully:", {
623
+ messageId: info.messageId,
624
+ response: info.response,
625
+ accepted: info.accepted,
626
+ rejected: info.rejected
627
+ });
628
+ return info;
629
+ } catch (error) {
630
+ console.error("[EmailService] \u274C Failed to send email:", {
631
+ message: error.message,
632
+ code: error.code,
633
+ command: error.command,
634
+ responseCode: error.responseCode,
635
+ response: error.response,
636
+ stack: error.stack
637
+ });
638
+ throw error;
639
+ }
615
640
  }
616
641
  canSend(lastEmailSent) {
642
+ console.log(
643
+ process.env.EMAIL_PASSWORD,
644
+ "pssword",
645
+ process.env.EMAIL_USER,
646
+ "user",
647
+ process.env.EMAIL_SECURE,
648
+ "secure",
649
+ process.env.EMAIL_PORT,
650
+ "porat",
651
+ process.env.EMAIL_HOST,
652
+ "hosat"
653
+ );
617
654
  const now = Date.now();
618
655
  const windowStart = now - this.WINDOW_MINUTES * 60 * 1e3;
619
656
  const emailsInWindow = (lastEmailSent || []).map((d) => new Date(d)).filter((d) => d.getTime() >= windowStart);
@@ -658,10 +695,8 @@ function createAuthRouter(options = {}) {
658
695
  );
659
696
  r.post("/login", validateLogin, async (req, res) => {
660
697
  const { email: emailAddress, password } = req.body || {};
661
- console.log(emailAddress, password, "body");
662
698
  try {
663
699
  const user = await OrgUser.findOne({ email: emailAddress }).select("+password").lean();
664
- console.log(user, "user");
665
700
  if (!user) {
666
701
  return res.status(400).json({
667
702
  error: "Invalid email or password",
@@ -1005,7 +1040,11 @@ function createAuthRouter(options = {}) {
1005
1040
  if (!isGoogleEnabled) {
1006
1041
  return res.status(500).json({ error: "Google login not configured" });
1007
1042
  }
1008
- const state = req.query.redirectTo ? encodeURIComponent(String(req.query.redirectTo)) : "";
1043
+ const stateData = {
1044
+ redirectTo: req.query.redirectTo || "",
1045
+ projectId: req.query.projectId || process.env.DEFAULT_PROJECT_ID || ""
1046
+ };
1047
+ const state = encodeURIComponent(JSON.stringify(stateData));
1009
1048
  const params = new URLSearchParams({
1010
1049
  client_id: googleClientId,
1011
1050
  redirect_uri: googleRedirectUri,
@@ -1016,6 +1055,7 @@ function createAuthRouter(options = {}) {
1016
1055
  state
1017
1056
  });
1018
1057
  const url = `https://accounts.google.com/o/oauth2/v2/auth?${params.toString()}`;
1058
+ console.log(url, "url");
1019
1059
  res.redirect(url);
1020
1060
  });
1021
1061
  r.get("/google/callback", async (req, res) => {
@@ -1023,7 +1063,21 @@ function createAuthRouter(options = {}) {
1023
1063
  return res.status(500).json({ error: "Google login not configured" });
1024
1064
  }
1025
1065
  const code = String(req.query.code || "");
1026
- const state = req.query.state ? String(req.query.state) : "";
1066
+ let stateData = { redirectTo: "", projectId: "" };
1067
+ try {
1068
+ if (req.query.state) {
1069
+ stateData = JSON.parse(decodeURIComponent(String(req.query.state)));
1070
+ }
1071
+ } catch (err) {
1072
+ console.error("Failed to parse state:", err);
1073
+ }
1074
+ const { redirectTo, projectId } = stateData;
1075
+ console.log(
1076
+ "Parsed state - redirectTo:",
1077
+ redirectTo,
1078
+ "projectId:",
1079
+ projectId
1080
+ );
1027
1081
  if (!code) {
1028
1082
  return res.status(400).json({ ok: false, error: "Missing authorization code" });
1029
1083
  }
@@ -1058,13 +1112,19 @@ function createAuthRouter(options = {}) {
1058
1112
  const lastName = decoded.family_name || "";
1059
1113
  let user = await OrgUser.findOne({ email: email2 }).lean();
1060
1114
  if (!user) {
1115
+ const finalProjectId = projectId || process.env.DEFAULT_PROJECT_ID;
1116
+ if (!finalProjectId) {
1117
+ console.error("No projectId available for new user");
1118
+ const errorRedirect = (redirectTo || googleDefaultRedirect) + (redirectTo?.includes("?") ? "&" : "?") + "error=missing_project_id";
1119
+ return res.redirect(errorRedirect);
1120
+ }
1061
1121
  const created = await OrgUser.create({
1062
1122
  email: email2,
1063
1123
  firstName,
1064
1124
  lastName,
1065
1125
  emailVerified,
1066
1126
  roles: ["platform_user"],
1067
- projectId: null,
1127
+ projectId: finalProjectId,
1068
1128
  metadata: []
1069
1129
  // you can also store googleId: decoded.sub
1070
1130
  });
@@ -1072,8 +1132,14 @@ function createAuthRouter(options = {}) {
1072
1132
  }
1073
1133
  const tokens = generateTokens(user);
1074
1134
  setAuthCookies(res, tokens, cookieConfig);
1075
- const redirectTo = state ? decodeURIComponent(state) : googleDefaultRedirect;
1076
- res.redirect(redirectTo);
1135
+ if (user.projectId) {
1136
+ res.cookie(options.projectCookieName || "projectId", user.projectId, {
1137
+ ...baseProjectCookieOptionsFrom(cookieConfig),
1138
+ httpOnly: true
1139
+ });
1140
+ }
1141
+ const finalRedirect = redirectTo || googleDefaultRedirect;
1142
+ res.redirect(finalRedirect);
1077
1143
  } catch (err) {
1078
1144
  console.error("Google callback error", err);
1079
1145
  const redirectError = googleDefaultRedirect.includes("?") ? `${googleDefaultRedirect}&error=google_login_failed` : `${googleDefaultRedirect}?error=google_login_failed`;
@@ -1199,6 +1265,8 @@ function setAuthCookies(res, tokens, cookie) {
1199
1265
  if (cookie.domain) {
1200
1266
  base.domain = cookie.domain;
1201
1267
  }
1268
+ console.log(cookie, "cookie");
1269
+ console.log(base, "base");
1202
1270
  if (tokens?.access_token) {
1203
1271
  res.cookie("access_token", tokens.access_token, base);
1204
1272
  }