firebase-auth-basics

作者: firebase

配置Firebase身份验证,支持多种身份提供商并确保数据访问安全。支持邮箱/密码、手机号、匿名登录、联合身份提供商(Google、Facebook、Twitter、GitHub、Microsoft、Apple)以及自定义身份验证集成。用户通过唯一UID标识,可选属性包括邮箱、显示名称、照片URL和邮箱验证状态。通过CLI进行身份验证可实现Google登录、匿名认证和邮箱/密码登录;其他提供商需使用Firebase...

npx skills add https://github.com/firebase/skills --skill firebase-auth-basics

Prerequisites

  • Firebase Project: Created via npx -y firebase-tools@latest projects:create (see firebase-basics).
  • Firebase CLI: Installed and logged in (see firebase-basics).

Core Concepts

Firebase Authentication provides backend services, easy-to-use SDKs, and ready-made UI libraries to authenticate users to your app.

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.

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.

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.

Workflow

1. Provisioning

Option 1. Enabling Authentication via 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": "[email protected]",
        "authorizedRedirectUris": ["https://example.com", "http://localhost"]
      }
    }
  }
}

[!NOTE] If the Google Sign-In popup opens and immediately closes with the error [firebase_auth/unauthorized-domain], it means the domain is not authorized. For local development, ensure localhost is included in the Authorized Domains list in the Firebase Console or via the authorizedDomains field in firebase.json. CRITICAL: Do NOT include the protocol or port number in the Authorized Domains list (e.g., use localhost, NOT http://localhost:9090).

CRITICAL: After configuring firebase.json, you MUST deploy the auth configuration to the Firebase backend for the changes to take effect. This is essential for auth providers like Google Sign-In, email/password, etc. to auto-generate the necessary OAuth clients for your app platforms. Run:

npx -y firebase-tools@latest deploy --only auth

Option 2. Enabling Authentication in Console

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).

2. Client Setup & Usage

Web See references/client_sdk_web.md.

Flutter See references/flutter_setup.md. Android (Kotlin) See references/client_sdk_android.md.

3. Security Rules

Secure your data using request.auth in Firestore/Storage rules.

See references/security_rules.md.

来自 firebase 的更多技能

firebase-remote-config-basics
firebase
Firebase Remote Config 的全面指南,涵盖模板管理和 SDK 使用。当用户需要帮助设置 Remote Config、管理功能标志或动态更新应用行为时,使用此技能。
officialdevelopmentapi
developing-genkit-dart
firebase
面向 Dart 的统一 AI SDK,支持代码生成、结构化输出、工具、流程和智能体。提供核心 API,涵盖生成、工具定义、流程编排、嵌入和流式处理,采用统一接口。包含 8 个以上插件,支持 LLM 提供商(Google Gemini、Anthropic Claude、OpenAI GPT)、Firebase AI、模型上下文协议、Chrome 浏览器集成,以及通过 Shelf 托管 HTTP 服务器。内置 CLI 和本地开发 UI,用于流程执行、追踪、模型实验等。
official
developing-genkit-go
firebase
使用 Go 语言中的 Genkit 开发 AI 驱动的应用程序。当用户要求使用 Genkit 在 Go 中构建 AI 功能、代理、流程或工具时使用,或在处理…
official
developing-genkit-js
firebase
使用Genkit流程、工具和多模型支持,构建AI驱动的Node.js/TypeScript应用程序。Genkit与提供商无关;通过插件支持Google AI、OpenAI、Anthropic、Ollama及其他LLM提供商。使用Zod定义类型安全的模式流程,执行生成请求,并在TypeScript中组合多步骤AI工作流。需要Genkit CLI v1.29.0及以上版本;近期主要API变更意味着您必须查阅genkit文档:read和common-errors.md以获取当前模式,而非依赖先前知识...
official
developing-genkit-python
firebase
使用 Python 中的 Genkit 开发 AI 驱动的应用程序。当用户询问关于 Genkit、AI 代理、流程或 Python 中的工具,或遇到 Genkit 相关问题时使用。
official
firebase-ai-logic
firebase
客户端Gemini集成,适用于网页应用,支持多模态推理、流式传输及设备端混合执行。支持纯文本和多模态输入(图像、音频、视频、PDF);超过20MB的文件通过Cloud Storage路由。包含自动历史记录的聊天会话、用于实时显示的流式响应,以及结构化JSON输出强制。通过Chrome中的Gemini Nano提供混合设备端推理,并自动回退到云端执行。生产环境需启用App Check...
official
firebase-ai-logic-basics
firebase
用于将Firebase AI Logic(Gemini API)集成到Web应用程序中的官方技能。涵盖设置、多模态推理、结构化输出和安全性。
official
firebase-app-hosting-basics
firebase
使用Firebase App Hosting部署和管理基于Next.js、Angular及其他支持框架的全栈Web应用。需使用Blaze定价计划的Firebase项目;支持服务端渲染(SSR)和增量静态再生(ISR)工作流。通过firebase.json配置部署,可选apphosting.yaml进行后端设置,或通过GitHub集成启用自动化"git push部署"。包含通过CLI命令进行密钥管理,以安全访问敏感密钥...
official