在 Apple 平台上使用 Yahoo 驗證

您可以透過 Firebase SDK 將一般 OAuth 登入功能整合至應用程式,執行端對端登入流程,讓使用者透過 Yahoo 等 OAuth 提供者向 Firebase 驗證身分。

事前準備

如要使用 Yahoo 帳戶登入使用者,請先為 Firebase 專案啟用 Yahoo 做為登入供應商:

  1. 將 Firebase 新增至 Apple 專案
  2. Firebase 控制台中,依序前往「安全性」>「驗證」
  3. 在「Sign-in method」(登入方法) 分頁中,啟用「Yahoo」登入供應商。
  4. 將該供應商開發人員控制台的「用戶端 ID」和「用戶端密鑰」新增至供應商設定:
    1. 如要註冊 Yahoo OAuth 用戶端,請參閱 Yahoo 開發人員說明文件,瞭解如何向 Yahoo 註冊網頁應用程式

      請務必選取兩個 OpenID Connect API 權限:profileemail

    2. 向這些供應商註冊應用程式時,請務必將專案的 *.firebaseapp.com 網域註冊為應用程式的重新導向網域。
  5. 按一下「儲存」

使用 Firebase SDK 處理登入流程

如要使用 Firebase Apple 平台 SDK 處理登入流程,請按照下列步驟操作:

  1. 在 Xcode 專案中新增自訂網址架構:

    1. 開啟專案設定:在左側樹狀檢視中按兩下專案名稱。在「目標」部分選取您的應用程式,然後選取「資訊」分頁標籤,並展開「網址類型」部分。
    2. 按一下 + 按鈕,然後將編碼後的應用程式 ID 新增為網址配置。將其他欄位留空。

      您可以在 Firebase 控制台中找到編碼後的應用程式 ID:依序前往「設定」 >「一般」分頁標籤,然後向下捲動至「您的應用程式」部分,即可查看 iOS 應用程式的詳細資料。

      完成後,您的設定應如下所示 (但會使用應用程式專屬值):

      Xcode 的自訂網址配置設定介面螢幕截圖

  2. 使用供應商 ID yahoo.com 建立 OAuthProvider 的執行個體。

    Swift

    var provider = OAuthProvider(providerID: "yahoo.com")
        

    Objective-C

    FIROAuthProvider *provider = [FIROAuthProvider providerWithProviderID:@"yahoo.com"];
        
  3. 選用:指定要隨 OAuth 要求傳送的其他自訂 OAuth 參數。

    Swift

    provider.customParameters = [
    "prompt": "login",
    "language": "fr"
    ]
        

    Objective-C

    [provider setCustomParameters:@{@"prompt": @"login", @"language": @"fr"}];
        

    如要瞭解 Yahoo 支援的參數,請參閱 Yahoo OAuth 說明文件。請注意,您無法使用 setCustomParameters 傳遞 Firebase 必要參數。這些參數包括 client_idredirect_uriresponse_typescopestate

  4. 選用:指定要向驗證供應商要求的 profileemail 以外的其他 OAuth 2.0 範圍。如果您的應用程式需要存取 Yahoo API 的私人使用者資料,請在 Yahoo 開發人員控制台的「API Permissions」(API 權限) 下方,要求 Yahoo API 的權限。要求的 OAuth 範圍必須與應用程式 API 權限中預先設定的範圍完全相符。舉例來說,如果要求讀取/寫入使用者聯絡人資料的權限,且已在應用程式的 API 權限中預先設定,則必須傳遞 sdct-w,而非唯讀 OAuth 範圍 sdct-r。否則流程會失敗,且系統會向使用者顯示錯誤訊息。

    Swift

    // 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 範圍說明文件

  5. 選用:如要自訂應用程式向使用者顯示 reCAPTCHA 時呈現 SFSafariViewControllerUIWebView 的方式,請建立符合 AuthUIDelegate 通訊協定的自訂類別,並將其傳遞至 credentialWithUIDelegate

  6. 使用 OAuth 供應商物件向 Firebase 進行驗證。

    Swift

    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 as? OAuthCredential)?.accessToken
      // Yahoo OAuth ID token can be retrieved by calling:
      // (authResult.credential as? OAuthCredential)?.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:
      // ((FIROAuthCredential *)authResult.credential).accessToken
      // Yahoo OAuth ID token can be retrieved by calling:
      // ((FIROAuthCredential *)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 擷取。

  7. 雖然上述範例著重於登入流程,但您也可以使用 linkWithPopup,將 Yahoo 供應商連結至現有使用者。舉例來說,您可以將多個供應商連結至同一位使用者,讓使用者透過任一供應商登入。

    Swift

    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 as? OAuthCredential)?.accessToken
    // Yahoo OAuth ID token can be retrieved by calling:
    // (authResult.credential as? OAuthCredential)?.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:
    // ((FIROAuthCredential *)authResult.credential).accessToken
    // Yahoo OAuth ID token can be retrieved by calling:
    // ((FIROAuthCredential *)authResult.credential).idToken
    }];
        
  8. reauthenticateWithPopup/reauthenticateWithRedirect 也可使用相同模式,用於擷取需要最近登入的敏感作業的新憑證。

    Swift

    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 as? OAuthCredential)?.accessToken
    // Yahoo OAuth ID token can be retrieved by calling:
    // (authResult.credential as? OAuthCredential)?.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:
    // ((FIROAuthCredential *)authResult.credential).accessToken
    // Yahoo OAuth ID token can be retrieved by calling:
    // ((FIROAuthCredential *)authResult.credential).idToken
    }];
        

後續步驟

使用者首次登入後,系統會建立新的使用者帳戶,並連結至使用者登入時使用的憑證 (即使用者名稱和密碼、電話號碼或驗證供應商資訊)。這個新帳戶會儲存在 Firebase 專案中,可用於識別專案中每個應用程式的使用者,無論使用者登入方式為何。

  • 在應用程式中,您可以從 User 物件取得使用者的基本個人資料資訊。請參閱「管理使用者」。

  • Firebase Realtime DatabaseCloud Storage 安全規則中,您可以從 auth 變數取得已登入使用者的專屬使用者 ID, 並使用該 ID 控制使用者可存取的資料。

您可以將驗證供應商憑證連結至現有使用者帳戶,允許使用者透過多個驗證供應商登入應用程式。

如要登出使用者,請呼叫 signOut:

Swift

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;
}

您也可以新增錯誤處理程式碼,處理各種驗證錯誤。請參閱「處理錯誤」。