@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.js CHANGED
@@ -3258,6 +3258,16 @@ function generateEmailOtp() {
3258
3258
  const num = (bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3]) >>> 0;
3259
3259
  return String(num % 1e6).padStart(6, "0");
3260
3260
  }
3261
+ var EMAIL_CODE_TTL_SECONDS = 300;
3262
+ async function sendEmailChallengeCode(env, database, usersTable, user, emailBranding) {
3263
+ const code = generateEmailOtp();
3264
+ await env.OAUTH_STATES.put(`email_2fa_challenge:${user.id}`, await hashToken(code), {
3265
+ expirationTtl: EMAIL_CODE_TTL_SECONDS
3266
+ });
3267
+ const emailService = new EmailService(env, void 0, emailBranding);
3268
+ const firstName = user.name?.split(" ")[0] || null;
3269
+ await emailService.send2faCodeEmail({ email: user.email, firstName, code }, database, usersTable);
3270
+ }
3261
3271
  async function verifyAny2faCode(database, env, userId, code, method, tables2) {
3262
3272
  const { user2faMethods: user2faMethodsTable, userBackupCodes: userBackupCodesTable } = tables2;
3263
3273
  if (method === "totp") {
@@ -3342,7 +3352,7 @@ var loginRoute = createRoute2({
3342
3352
  });
3343
3353
  var loginHandler = async (c) => {
3344
3354
  const { email, password, turnstileToken } = c.req.valid("json");
3345
- const { db: database, schema } = getAuthContext(c);
3355
+ const { db: database, schema, emailBranding } = getAuthContext(c);
3346
3356
  const env = c.env;
3347
3357
  const turnstileValid = await verifyTurnstileToken(
3348
3358
  turnstileToken,
@@ -3427,6 +3437,10 @@ var loginHandler = async (c) => {
3427
3437
  };
3428
3438
  await storeChallengeToken(env.OAUTH_STATES, challengeToken, challengePayload);
3429
3439
  setChallengeCookie(c, env, challengeToken);
3440
+ const emailMethod = enabled2faMethods.find((m) => m.method === "email");
3441
+ if (emailMethod && (emailMethod.isPrimary || enabled2faMethods.length === 1)) {
3442
+ await sendEmailChallengeCode(env, database, schema.users, user, emailBranding);
3443
+ }
3430
3444
  return c.json({
3431
3445
  requires2fa: true,
3432
3446
  methods: enabled2faMethods.map((m) => m.method)
@@ -5283,14 +5297,7 @@ var challengeResendHandler = async (c) => {
5283
5297
  logger_default.error("User not found during 2FA resend", { userId: payload.userId });
5284
5298
  return problems.notFound(c, "User not found");
5285
5299
  }
5286
- const code = generateEmailOtp();
5287
- const codeHash = await hashToken(code);
5288
- await c.env.OAUTH_STATES.put(`email_2fa_challenge:${payload.userId}`, codeHash, {
5289
- expirationTtl: 300
5290
- });
5291
- const emailService = new EmailService(c.env, void 0, emailBranding);
5292
- const firstName = user.name?.split(" ")[0] || null;
5293
- await emailService.send2faCodeEmail({ email: user.email, firstName, code }, db, schema.users);
5300
+ await sendEmailChallengeCode(c.env, db, schema.users, { id: payload.userId, ...user }, emailBranding);
5294
5301
  logger_default.info("Email 2FA code resent", { userId: payload.userId });
5295
5302
  return c.json({ success: true });
5296
5303
  };