您可以使用 Firebase 驗證,允許使用者使用一種或多種登入方法登入您的應用程式,包括電子郵件地址和密碼登錄,以及聯合身分提供者(例如 Google 登入和 Facebook 登入)。本教學向您展示如何為您的應用程式新增電子郵件地址和密碼登錄,協助您開始使用 Firebase 身份驗證。
新增並初始化認證SDK
新增 Firebase 身份驗證 JS SDK 並初始化 Firebase 身份驗證:
網路模組化API
import { initializeApp } from "firebase/app"; import { getAuth } from "firebase/auth"; // TODO: Replace the following with your app's Firebase project configuration // See: https://firebase.google.com/docs/web/learn-more#config-object const firebaseConfig = { // ... }; // Initialize Firebase const app = initializeApp(firebaseConfig); // Initialize Firebase Authentication and get a reference to the service const auth = getAuth(app);
Web 命名空間 API
import firebase from "firebase/compat/app"; import "firebase/compat/auth"; // TODO: Replace the following with your app's Firebase project configuration // See: https://firebase.google.com/docs/web/learn-more#config-object const firebaseConfig = { // ... }; // Initialize Firebase firebase.initializeApp(firebaseConfig); // Initialize Firebase Authentication and get a reference to the service const auth = firebase.auth();
(可選)使用 Firebase 本機模擬器套件進行原型設計和測試
在討論您的應用程式如何對使用者進行身份驗證之前,我們先介紹一組可用於原型設計和測試身份驗證功能的工具:Firebase Local Emulator Suite。如果您正在選擇身份驗證技術和提供者,使用身份驗證和Firebase 安全性規則嘗試使用公共和私有資料的不同資料模型,或者對登入UI 設計進行原型設計,那麼能夠在本地工作而無需部署即時服務可能是一個好主意。
身份驗證模擬器是本機模擬器套件的一部分,它使您的應用程式能夠與模擬資料庫內容和配置以及可選的模擬項目資源(函數、其他資料庫和安全規則)進行互動。
使用身份驗證模擬器只需幾個步驟:
- 將一行程式碼新增至應用程式的測試配置以連接到模擬器。
- 從本地專案目錄的根目錄中,運行
firebase emulators:start
。 - 使用本機模擬器套件 UI 進行互動式原型設計,或使用驗證模擬器 REST API 進行非互動式測試。
詳細指南可在將您的應用程式連接到身份驗證模擬器中找到。有關詳細信息,請參閱本地模擬器套件簡介。
現在讓我們繼續了解如何對使用者進行身份驗證。
註冊新用戶
建立一個表單,允許新使用者使用他們的電子郵件地址和密碼在您的應用程式中註冊。當使用者填寫表單時,請驗證使用者提供的電子郵件地址和密碼,然後將它們傳遞給createUserWithEmailAndPassword
方法:
Web modular API
import { getAuth, createUserWithEmailAndPassword } from "firebase/auth"; const auth = getAuth(); createUserWithEmailAndPassword(auth, email, password) .then((userCredential) => { // Signed up const user = userCredential.user; // ... }) .catch((error) => { const errorCode = error.code; const errorMessage = error.message; // .. });
Web namespaced API
firebase.auth().createUserWithEmailAndPassword(email, password) .then((userCredential) => { // Signed in var user = userCredential.user; // ... }) .catch((error) => { var errorCode = error.code; var errorMessage = error.message; // .. });
登入現有用戶
建立一個表單,允許現有使用者使用其電子郵件地址和密碼登入。當使用者完成表單後,呼叫signInWithEmailAndPassword
方法:
Web modular API
import { getAuth, signInWithEmailAndPassword } from "firebase/auth"; const auth = getAuth(); signInWithEmailAndPassword(auth, email, password) .then((userCredential) => { // Signed in const user = userCredential.user; // ... }) .catch((error) => { const errorCode = error.code; const errorMessage = error.message; });
Web namespaced API
firebase.auth().signInWithEmailAndPassword(email, password) .then((userCredential) => { // Signed in var user = userCredential.user; // ... }) .catch((error) => { var errorCode = error.code; var errorMessage = error.message; });
設定認證狀態觀察者並取得使用者數據
對於需要登入使用者資訊的每個應用程式頁面,將觀察者附加到全域身份驗證物件。每當使用者的登入狀態改變時,就會呼叫此觀察者。
使用onAuthStateChanged
方法附加觀察者。當使用者成功登入後,您可以在觀察者中獲取有關該使用者的信息。
Web modular API
import { getAuth, onAuthStateChanged } from "firebase/auth"; const auth = getAuth(); onAuthStateChanged(auth, (user) => { if (user) { // User is signed in, see docs for a list of available properties // https://firebase.google.com/docs/reference/js/auth.user const uid = user.uid; // ... } else { // User is signed out // ... } });
Web namespaced API
firebase.auth().onAuthStateChanged((user) => { if (user) { // User is signed in, see docs for a list of available properties // https://firebase.google.com/docs/reference/js/v8/firebase.User var uid = user.uid; // ... } else { // User is signed out // ... } });
下一步
了解如何新增對其他身分提供者和匿名訪客帳號的支援: