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.
package/dist/index.cjs CHANGED
@@ -680,24 +680,61 @@ var EmailService = class {
680
680
  host: process.env.EMAIL_HOST || "smtp.postmarkapp.com",
681
681
  port: process.env.EMAIL_PORT ? Number(process.env.EMAIL_PORT) : 587,
682
682
  secure: (process.env.EMAIL_SECURE || "false") === "true",
683
- auth: { user: process.env.EMAIL_USER, pass: process.env.EMAIL_PASSWORD }
683
+ auth: {
684
+ user: process.env.EMAIL_USER,
685
+ pass: process.env.EMAIL_PASSWORD
686
+ }
684
687
  });
685
688
  }
686
689
  sign(payload, ttlSec = 60 * 60 * 24) {
687
- return import_jsonwebtoken3.default.sign(payload, process.env.EMAIL_JWT_SECRET, { expiresIn: ttlSec });
690
+ return import_jsonwebtoken3.default.sign(payload, process.env.EMAIL_JWT_SECRET, {
691
+ expiresIn: ttlSec
692
+ });
688
693
  }
689
694
  verify(token) {
690
695
  return import_jsonwebtoken3.default.verify(token, process.env.EMAIL_JWT_SECRET);
691
696
  }
692
697
  async send(to, subject, html) {
693
- await this.transporter.sendMail({
694
- from: process.env.EMAIL_FROM,
695
- to,
696
- subject,
697
- html
698
- });
698
+ console.log("[EmailService] Attempting to send:", { to, subject });
699
+ try {
700
+ const info = await this.transporter.sendMail({
701
+ from: process.env.EMAIL_FROM,
702
+ to,
703
+ subject,
704
+ html
705
+ });
706
+ console.log("[EmailService] \u2705 Email sent successfully:", {
707
+ messageId: info.messageId,
708
+ response: info.response,
709
+ accepted: info.accepted,
710
+ rejected: info.rejected
711
+ });
712
+ return info;
713
+ } catch (error) {
714
+ console.error("[EmailService] \u274C Failed to send email:", {
715
+ message: error.message,
716
+ code: error.code,
717
+ command: error.command,
718
+ responseCode: error.responseCode,
719
+ response: error.response,
720
+ stack: error.stack
721
+ });
722
+ throw error;
723
+ }
699
724
  }
700
725
  canSend(lastEmailSent) {
726
+ console.log(
727
+ process.env.EMAIL_PASSWORD,
728
+ "pssword",
729
+ process.env.EMAIL_USER,
730
+ "user",
731
+ process.env.EMAIL_SECURE,
732
+ "secure",
733
+ process.env.EMAIL_PORT,
734
+ "porat",
735
+ process.env.EMAIL_HOST,
736
+ "hosat"
737
+ );
701
738
  const now = Date.now();
702
739
  const windowStart = now - this.WINDOW_MINUTES * 60 * 1e3;
703
740
  const emailsInWindow = (lastEmailSent || []).map((d) => new Date(d)).filter((d) => d.getTime() >= windowStart);
@@ -742,10 +779,8 @@ function createAuthRouter(options = {}) {
742
779
  );
743
780
  r.post("/login", validateLogin, async (req, res) => {
744
781
  const { email: emailAddress, password } = req.body || {};
745
- console.log(emailAddress, password, "body");
746
782
  try {
747
783
  const user = await OrgUser.findOne({ email: emailAddress }).select("+password").lean();
748
- console.log(user, "user");
749
784
  if (!user) {
750
785
  return res.status(400).json({
751
786
  error: "Invalid email or password",
@@ -1089,7 +1124,11 @@ function createAuthRouter(options = {}) {
1089
1124
  if (!isGoogleEnabled) {
1090
1125
  return res.status(500).json({ error: "Google login not configured" });
1091
1126
  }
1092
- const state = req.query.redirectTo ? encodeURIComponent(String(req.query.redirectTo)) : "";
1127
+ const stateData = {
1128
+ redirectTo: req.query.redirectTo || "",
1129
+ projectId: req.query.projectId || process.env.DEFAULT_PROJECT_ID || ""
1130
+ };
1131
+ const state = encodeURIComponent(JSON.stringify(stateData));
1093
1132
  const params = new URLSearchParams({
1094
1133
  client_id: googleClientId,
1095
1134
  redirect_uri: googleRedirectUri,
@@ -1100,6 +1139,7 @@ function createAuthRouter(options = {}) {
1100
1139
  state
1101
1140
  });
1102
1141
  const url = `https://accounts.google.com/o/oauth2/v2/auth?${params.toString()}`;
1142
+ console.log(url, "url");
1103
1143
  res.redirect(url);
1104
1144
  });
1105
1145
  r.get("/google/callback", async (req, res) => {
@@ -1107,7 +1147,21 @@ function createAuthRouter(options = {}) {
1107
1147
  return res.status(500).json({ error: "Google login not configured" });
1108
1148
  }
1109
1149
  const code = String(req.query.code || "");
1110
- const state = req.query.state ? String(req.query.state) : "";
1150
+ let stateData = { redirectTo: "", projectId: "" };
1151
+ try {
1152
+ if (req.query.state) {
1153
+ stateData = JSON.parse(decodeURIComponent(String(req.query.state)));
1154
+ }
1155
+ } catch (err) {
1156
+ console.error("Failed to parse state:", err);
1157
+ }
1158
+ const { redirectTo, projectId } = stateData;
1159
+ console.log(
1160
+ "Parsed state - redirectTo:",
1161
+ redirectTo,
1162
+ "projectId:",
1163
+ projectId
1164
+ );
1111
1165
  if (!code) {
1112
1166
  return res.status(400).json({ ok: false, error: "Missing authorization code" });
1113
1167
  }
@@ -1142,13 +1196,19 @@ function createAuthRouter(options = {}) {
1142
1196
  const lastName = decoded.family_name || "";
1143
1197
  let user = await OrgUser.findOne({ email: email2 }).lean();
1144
1198
  if (!user) {
1199
+ const finalProjectId = projectId || process.env.DEFAULT_PROJECT_ID;
1200
+ if (!finalProjectId) {
1201
+ console.error("No projectId available for new user");
1202
+ const errorRedirect = (redirectTo || googleDefaultRedirect) + (redirectTo?.includes("?") ? "&" : "?") + "error=missing_project_id";
1203
+ return res.redirect(errorRedirect);
1204
+ }
1145
1205
  const created = await OrgUser.create({
1146
1206
  email: email2,
1147
1207
  firstName,
1148
1208
  lastName,
1149
1209
  emailVerified,
1150
1210
  roles: ["platform_user"],
1151
- projectId: null,
1211
+ projectId: finalProjectId,
1152
1212
  metadata: []
1153
1213
  // you can also store googleId: decoded.sub
1154
1214
  });
@@ -1156,8 +1216,14 @@ function createAuthRouter(options = {}) {
1156
1216
  }
1157
1217
  const tokens = generateTokens(user);
1158
1218
  setAuthCookies(res, tokens, cookieConfig);
1159
- const redirectTo = state ? decodeURIComponent(state) : googleDefaultRedirect;
1160
- res.redirect(redirectTo);
1219
+ if (user.projectId) {
1220
+ res.cookie(options.projectCookieName || "projectId", user.projectId, {
1221
+ ...baseProjectCookieOptionsFrom(cookieConfig),
1222
+ httpOnly: true
1223
+ });
1224
+ }
1225
+ const finalRedirect = redirectTo || googleDefaultRedirect;
1226
+ res.redirect(finalRedirect);
1161
1227
  } catch (err) {
1162
1228
  console.error("Google callback error", err);
1163
1229
  const redirectError = googleDefaultRedirect.includes("?") ? `${googleDefaultRedirect}&error=google_login_failed` : `${googleDefaultRedirect}?error=google_login_failed`;
@@ -1283,6 +1349,8 @@ function setAuthCookies(res, tokens, cookie) {
1283
1349
  if (cookie.domain) {
1284
1350
  base.domain = cookie.domain;
1285
1351
  }
1352
+ console.log(cookie, "cookie");
1353
+ console.log(base, "base");
1286
1354
  if (tokens?.access_token) {
1287
1355
  res.cookie("access_token", tokens.access_token, base);
1288
1356
  }