0Pricing
Firebase Auth & Realtime Database Apps · 课时

密码重置与电子邮件验证

通过允许用户重置遗忘的密码并验证电子邮件地址,完善电子邮件/密码身份验证,同时提升安全性和账户恢复能力。

密码重置与电子邮件验证 是 CoddyKit 上的免费 Firebase Auth & Realtime Database Apps 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Firebase Auth & Realtime Database Apps 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Firebase Auth & Realtime Database Apps 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Why Reset and Verification Matter

Email/password sign-in is incomplete without two flows: password reset for forgotten credentials and email verification to prove the user owns the address.

  • Reset reduces support tickets and lockouts
  • Verification blocks fake or mistyped emails

How Password Reset Works

The flow is entirely email-driven so the user never reveals an old password:

  • User requests a reset for their email
  • Firebase sends a secure, time-limited link
  • User clicks it and chooses a new password

Your app only triggers the email; Firebase hosts the reset page by default.

Triggering a Reset Email

Call sendPasswordResetEmail with the user's address. Firebase handles delivery and the link.

import { getAuth, sendPasswordResetEmail } from 'firebase/auth';

const auth = getAuth();
await sendPasswordResetEmail(auth, 'user@example.com');
console.log('Reset email sent');

Handling Reset Errors Gracefully

For security, avoid revealing whether an email exists. Show the same confirmation message whether or not the account is found.

try {
  await sendPasswordResetEmail(auth, email);
} catch (e) {
  // log internally but do not expose to user
}
showMessage('If that email exists, a reset link was sent.');

Sending a Verification Email

After a user signs up, send a verification email with sendEmailVerification on the current user object.

import { getAuth, sendEmailVerification } from 'firebase/auth';

const user = getAuth().currentUser;
if (user) {
  await sendEmailVerification(user);
}

Checking Verification Status

The user object exposes emailVerified. Use it to gate sensitive features until the address is confirmed.

const user = getAuth().currentUser;
if (user && !user.emailVerified) {
  showBanner('Please verify your email to continue.');
}

Refreshing the Token After Verification

The emailVerified flag is cached in the ID token. After a user verifies, call reload to refresh their local state.

const user = getAuth().currentUser;
await user.reload();
console.log('Verified now?', user.emailVerified);

Customizing Email Templates

In the Firebase console under Authentication > Templates you can customize the sender name, subject, and body of reset and verification emails, and set a custom action URL for branded pages.

Enforcing Verification

You can require a verified email before granting access to certain data using Security Rules. Tokens carry an email_verified claim you can check.

{
  "rules": {
    "posts": {
      ".write": "auth != null && auth.token.email_verified == true"
    }
  }
}

Rate Limiting and Abuse

Firebase throttles repeated reset and verification requests to prevent abuse and spam. In your UI, disable the button briefly after sending so users do not trigger the limit accidentally.

Putting It Together

A complete signup typically looks like: create account, send verification email, show a 'check your inbox' screen, and reveal full features once emailVerified becomes true. A 'Forgot password?' link calls the reset flow.

Quick Check

Test your understanding of reset and verification.

Recap

Your email/password auth is now complete and recoverable.

  • Use sendPasswordResetEmail for forgotten passwords
  • Avoid revealing whether an email exists
  • Verify ownership with sendEmailVerification and emailVerified
  • Call reload to refresh status
  • Customize templates and enforce verification via rules

常见问题解答

「密码重置与电子邮件验证」课时是免费的吗?

是的 — 「密码重置与电子邮件验证」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Firebase Auth & Realtime Database Apps 课程的其余内容,请升级到 CoddyKit PRO。 Firebase Auth & Realtime Database Apps 课程共包含 4 节课。

「密码重置与电子邮件验证」这节课中我会学到什么?

通过允许用户重置遗忘的密码并验证电子邮件地址,完善电子邮件/密码身份验证,同时提升安全性和账户恢复能力。 你通过在浏览器中直接运行的动手代码来练习 Firebase Auth & Realtime Database Apps,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Firebase Auth & Realtime Database Apps 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Firebase Auth & Realtime Database Apps 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「密码重置与电子邮件验证」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Firebase Auth & Realtime Database Apps 课中编写并运行代码吗?

能。每节 Firebase Auth & Realtime Database Apps 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 实现电子邮件/密码身份验证
  2. 管理用户会话与状态
  3. 处理身份验证错误
  4. 密码重置与电子邮件验证
← 返回 Firebase Auth & Realtime Database Apps