您可以使用 Firebase SDK 將通用 OAuth 登入整合到您的應用程式中,以執行端對端登入流程,讓您的使用者使用 Yahoo 等 OAuth 提供者透過 Firebase 進行身份驗證。
在你開始之前
若要使用 Yahoo 帳戶登入用戶,您必須先啟用 Yahoo 作為 Firebase 專案的登入提供者:
- 將 Firebase 新增到您的 JavaScript 專案。
- 在Firebase 控制台中,開啟「驗證」部分。
- 在「登入方法」標籤上,啟用Yahoo提供者。
- 將客戶端 ID和客戶端金鑰從該提供者的開發人員控制台新增至提供者設定:
要註冊 Yahoo OAuth 用戶端,請遵循有關向 Yahoo 註冊 Web 應用程式的Yahoo 開發人員文件。
請務必選擇兩個 OpenID Connect API 權限:
profile
和email
。- 向這些提供者註冊應用程式時,請務必將專案的
*.firebaseapp.com
網域註冊為應用程式的重新導向網域。
- 按一下「儲存」 。
使用 Firebase SDK 處理登入流程
如果您正在建立 Web 應用程序,使用 Yahoo 帳戶透過 Firebase 對使用者進行身份驗證的最簡單方法是使用 Firebase JavaScript SDK 處理整個登入流程。
若要使用 Firebase JavaScript SDK 處理登入流程,請依照下列步驟操作:
使用提供者 ID yahoo.com建立OAuthProvider的實例。
Web modular API
import { OAuthProvider } from "firebase/auth"; const provider = new OAuthProvider('yahoo.com');
Web namespaced API
var provider = new firebase.auth.OAuthProvider('yahoo.com');
可選:指定要與 OAuth 請求一起傳送的其他自訂 OAuth 參數。
Web modular API
provider.setCustomParameters({ // Prompt user to re-authenticate to Yahoo. prompt: 'login', // Localize to French. language: 'fr' });
Web namespaced API
provider.setCustomParameters({ // Prompt user to re-authenticate to Yahoo. prompt: 'login', // Localize to French. language: 'fr' });
有關 Yahoo 支援的參數,請參閱Yahoo OAuth 文件。請注意,您無法使用
setCustomParameters()
傳遞 Firebase 所需的參數。這些參數是client_id 、 redirect_uri 、 response_type 、 scope和state 。可選:指定您想要從身分驗證提供者要求的
profile
和email
以外的其他 OAuth 2.0 範圍。如果您的應用程式需要從 Yahoo API 存取私人使用者數據,您需要在 Yahoo 開發者控制台的API 權限下請求對 Yahoo API 的權限。請求的 OAuth 範圍必須與應用程式 API 權限中預先配置的範圍完全相符。例如,如果向使用者聯絡人請求讀取/寫入存取權限並在應用程式的 API 權限中預先配置,則必須傳遞sdct-w
而不是只讀 OAuth 範圍sdct-r
。否則,流程將失敗並向最終使用者顯示錯誤。Web modular API
// Request access to Yahoo Mail API. provider.addScope('mail-r'); // Request read/write access to user contacts. // This must be preconfigured in the app's API permissions. provider.addScope('sdct-w');
Web namespaced API
// Request access to Yahoo Mail API. provider.addScope('mail-r'); // Request read/write access to user contacts. // This must be preconfigured in the app's API permissions. provider.addScope('sdct-w');
要了解更多信息,請參閱Yahoo 範圍文件。
使用 OAuth 提供者物件透過 Firebase 進行身份驗證。您可以透過開啟彈出視窗或重定向到登入頁面來提示使用者使用其 Yahoo 帳戶登入。重定向方法在行動裝置上是首選。
若要使用彈出視窗登錄,請呼叫
signInWithPopup
:Web modular API
import { getAuth, signInWithPopup, OAuthProvider } from "firebase/auth"; const auth = getAuth(); signInWithPopup(auth, provider) .then((result) => { // IdP data available in result.additionalUserInfo.profile // ... // Yahoo OAuth access token and ID token can be retrieved by calling: const credential = OAuthProvider.credentialFromResult(result); const accessToken = credential.accessToken; const idToken = credential.idToken; }) .catch((error) => { // Handle error. });
Web namespaced API
firebase.auth().signInWithPopup(provider) .then((result) => { // IdP data available in result.additionalUserInfo.profile // ... /** @type {firebase.auth.OAuthCredential} */ const credential = result.credential; // Yahoo OAuth access token and ID token can be retrieved by calling: var accessToken = credential.accessToken; var idToken = credential.idToken; }) .catch((error) => { // Handle error. });
若要透過重定向到登錄頁面來登錄,請呼叫
signInWithRedirect
:
使用
signInWithRedirect
、linkWithRedirect
或reauthenticateWithRedirect
時請遵循最佳實務。firebase.auth().signInWithRedirect(provider);
使用者完成登入並返回頁面後,您可以透過呼叫
getRedirectResult
來取得登入結果。Web modular API
import { getAuth, signInWithRedirect } from "firebase/auth"; const auth = getAuth(); signInWithRedirect(auth, provider);
Web namespaced API
firebase.auth().signInWithRedirect(provider);
成功完成後,可以從傳回的
firebase.auth.UserCredential
物件中檢索與提供者關聯的 OAuth ID 令牌和存取權杖。使用 OAuth 存取令牌,您可以呼叫Yahoo API 。
例如,要取得基本的個人資料信息,可以呼叫以下REST API:
curl -i -H "Authorization: Bearer ACCESS_TOKEN" https://social.yahooapis.com/v1/user/YAHOO_USER_UID/profile?format=json
其中
YAHOO_USER_UID
是 Yahoo 使用者的 ID,可以從firebase.auth().currentUser.providerData[0].uid
欄位或result.additionalUserInfo.profile
檢索。雖然上面的範例重點關注登入流程,但您也可以使用
linkWithPopup
/linkWithRedirect
將 Yahoo 提供者連結到現有用戶。例如,您可以將多個提供者連結到同一用戶,允許他們使用其中任一提供者登入。Web modular API
import { getAuth, linkWithPopup, OAuthProvider } from "firebase/auth"; const provider = new OAuthProvider('yahoo.com'); const auth = getAuth(); linkWithPopup(auth.currentUser, provider) .then((result) => { // Yahoo credential is linked to the current user. // IdP data available in result.additionalUserInfo.profile. // Get the OAuth access token and ID Token const credential = OAuthProvider.credentialFromResult(result); const accessToken = credential.accessToken; const idToken = credential.idToken; }) .catch((error) => { // Handle error. });
Web namespaced API
var provider = new firebase.auth.OAuthProvider('yahoo.com'); firebase.auth().currentUser.linkWithPopup(provider) .then((result) => { // Yahoo credential is linked to the current user. // IdP data available in result.additionalUserInfo.profile. // Yahoo OAuth access token can be retrieved by calling: // result.credential.accessToken // Yahoo OAuth ID token can be retrieved by calling: // result.credential.idToken }) .catch((error) => { // Handle error. });
相同的模式可以與
reauthenticateWithPopup
/reauthenticateWithRedirect
一起使用,它可用於檢索需要最近登入的敏感操作的新憑證。Web modular API
import { getAuth, reauthenticateWithPopup, OAuthProvider } from "firebase/auth"; const provider = new OAuthProvider('yahoo.com'); const auth = getAuth(); reauthenticateWithPopup(auth.currentUser, provider) .then((result) => { // User is re-authenticated with fresh tokens minted and // should be able to perform sensitive operations like account // deletion and email or password update. // IdP data available in result.additionalUserInfo.profile. // Get the OAuth access token and ID Token const credential = OAuthProvider.credentialFromResult(result); const accessToken = credential.accessToken; const idToken = credential.idToken; }) .catch((error) => { // Handle error. });
Web namespaced API
var provider = new firebase.auth.OAuthProvider('yahoo.com'); firebase.auth().currentUser.reauthenticateWithPopup(provider) .then((result) => { // User is re-authenticated with fresh tokens minted and // should be able to perform sensitive operations like account // deletion and email or password update. // IdP data available in result.additionalUserInfo.profile. // Yahoo OAuth access token can be retrieved by calling: // result.credential.accessToken // Yahoo OAuth ID token can be retrieved by calling: // result.credential.idToken }) .catch((error) => { // Handle error. });
在 Chrome 擴充功能中使用 Firebase 進行驗證
如果您正在建立 Chrome 擴充功能應用程式,則必須新增您的 Chrome 擴充功能 ID:
- 在Firebase 控制台中開啟您的專案。
- 在「身份驗證」部分中,開啟「登入方法」頁面。
- 將如下所示的 URI 加入到授權網域清單中:
chrome-extension://CHROME_EXTENSION_ID
Chrome 擴充功能只能使用彈出操作( signInWithPopup
、 linkWithPopup
和reauthenticateWithPopup
),因為 Chrome 擴充功能無法使用 HTTP 重新導向。您應該從後台頁面腳本而不是瀏覽器操作彈出視窗呼叫這些方法,因為身份驗證彈出視窗將取消瀏覽器操作彈出視窗。彈出方法只能在使用Manifest V2 的擴充中使用。較新的Manifest V3只允許 Service Worker 形式的後台腳本,根本無法執行彈出操作。
在 Chrome 擴充功能的清單檔案中,請確保將https://apis.google.com
網址新增至content_security_policy
白名單中。
下一步
使用者首次登入後,系統會建立新的使用者帳戶,並將其連結到使用者登入時所使用的憑證(即使用者名稱和密碼、電話號碼或驗證提供者資訊)。此新帳戶將作為 Firebase 專案的一部分存儲,並且可用於識別專案中每個應用程式中的用戶,無論用戶如何登入。
在您的應用程式中,了解使用者身份驗證狀態的建議方法是在
Auth
物件上設定觀察者。然後,您可以從User
物件取得使用者的基本個人資料資訊。請參閱管理用戶。在 Firebase 即時資料庫和雲端儲存安全性規則中,您可以從
auth
變數取得登入使用者的唯一使用者 ID,並使用它來控制使用者可以存取哪些資料。
您可以透過將身分驗證提供者憑證連結到現有使用者帳戶,允許使用者使用多個驗證提供者登入您的應用程式。
若要登出用戶,請呼叫signOut
:
Web modular API
import { getAuth, signOut } from "firebase/auth"; const auth = getAuth(); signOut(auth).then(() => { // Sign-out successful. }).catch((error) => { // An error happened. });
Web namespaced API
firebase.auth().signOut().then(() => { // Sign-out successful. }).catch((error) => { // An error happened. });