firebase-auth-basics

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Prerequisites

前提条件

  • Firebase Project: Created via
    firebase projects:create
    (see
    firebase-basics
    ).
  • Firebase CLI: Installed and logged in (see
    firebase-basics
    ).
  • Firebase 项目:通过
    firebase projects:create
    创建(详见
    firebase-basics
    )。
  • Firebase CLI:已安装并完成登录(详见
    firebase-basics
    )。

Core Concepts

核心概念

Firebase Authentication provides backend services, easy-to-use SDKs, and ready-made UI libraries to authenticate users to your app.
Firebase Authentication 提供后端服务、易用的SDK以及现成的UI库,用于验证应用的用户身份。

Users

用户

A user is an entity that can sign in to your app. Each user is identified by a unique ID (
uid
) which is guaranteed to be unique across all providers. User properties include:
  • uid
    : Unique identifier.
  • email
    : User's email address (if available).
  • displayName
    : User's display name (if available).
  • photoURL
    : URL to user's photo (if available).
  • emailVerified
    : Boolean indicating if the email is verified.
用户是可以登录到您应用的实体。每个用户都由唯一ID(
uid
)标识,该ID在所有身份提供商中保证唯一。 用户属性包括:
  • uid
    :唯一标识符。
  • email
    :用户的电子邮箱地址(若存在)。
  • displayName
    :用户的显示名称(若存在)。
  • photoURL
    :用户头像的URL(若存在)。
  • emailVerified
    :表示电子邮箱是否已验证的布尔值。

Identity Providers

身份提供商

Firebase Auth supports multiple ways to sign in:
  • Email/Password: Basic email and password authentication.
  • Federated Identity Providers: Google, Facebook, Twitter, GitHub, Microsoft, Apple, etc.
  • Phone Number: SMS-based authentication.
  • Anonymous: Temporary guest accounts that can be linked to permanent accounts later.
  • Custom Auth: Integrate with your existing auth system.
Google Sign In is recommended as a good and secure default provider.
Firebase Auth 支持多种登录方式:
  • 邮箱/密码:基础的邮箱密码身份验证。
  • 联合身份提供商:Google、Facebook、Twitter、GitHub、Microsoft、Apple等。
  • 手机号码:基于短信的身份验证。
  • 匿名登录:临时访客账户,后续可关联至永久账户。
  • 自定义身份验证:与您现有的身份验证系统集成。
推荐使用Google Sign In作为安全可靠的默认身份提供商。

Tokens

令牌

When a user signs in, they receive an ID Token (JWT). This token is used to identify the user when making requests to Firebase services (Realtime Database, Cloud Storage, Firestore) or your own backend.
  • ID Token: Short-lived (1 hour), verifies identity.
  • Refresh Token: Long-lived, used to get new ID tokens.
用户登录后,会获得一个ID Token(JWT)。在向Firebase服务(Realtime Database、Cloud Storage、Firestore)或您自己的后端发起请求时,该令牌用于识别用户身份。
  • ID Token:短期有效(1小时),用于验证身份。
  • Refresh Token:长期有效,用于获取新的ID Token。

Workflow

工作流程

1. Provisioning

1. 配置准备

Option 1. Enabling Authentication via CLI

选项1. 通过CLI启用身份验证

Only Google Sign In, anonymous auth, and email/password auth can be enabled via CLI. For other providers, use the Firebase Console.
Configure Firebase Authentication in
firebase.json
by adding an 'auth' block:
{
  "auth": {
    "providers": {
      "anonymous": true,
      "emailPassword": true,
      "googleSignIn": {
        "oAuthBrandDisplayName": "Your Brand Name",
        "supportEmail": "support@example.com",
        "authorizedRedirectUris": ["https://example.com"]
      }
    }
  }
}
仅Google Sign In、匿名身份验证和邮箱/密码身份验证可通过CLI启用。其他身份提供商请使用Firebase控制台。
通过在
firebase.json
中添加'auth'块来配置Firebase Authentication:
{
  "auth": {
    "providers": {
      "anonymous": true,
      "emailPassword": true,
      "googleSignIn": {
        "oAuthBrandDisplayName": "Your Brand Name",
        "supportEmail": "support@example.com",
        "authorizedRedirectUris": ["https://example.com"]
      }
    }
  }
}

Option 2. Enabling Authentication in Console

选项2. 在控制台中启用身份验证

Enable other providers in the Firebase Console.
  1. Go to the https://console.firebase.google.com/project/_/authentication/providers
  2. Select your project.
  3. Enable the desired Sign-in providers (e.g., Email/Password, Google).
在Firebase控制台中启用其他身份提供商。
  1. 访问https://console.firebase.google.com/project/_/authentication/providers
  2. 选择您的项目。
  3. 启用所需的登录提供商(如邮箱/密码、Google)。

2. Client Setup & Usage

2. 客户端设置与使用

Web See references/client_sdk_web.md.
Web端 详见references/client_sdk_web.md

3. Security Rules

3. 安全规则

Secure your data using
request.auth
in Firestore/Storage rules.
See references/security_rules.md.
在Firestore/Storage规则中使用
request.auth
来保护您的数据。
详见references/security_rules.md