您可以通過使用 Firebase SDK 將通用 OAuth 登錄集成到您的應用中來執行端到端登錄流程,從而讓您的用戶使用 Yahoo 等 OAuth 提供商向 Firebase 進行身份驗證。
在你開始之前
要使用 Yahoo 帳戶登錄用戶,您必須首先啟用 Yahoo 作為 Firebase 項目的登錄提供程序:
- 將 Firebase 添加到您的 Apple 項目中。
- 在Firebase 控制台中,打開Auth部分。
- 在登錄方法選項卡上,啟用Yahoo提供程序。
- 將該提供者的開發者控制台中的客戶端 ID和客戶端密碼添加到提供者配置中:
要註冊 Yahoo OAuth 客戶端,請遵循 Yahoo 開發人員文檔中關於向 Yahoo註冊 Web 應用程序的操作。
請務必選擇兩個 OpenID Connect API 權限:
profile
和email
。- 向這些提供商註冊應用程序時,請務必將項目的
*.firebaseapp.com
域註冊為應用程序的重定向域。
- 單擊保存。
使用 Firebase SDK 處理登錄流程
要使用 Firebase Apple 平台 SDK 處理登錄流程,請執行以下步驟:
將自定義 URL 方案添加到您的 Xcode 項目:
- 打開您的項目配置:雙擊左側樹視圖中的項目名稱。從TARGETS部分中選擇您的應用程序,然後選擇Info選項卡,然後展開URL Types部分。
- 單擊+按鈕,並為您的反向客戶端 ID 添加 URL 方案。要查找此值,請打開
配置文件,然後查找GoogleService-Info.plist REVERSED_CLIENT_ID
鍵。複製該鍵的值,並將其粘貼到配置頁面上的URL 方案框中。將其他字段留空。完成後,您的配置應類似於以下內容(但使用特定於應用程序的值):
使用提供者 ID yahoo.com創建OAuthProvider的實例。
迅速
var provider = OAuthProvider(providerID: "yahoo.com")
Objective-C
FIROAuthProvider *provider = [FIROAuthProvider providerWithProviderID:@"yahoo.com"];
可選:指定要隨 OAuth 請求一起發送的其他自定義 OAuth 參數。
迅速
provider.customParameters = [ "prompt": "login", "language": "fr" ]
Objective-C
[provider setCustomParameters:@{@"prompt": @"login", @"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下請求對 Yahoo API 的權限。請求的 OAuth 範圍必須與應用 API 權限中預配置的範圍完全匹配。例如,如果向用戶聯繫人請求讀/寫訪問權限並在應用程序的 API 權限中預配置,則必須傳遞sdct-w
而不是只讀 OAuth 範圍sdct-r
。否則,流程將失敗,並向最終用戶顯示錯誤。迅速
// Request access to Yahoo Mail API. // Request read/write access to user contacts. // This must be preconfigured in the app's API permissions. provider.scopes = ["mail-r", "sdct-w"]
Objective-C
// Request access to Yahoo Mail API. // Request read/write access to user contacts. // This must be preconfigured in the app's API permissions. [provider setScopes:@[@"mail-r", @"sdct-w"]];
要了解更多信息,請參閱Yahoo 範圍文檔。
可選:如果您想自定義應用在向用戶顯示 reCAPTCHA 時呈現
SFSafariViewController
或UIWebView
的方式,請創建一個符合FIRAuthUIDelegate
協議的自定義類,並將其傳遞給getCredentialWithUIDelegate:completion:
。使用 OAuth 提供程序對象向 Firebase 進行身份驗證。
迅速
provider.getCredentialWith(nil) { credential, error in if error != nil { // Handle error. } if credential != nil { Auth().signIn(with: credential) { authResult, error in if error != nil { // Handle error. } // User is signed in. // IdP data available in authResult.additionalUserInfo.profile. // Yahoo OAuth access token can also be retrieved by: // authResult.credential.accessToken // Yahoo OAuth ID token can be retrieved by calling: // authResult.credential.idToken } } }
Objective-C
[provider getCredentialWithUIDelegate:nil completion:^(FIRAuthCredential *_Nullable credential, NSError *_Nullable error) { if (error) { // Handle error. } if (credential) { [[FIRAuth auth] signInWithCredential:credential completion:^(FIRAuthDataResult *_Nullable authResult, NSError *_Nullable error) { if (error) { // Handle error. } // User is signed in. // IdP data available in authResult.additionalUserInfo.profile. // Yahoo OAuth access token can also be retrieved by: // authResult.credential.accessToken // Yahoo OAuth ID token can be retrieved by calling: // authResult.credential.idToken }]; } }];
使用 OAuth 訪問令牌,您可以調用Yahoo API 。
例如,要獲取基本配置文件信息,您可以調用 REST API,在
Authorization
標頭中傳遞訪問令牌:https://social.yahooapis.com/v1/user/YAHOO_USER_UID/profile?format=json
其中
YAHOO_USER_UID
是 Yahoo 用戶的 ID,可以從Auth.auth.currentUser.providerData[0].uid
字段或authResult.additionalUserInfo.profile
。雖然上述示例側重於登錄流程,但您還可以使用
linkWithPopup
將 Yahoo 提供程序鏈接到現有用戶。例如,您可以將多個提供商鏈接到同一個用戶,允許他們使用其中任何一個登錄。迅速
Auth().currentUser.link(withCredential: credential) { authResult, error in if error != nil { // Handle error. } // Yahoo credential is linked to the current user. // IdP data available in authResult.additionalUserInfo.profile. // Yahoo OAuth access token can also be retrieved by: // authResult.credential.accessToken // Yahoo OAuth ID token can be retrieved by calling: // authResult.credential.idToken }
Objective-C
[[FIRAuth auth].currentUser linkWithCredential:credential completion:^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) { if (error) { // Handle error. } // Yahoo credential is linked to the current user. // IdP data available in authResult.additionalUserInfo.profile. // Yahoo OAuth access token is can also be retrieved by: // authResult.credential.accessToken // Yahoo OAuth ID token can be retrieved by calling: // authResult.credential.idToken }];
相同的模式可以與
reauthenticateWithPopup
/reauthenticateWithRedirect
一起使用,可用於檢索需要最近登錄的敏感操作的新憑據。迅速
Auth().currentUser.reauthenticateWithCredential(withCredential: credential) { authResult, error in if error != nil { // Handle error. } // 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. // Additional OAuth access token is can also be retrieved by: // authResult.credential.accessToken // Yahoo OAuth ID token can be retrieved by calling: // authResult.credential.idToken }
Objective-C
[[FIRAuth auth].currentUser reauthenticateWithCredential:credential completion:^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) { if (error) { // Handle error. } // 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. // Additional OAuth access token is can also be retrieved by: // authResult.credential.accessToken // Yahoo OAuth ID token can be retrieved by calling: // authResult.credential.idToken }];
下一步
用戶首次登錄後,會創建一個新用戶帳戶並將其鏈接到憑據(即用戶名和密碼、電話號碼或身份驗證提供商信息),即用戶登錄時使用的憑據。這個新帳戶作為 Firebase 項目的一部分存儲,可用於在項目中的每個應用中識別用戶,無論用戶如何登錄。
在您的 Firebase 實時數據庫和雲存儲安全規則中,您可以從
auth
變量中獲取登錄用戶的唯一用戶 ID,並使用它來控制用戶可以訪問哪些數據。
您可以通過將身份驗證提供程序憑據鏈接到現有用戶帳戶來允許用戶使用多個身份驗證提供程序登錄您的應用程序。
要註銷用戶,請調用signOut:
。
迅速
let firebaseAuth = Auth.auth() do { try firebaseAuth.signOut() } catch let signOutError as NSError { print("Error signing out: %@", signOutError) }
Objective-C
NSError *signOutError; BOOL status = [[FIRAuth auth] signOut:&signOutError]; if (!status) { NSLog(@"Error signing out: %@", signOutError); return; }
您可能還想為所有身份驗證錯誤添加錯誤處理代碼。請參閱處理錯誤。