实现账户邮箱验证与换绑流程
为应用建立邮箱验证状态与令牌机制,实现重发、换绑和未验证权限控制,并补齐测试。
完整提示词
<context>
You are a security-minded engineer implementing email verification. It looks like "send a link, flip a flag", and the gaps lead to account takeover and abuse:
- Tokens: at least 128 bits from a secure random source, stored hashed, single use, with an expiry (commonly 24 hours). Bind each token to the user id and the exact address it was sent to, so a token issued for an old address cannot verify a new one.
- Email security scanners open links before people do. A GET that verifies and logs the user in can be triggered by a scanner, or by anyone who intercepts the link. Either make verification idempotent and keep login separate, or show a confirm page that POSTs.
- Resend: rate limit per account and per address, invalidate older tokens or allow only the latest few, and return the same response whether or not the address exists.
- Changing email: keep the old address active until the new one is verified, require recent authentication to start the change, notify the old address, and re-check uniqueness at confirmation time because another account may have claimed the address meanwhile.
- Unverified accounts: decide what they may do. Gate the valuable or abusable actions (sending invites or messages, payments, free credits) rather than blocking everything, and clean up accounts never verified after a set time.
- Uniqueness: compare addresses case-insensitively in a unique index; do not strip dots or plus tags unless the product chooses to.
- Social sign-in: trust an `email_verified` claim only from providers that guarantee it; otherwise verify the address yourself before linking accounts by email.
</context>
<task>
Implement email verification for this app:
{{requirements}}
1. Read the user model, sign-up and sign-in flows, social login, the email sending code, and existing token handling. Follow the existing patterns and libraries.
2. Write the lifecycle: states (unverified, verified, change pending), transitions, what each state may do, token expiry and resend limits.
3. Add the data model (hashed tokens bound to user and address, verified timestamp, pending email) with a migration.
4. Implement send, resend, verify (scanner-safe), change email with old-address notice, enforcement of what unverified users may do, and cleanup of stale unverified accounts.
5. Write tests: valid token, expired token, reused token, token for an old address after change, resend limit, enumeration-safe responses, gated action blocked while unverified.
6. Run the tests and linters. Fix what fails.
</task>
<constraints>
- Never store tokens in plain text or put them in logs.
- Never auto-link a social login to an existing account by email unless the provider verified that email.
- Do not send real emails from tests.
{{> guardrails/scope-discipline}}
{{> guardrails/investigate-before-answering}}
{{> guardrails/verify-before-done}}
</constraints>
<output_format>
## Lifecycle
States, transitions and what each state may do.
## Data model
Tables or fields added, with indexes.
## Changes
One line per file: `path`, what changed.
## Tests
One line per test and the case it covers.
## Verification
Commands run and their actual results.
## Open questions
Product decisions still needed, such as expiry length or cleanup period.
</output_format>可替换内容
- {{requirements}}
- {{> guardrails/scope-discipline}}
- {{> guardrails/investigate-before-answering}}
- {{> guardrails/verify-before-done}}
分类与标签
编程开发、邮箱验证、账户安全、令牌管理、邮箱换绑