@syncello/auth 3.6.0 → 3.6.1

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
@@ -3650,6 +3650,16 @@ function generateEmailOtp() {
3650
3650
  const num = (bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3]) >>> 0;
3651
3651
  return String(num % 1e6).padStart(6, "0");
3652
3652
  }
3653
+ var EMAIL_CODE_TTL_SECONDS = 300;
3654
+ async function sendEmailChallengeCode(env, database, usersTable, user, emailBranding) {
3655
+ const code = generateEmailOtp();
3656
+ await env.OAUTH_STATES.put(`email_2fa_challenge:${user.id}`, await hashToken(code), {
3657
+ expirationTtl: EMAIL_CODE_TTL_SECONDS
3658
+ });
3659
+ const emailService = new EmailService(env, void 0, emailBranding);
3660
+ const firstName = user.name?.split(" ")[0] || null;
3661
+ await emailService.send2faCodeEmail({ email: user.email, firstName, code }, database, usersTable);
3662
+ }
3653
3663
  async function verifyAny2faCode(database, env, userId, code, method, tables2) {
3654
3664
  const { user2faMethods: user2faMethodsTable, userBackupCodes: userBackupCodesTable } = tables2;
3655
3665
  if (method === "totp") {
@@ -3734,7 +3744,7 @@ var loginRoute = (0, import_zod_openapi3.createRoute)({
3734
3744
  });
3735
3745
  var loginHandler = async (c) => {
3736
3746
  const { email, password, turnstileToken } = c.req.valid("json");
3737
- const { db: database, schema } = getAuthContext(c);
3747
+ const { db: database, schema, emailBranding } = getAuthContext(c);
3738
3748
  const env = c.env;
3739
3749
  const turnstileValid = await verifyTurnstileToken(
3740
3750
  turnstileToken,
@@ -3819,6 +3829,10 @@ var loginHandler = async (c) => {
3819
3829
  };
3820
3830
  await storeChallengeToken(env.OAUTH_STATES, challengeToken, challengePayload);
3821
3831
  setChallengeCookie(c, env, challengeToken);
3832
+ const emailMethod = enabled2faMethods.find((m) => m.method === "email");
3833
+ if (emailMethod && (emailMethod.isPrimary || enabled2faMethods.length === 1)) {
3834
+ await sendEmailChallengeCode(env, database, schema.users, user, emailBranding);
3835
+ }
3822
3836
  return c.json({
3823
3837
  requires2fa: true,
3824
3838
  methods: enabled2faMethods.map((m) => m.method)
@@ -5675,14 +5689,7 @@ var challengeResendHandler = async (c) => {
5675
5689
  logger_default.error("User not found during 2FA resend", { userId: payload.userId });
5676
5690
  return problems.notFound(c, "User not found");
5677
5691
  }
5678
- const code = generateEmailOtp();
5679
- const codeHash = await hashToken(code);
5680
- await c.env.OAUTH_STATES.put(`email_2fa_challenge:${payload.userId}`, codeHash, {
5681
- expirationTtl: 300
5682
- });
5683
- const emailService = new EmailService(c.env, void 0, emailBranding);
5684
- const firstName = user.name?.split(" ")[0] || null;
5685
- await emailService.send2faCodeEmail({ email: user.email, firstName, code }, db, schema.users);
5692
+ await sendEmailChallengeCode(c.env, db, schema.users, { id: payload.userId, ...user }, emailBranding);
5686
5693
  logger_default.info("Email 2FA code resent", { userId: payload.userId });
5687
5694
  return c.json({ success: true });
5688
5695
  };