指南

Email 驗證

Email 驗證碼 HTML

App 需啟用 email_verify 服務。以下 HTML 示範請求 6 位驗證碼與確認流程(用途 registerlogin_verify)。

API

  • POST {{api_base}}/v1/email_verify/request — body: { client_id, email, purpose }
  • POST {{api_base}}/v1/email_verify/confirm — body: { client_id, email, purpose, code }

email-verify.html

<!DOCTYPE html>
<html lang="zh-Hant">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>{{app_name}} · Email 驗證</title>
  <style>
    body { font-family: system-ui, sans-serif; max-width: 24rem; margin: 2rem auto; }
    label { display: block; margin-top: 1rem; }
    input, select, button { width: 100%; padding: 0.5rem; margin-top: 0.25rem; box-sizing: border-box; }
    pre { background: #f4f4f5; padding: 0.75rem; font-size: 0.85rem; overflow-x: auto; }
  </style>
</head>
<body>
  <h1>Email 驗證碼</h1>
  <label>Email<input type="email" id="email" placeholder="user@example.com" /></label>
  <label>用途
    <select id="purpose">
      <option value="register">register</option>
      <option value="login_verify">login_verify</option>
    </select>
  </label>
  <button type="button" id="btn-request" style="margin-top: 1rem">寄送驗證碼</button>
  <label>6 位驗證碼<input type="text" id="code" maxlength="6" pattern="[0-9]{6}" inputmode="numeric" /></label>
  <button type="button" id="btn-confirm" style="margin-top: 0.5rem">確認驗證碼</button>
  <pre id="out"></pre>
  <script>
    const API_BASE = "{{api_base}}";
    const CLIENT_ID = "{{client_id}}";
    const out = document.getElementById("out");

    async function post(path, body) {
      const res = await fetch(API_BASE + path, {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify(body),
      });
      const data = await res.json().catch(() => ({}));
      return { ok: res.ok, status: res.status, data };
    }

    document.getElementById("btn-request").addEventListener("click", async () => {
      const email = document.getElementById("email").value.trim();
      const purpose = document.getElementById("purpose").value;
      const r = await post("/v1/email_verify/request", { client_id: CLIENT_ID, email, purpose });
      out.textContent = JSON.stringify(r, null, 2);
    });

    document.getElementById("btn-confirm").addEventListener("click", async () => {
      const email = document.getElementById("email").value.trim();
      const purpose = document.getElementById("purpose").value;
      const code = document.getElementById("code").value.trim();
      const r = await post("/v1/email_verify/confirm", { client_id: CLIENT_ID, email, purpose, code });
      out.textContent = JSON.stringify(r, null, 2);
    });
  </script>
</body>
</html>