您可以將 Twitter 驗證整合至應用程式,讓使用者透過 Twitter 帳戶使用 Firebase 進行驗證。您可以透過 Firebase SDK 執行登入流程,或手動執行 Twitter OAuth 流程,並將產生的存取權杖和密鑰傳遞至 Firebase,藉此整合 Twitter 驗證。
事前準備
- 將 Firebase 新增至 JavaScript 專案。
- 在 Firebase 控制台中,依序前往 「Security」(安全性) >「Authentication」(驗證)。
- 在「登入方式」分頁中,啟用「Twitter」登入供應商。
-
將該供應商開發人員控制台中的 API 金鑰和 API 密鑰新增至供應商設定:
- 在 Twitter 將應用程式註冊為開發人員應用程式,並取得應用程式的 OAuth API 金鑰和 API 密鑰。
-
請務必在 Twitter 應用程式設定的應用程式設定頁面中,將 Firebase OAuth 重新導向 URI (例如
my-app-12345.firebaseapp.com/__/auth/handler) 設為授權回呼網址。
- 按一下「儲存」。
使用 Firebase SDK 處理登入流程
如果您正在建構網頁應用程式,最簡單的方法是使用 Firebase JavaScript SDK 處理登入流程,讓使用者透過 Twitter 帳戶進行 Firebase 驗證。(如要在 Node.js 或其他非瀏覽器環境中驗證使用者,則必須手動處理登入流程)。
如要使用 Firebase JavaScript SDK 處理登入流程,請按照下列步驟操作:
- 建立 Twitter 供應商物件的執行個體:
Web
import { TwitterAuthProvider } from "firebase/auth"; const provider = new TwitterAuthProvider();
Web
var provider = new firebase.auth.TwitterAuthProvider();
- 選用:如要將供應商的 OAuth 流程本地化為使用者偏好的語言,而不明確傳遞相關的自訂 OAuth 參數,請在啟動 OAuth 流程前,更新 Auth 執行個體上的語言代碼。例如:
Web
import { getAuth } from "firebase/auth"; const auth = getAuth(); auth.languageCode = 'it'; // To apply the default browser preference instead of explicitly setting it. // auth.useDeviceLanguage();
Web
firebase.auth().languageCode = 'it'; // To apply the default browser preference instead of explicitly setting it. // firebase.auth().useDeviceLanguage();
- 選用:指定要與 OAuth 要求一併傳送的其他自訂 OAuth 供應商參數。如要新增自訂參數,請在已初始化的供應商上呼叫
setCustomParameters,並傳入一個物件,其中包含 OAuth 供應商說明文件指定的鍵和對應的值。例如:不允許使用保留的必要 OAuth 參數,系統會忽略這些參數。 詳情請參閱驗證供應商參考資料。Web
provider.setCustomParameters({ 'lang': 'es' });
Web
provider.setCustomParameters({ 'lang': 'es' });
- 使用 Twitter 供應商物件向 Firebase 進行驗證。您可以開啟彈出式視窗或重新導向至登入頁面,提示使用者以 Twitter 帳戶登入。在行動裝置上,建議使用重新導向方法。
- 如要使用彈出式視窗登入,請呼叫
signInWithPopup:此外,您也可以擷取 Twitter 供應商的 OAuth 權杖,以便使用 Twitter API 擷取其他資料。Web
import { getAuth, signInWithPopup, TwitterAuthProvider } from "firebase/auth"; const auth = getAuth(); signInWithPopup(auth, provider) .then((result) => { // This gives you a the Twitter OAuth 1.0 Access Token and Secret. // You can use these server side with your app's credentials to access the Twitter API. const credential = TwitterAuthProvider.credentialFromResult(result); const token = credential.accessToken; const secret = credential.secret; // The signed-in user info. const user = result.user; // IdP data available using getAdditionalUserInfo(result) // ... }).catch((error) => { // Handle Errors here. const errorCode = error.code; const errorMessage = error.message; // The email of the user's account used. const email = error.customData.email; // The AuthCredential type that was used. const credential = TwitterAuthProvider.credentialFromError(error); // ... });
Web
firebase .auth() .signInWithPopup(provider) .then((result) => { /** @type {firebase.auth.OAuthCredential} */ var credential = result.credential; // This gives you a the Twitter OAuth 1.0 Access Token and Secret. // You can use these server side with your app's credentials to access the Twitter API. var token = credential.accessToken; var secret = credential.secret; // The signed-in user info. var user = result.user; // IdP data available in result.additionalUserInfo.profile. // ... }).catch((error) => { // Handle Errors here. var errorCode = error.code; var errorMessage = error.message; // The email of the user's account used. var email = error.email; // The firebase.auth.AuthCredential type that was used. var credential = error.credential; // ... });
您也可以在這裡擷取及處理錯誤。如需錯誤代碼清單,請參閱驗證參考文件。
- 如要重新導向至登入頁面來登入,請呼叫
signInWithRedirect: 使用「signInWithRedirect」時,請遵循最佳做法。接著,您也可以在網頁載入時呼叫Web
import { getAuth, signInWithRedirect } from "firebase/auth"; const auth = getAuth(); signInWithRedirect(auth, provider);
Web
firebase.auth().signInWithRedirect(provider);
getRedirectResult,擷取 Twitter 供應商的 OAuth 權杖:您也可以在這裡擷取及處理錯誤。如需錯誤代碼清單,請參閱驗證參考文件。Web
import { getAuth, getRedirectResult, TwitterAuthProvider } from "firebase/auth"; const auth = getAuth(); getRedirectResult(auth) .then((result) => { // This gives you a the Twitter OAuth 1.0 Access Token and Secret. // You can use these server side with your app's credentials to access the Twitter API. const credential = TwitterAuthProvider.credentialFromResult(result); const token = credential.accessToken; const secret = credential.secret; // ... // The signed-in user info. const user = result.user; // IdP data available using getAdditionalUserInfo(result) // ... }).catch((error) => { // Handle Errors here. const errorCode = error.code; const errorMessage = error.message; // The email of the user's account used. const email = error.customData.email; // The AuthCredential type that was used. const credential = TwitterAuthProvider.credentialFromError(error); // ... });
Web
firebase.auth() .getRedirectResult() .then((result) => { if (result.credential) { /** @type {firebase.auth.OAuthCredential} */ var credential = result.credential; // This gives you a the Twitter OAuth 1.0 Access Token and Secret. // You can use these server side with your app's credentials to access the Twitter API. var token = credential.accessToken; var secret = credential.secret; // ... } // The signed-in user info. var user = result.user; // IdP data available in result.additionalUserInfo.profile. // ... }).catch((error) => { // Handle Errors here. var errorCode = error.code; var errorMessage = error.message; // The email of the user's account used. var email = error.email; // The firebase.auth.AuthCredential type that was used. var credential = error.credential; // ... });
- 如要使用彈出式視窗登入,請呼叫
手動處理登入流程
您也可以使用 Twitter 帳戶向 Firebase 驗證,方法是呼叫 Twitter OAuth 端點來處理登入流程:
- 請按照開發人員說明文件,將 Twitter 驗證整合至應用程式。Twitter 登入流程結束時,您會收到 OAuth 存取權杖和 OAuth 密鑰。
- 如需登入 Node.js 應用程式,請將 OAuth 存取權杖和 OAuth 密鑰傳送至 Node.js 應用程式。
- 使用者成功透過 Twitter 登入後,請將 OAuth 存取權杖和 OAuth 密鑰換成 Firebase 憑證:
var credential = firebase.auth.TwitterAuthProvider.credential(token, secret);
- 使用 Firebase 憑證向 Firebase 進行驗證:
Web
import { getAuth, signInWithCredential, FacebookAuthProvider } from "firebase/auth"; // Sign in with the credential from the Facebook user. const auth = getAuth(); signInWithCredential(auth, credential) .then((result) => { // Signed in const credential = FacebookAuthProvider.credentialFromResult(result); }) .catch((error) => { // Handle Errors here. const errorCode = error.code; const errorMessage = error.message; // The email of the user's account used. const email = error.customData.email; // The AuthCredential type that was used. const credential = FacebookAuthProvider.credentialFromError(error); // ... });
Web
// Sign in with the credential from the Facebook user. firebase.auth().signInWithCredential(credential) .then((result) => { // Signed in var credential = result.credential; // ... }) .catch((error) => { // Handle Errors here. var errorCode = error.code; var errorMessage = error.message; // The email of the user's account used. var email = error.email; // The firebase.auth.AuthCredential type that was used. var credential = error.credential; // ... });
在 Chrome 擴充功能中使用 Firebase 進行驗證
如果您正在建構 Chrome 擴充功能應用程式,請參閱螢幕外文件指南。
後續步驟
使用者首次登入後,系統會建立新的使用者帳戶,並連結至使用者登入時使用的憑證,也就是使用者名稱和密碼、電話號碼或驗證提供者資訊。這個新帳戶會儲存為 Firebase 專案的一部分,可用於識別專案中所有應用程式的使用者,無論使用者如何登入。
-
在應用程式中,建議在
Auth物件上設定觀察器,以瞭解使用者的驗證狀態。接著,您可以從User物件取得使用者的基本個人資料資訊。請參閱「管理使用者」。 在 Firebase Realtime Database 和 Cloud Storage安全性規則中,您可以從
auth變數取得已登入使用者的專屬使用者 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. });