您可以透過 Firebase SDK 將一般 OAuth 登入功能整合至應用程式,執行端對端登入流程,讓使用者透過 Yahoo 等 OAuth 提供者向 Firebase 驗證身分。
事前準備
如要使用 Yahoo 帳戶登入使用者,請先為 Firebase 專案啟用 Yahoo 做為登入供應商:
- 將 Firebase 新增至 JavaScript 專案。
- 在 Firebase 控制台中,開啟「Auth」(驗證) 專區。
- 在「登入方式」分頁中,啟用「Yahoo」供應器。
- 將該供應商開發人員控制台的「用戶端 ID」和「用戶端密鑰」新增至供應商設定:
-
如要註冊 Yahoo OAuth 用戶端,請參閱 Yahoo 開發人員說明文件,瞭解如何 向 Yahoo 註冊網路應用程式。
請務必選取下列兩項 OpenID Connect API 權限:
profile和email。 - 向這些供應商註冊應用程式時,請務必將專案的
*.firebaseapp.com網域註冊為應用程式的重新導向網域。
-
- 按一下 [儲存]。
使用 Firebase SDK 處理登入流程
如果您要建構網頁應用程式,最簡單的方法是使用 Firebase JavaScript SDK 處理整個登入流程,讓使用者透過 Yahoo 帳戶驗證身分。
如要使用 Firebase JavaScript SDK 處理登入流程,請按照下列步驟操作:
使用供應商 ID yahoo.com 建立 OAuthProvider 的執行個體。
Web
import { OAuthProvider } from "firebase/auth"; const provider = new OAuthProvider('yahoo.com');
Web
var provider = new firebase.auth.OAuthProvider('yahoo.com');
選用:指定要隨 OAuth 要求傳送的其他自訂 OAuth 參數。
Web
provider.setCustomParameters({ // Prompt user to re-authenticate to Yahoo. prompt: 'login', // Localize to French. language: 'fr' });
Web
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 Permissions」(API 權限) 下方,要求 Yahoo API 的權限。要求的 OAuth 範圍必須與應用程式 API 權限中預先設定的範圍完全相符。舉例來說,如果要求讀取/寫入使用者聯絡人資料的權限,且已在應用程式的 API 權限中預先設定,則必須傳遞sdct-w,而非唯讀 OAuth 範圍sdct-r。否則流程會失敗,且系統會向使用者顯示錯誤訊息。Web
// 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
// 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
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
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:
firebase.auth().signInWithRedirect(provider);
使用者完成登入並返回頁面後,您可以呼叫
getRedirectResult取得登入結果。Web
import { getAuth, signInWithRedirect } from "firebase/auth"; const auth = getAuth(); signInWithRedirect(auth, provider);
Web
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
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
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
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
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 擴充功能應用程式,請參閱 螢幕外文件指南。
後續步驟
使用者首次登入後,系統會建立新的使用者帳戶,並連結至使用者登入時使用的憑證 (即使用者名稱和密碼、電話號碼或驗證供應商資訊)。這個新帳戶會儲存在 Firebase 專案中,可用於識別專案中每個應用程式的使用者,無論使用者登入方式為何。
-
在應用程式中,如要瞭解使用者的驗證狀態,建議在
Auth物件上設定觀察器。接著,您就可以從User物件取得使用者的基本個人資料資訊。請參閱「管理使用者」。 在 Firebase Realtime Database 和 Cloud Storage 安全規則中,您可以從
auth變數取得已登入使用者的專屬使用者 ID, 並使用該 ID 控制使用者可存取的資料。
您可以將驗證供應商憑證連結至現有使用者帳戶,允許使用者透過多個驗證供應商登入應用程式。
如要登出使用者,請呼叫
signOut:
Web
import { getAuth, signOut } from "firebase/auth"; const auth = getAuth(); signOut(auth).then(() => { // Sign-out successful. }).catch((error) => { // An error happened. });
Web
firebase.auth().signOut().then(() => { // Sign-out successful. }).catch((error) => { // An error happened. });