Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

2.6 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.

import { VerificationEmail } from "@carbon/documents/email";
import { redis } from "@carbon/kv";
import { sendEmail } from "@carbon/lib/email.server";
import { getLogger } from "@carbon/logger";
import { render } from "react-email";

const log = getLogger("auth");

export async function sendVerificationCode(email: string) {
  try {
    // Generate 6-digit verification code
    const verificationCode = Math.floor(
      100000 + Math.random() * 900000
    ).toString();

    // Store in Redis with 10-minute expiration. @carbon/kv (withResilience) fails
    // soft: on a Redis-down condition `set` resolves null instead of "OK". If we
    // can't persist the code we must NOT send an unverifiable email — fail closed.
    const stored = await redis.set(
      `verification:${email.toLowerCase()}`,
      verificationCode,
      "EX",
      600
    );
    if (!stored) {
      log.error(
        "Failed to store verification code (Redis unavailable); not sending email"
      );
      return false;
    }

    // Send email with verification code using React template
    const html = await render(
      VerificationEmail({
        email,
        verificationCode
      })
    );

    const result = await sendEmail({
      to: email,
      subject: "Verify your email address",
      html
    });
    log.debug("Verification email sent", { result });

    // `data: null` with no error is the SMTP-unconfigured no-op — the user
    // will never receive a code, so don't report success.
    return !result.error && result.data !== null;
  } catch (error) {
    log.error("Failed to send verification code", { error });
    return false;
  }
}

export async function verifyEmailCode(email: string, code: string) {
  try {
    const storedCode = await redis.get(`verification:${email.toLowerCase()}`);

    // If Redis is down, @carbon/kv fails soft and `storedCode` is null, so we
    // return false. Blocking verification when the code store is unreachable is
    // the expected/acceptable fail-closed behavior — we cannot confirm the code.
    if (!storedCode || String(storedCode).trim() !== String(code).trim()) {
      return false;
    }

    // Delete the code after successful verification
    await redis.del(`verification:${email.toLowerCase()}`);

    return true;
  } catch (error) {
    log.error("Failed to verify email code", { error });
    return false;
  }
}