ตรวจสอบสิทธิ์โดยใช้ Yahoo บนแพลตฟอร์ม Apple

คุณสามารถอนุญาตให้ผู้ใช้ตรวจสอบสิทธิ์กับ Firebase โดยใช้ผู้ให้บริการ OAuth เช่น Yahoo โดยการผสานรวมการเข้าสู่ระบบ OAuth ทั่วไปเข้ากับแอปของคุณโดยใช้ Firebase SDK เพื่อดำเนินการขั้นตอนการลงชื่อเข้าใช้ตั้งแต่ต้นจนจบ

ก่อนที่คุณจะเริ่ม

หากต้องการลงชื่อเข้าใช้ผู้ใช้โดยใช้บัญชี Yahoo คุณต้องเปิดใช้งาน Yahoo เป็นผู้ให้บริการลงชื่อเข้าใช้สำหรับโครงการ Firebase ของคุณก่อน:

  1. เพิ่ม Firebase ให้กับโปรเจ็กต์ Apple ของคุณ
  2. ใน คอนโซล Firebase ให้เปิดส่วน การตรวจสอบสิทธิ์
  3. บนแท็บ วิธีการลงชื่อเข้า ใช้ ให้เปิดใช้งานผู้ให้บริการ Yahoo
  4. เพิ่ม รหัสไคลเอ็นต์ และ รหัสลับไคลเอ็นต์ จากคอนโซลนักพัฒนาซอฟต์แวร์ของผู้ให้บริการรายนั้นลงในการกำหนดค่าผู้ให้บริการ:
    1. หากต้องการลงทะเบียนไคลเอ็นต์ Yahoo OAuth ให้ปฏิบัติตามเอกสารสำหรับนักพัฒนา Yahoo ใน การลงทะเบียนเว็บแอปพลิเคชันกับ Yahoo

      อย่าลืมเลือกสิทธิ์ OpenID Connect API สองรายการ: profile และ email

    2. เมื่อลงทะเบียนแอพกับผู้ให้บริการเหล่านี้ อย่าลืมลงทะเบียนโดเมน *.firebaseapp.com สำหรับโปรเจ็กต์ของคุณเป็นโดเมนการเปลี่ยนเส้นทางสำหรับแอพของคุณ
  5. คลิก บันทึก

จัดการขั้นตอนการลงชื่อเข้าใช้ด้วย Firebase SDK

หากต้องการจัดการขั้นตอนการลงชื่อเข้าใช้ด้วย SDK แพลตฟอร์ม Firebase Apple ให้ทำตามขั้นตอนเหล่านี้:

  1. เพิ่มโครงร่าง URL ที่กำหนดเองให้กับโปรเจ็กต์ Xcode ของคุณ:

    1. เปิดการกำหนดค่าโครงการของคุณ: คลิกสองครั้งที่ชื่อโครงการในมุมมองต้นไม้ด้านซ้าย เลือกแอปของคุณจากส่วน เป้าหมาย จากนั้นเลือกแท็บ ข้อมูล และขยายส่วน ประเภท URL
    2. คลิกปุ่ม + และเพิ่ม ID แอปที่เข้ารหัสของคุณเป็นรูปแบบ URL คุณสามารถค้นหา ID ของแอปที่เข้ารหัสได้ในหน้า การตั้งค่าทั่วไป ของคอนโซล Firebase ในส่วนสำหรับแอป iOS ของคุณ ปล่อยให้ช่องอื่นๆ ว่างไว้

      เมื่อเสร็จแล้ว การกำหนดค่าของคุณควรมีลักษณะคล้ายกับตัวอย่างต่อไปนี้ (แต่ด้วยค่าเฉพาะแอปพลิเคชันของคุณ):

      ภาพหน้าจอของอินเทอร์เฟซการตั้งค่ารูปแบบ URL ที่กำหนดเองของ Xcode

  2. สร้างอินสแตนซ์ของ OAuthProvider โดยใช้ ID ผู้ให้บริการ yahoo.com

    สวิฟท์

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

    วัตถุประสงค์-C

    FIROAuthProvider *provider = [FIROAuthProvider providerWithProviderID:@"yahoo.com"];
        
  3. ทางเลือก : ระบุพารามิเตอร์ OAuth ที่กำหนดเองเพิ่มเติมที่คุณต้องการส่งพร้อมกับคำขอ OAuth

    สวิฟท์

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

    วัตถุประสงค์-C

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

    สำหรับพารามิเตอร์ที่ Yahoo รองรับ โปรดดู เอกสาร Yahoo OAuth โปรดทราบว่าคุณไม่สามารถส่งพารามิเตอร์ที่ต้องใช้ Firebase ด้วย setCustomParameters พารามิเตอร์เหล่านี้คือ client_id , เปลี่ยนเส้นทาง_uri , response_type , ขอบเขต และ state

  4. ทางเลือก : ระบุขอบเขต OAuth 2.0 เพิ่มเติมนอกเหนือจาก profile และ email ที่คุณต้องการขอจากผู้ให้บริการการตรวจสอบสิทธิ์ หากแอปพลิเคชันของคุณต้องการการเข้าถึงข้อมูลส่วนตัวผู้ใช้จาก Yahoo API คุณจะต้องขอสิทธิ์ไปยัง Yahoo API ภายใต้ สิทธิ์ของ API ในคอนโซลนักพัฒนาของ Yahoo ขอบเขต 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"]
        

    วัตถุประสงค์-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. ตัวเลือกเสริม : หากคุณต้องการปรับแต่งวิธีที่แอปนำเสนอ SFSafariViewController หรือ UIWebView เมื่อแสดง reCAPTCHA ให้กับผู้ใช้ ให้สร้างคลาสแบบกำหนดเองที่สอดคล้องกับโปรโตคอล AuthUIDelegate แล้วส่งต่อไปยัง credentialWithUIDelegate

  6. ตรวจสอบสิทธิ์กับ Firebase โดยใช้วัตถุผู้ให้บริการ OAuth

    สวิฟท์

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

    วัตถุประสงค์-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 คือ ID ผู้ใช้ Yahoo ซึ่งสามารถดึงข้อมูลจากฟิลด์ Auth.auth.currentUser.providerData[0].uid หรือจาก authResult.additionalUserInfo.profile

  7. แม้ว่าตัวอย่างข้างต้นจะเน้นไปที่ขั้นตอนการลงชื่อเข้าใช้ แต่คุณยังมีความสามารถในการเชื่อมโยงผู้ให้บริการ Yahoo กับผู้ใช้ที่มีอยู่โดยใช้ linkWithPopup ตัวอย่างเช่น คุณสามารถเชื่อมโยงผู้ให้บริการหลายรายกับผู้ใช้รายเดียวกันได้ โดยอนุญาตให้ผู้ให้บริการรายใดรายหนึ่งลงชื่อเข้าใช้ด้วยได้

    สวิฟท์

    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
    }
        

    วัตถุประสงค์-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 ซึ่งสามารถใช้เพื่อดึงข้อมูลรับรองใหม่สำหรับการดำเนินการที่มีความละเอียดอ่อนซึ่งจำเป็นต้องเข้าสู่ระบบล่าสุด

    สวิฟท์

    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
    }
        

    วัตถุประสงค์-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 และ กฎความปลอดภัยของ พื้นที่เก็บข้อมูลบนคลาวด์ คุณสามารถรับ ID ผู้ใช้เฉพาะของผู้ใช้ที่ลงชื่อเข้าใช้จากตัวแปร auth และใช้เพื่อควบคุมข้อมูลที่ผู้ใช้สามารถเข้าถึงได้

คุณสามารถอนุญาตให้ผู้ใช้ลงชื่อเข้าใช้แอปของคุณโดยใช้ผู้ให้บริการตรวจสอบสิทธิ์หลายรายได้โดย การเชื่อมโยงข้อมูลประจำตัวของผู้ให้บริการตรวจสอบสิทธิ์กับบัญชีผู้ใช้ที่มีอยู่

หากต้องการออกจากระบบผู้ใช้ ให้โทร signOut:

สวิฟท์

let firebaseAuth = Auth.auth()
do {
  try firebaseAuth.signOut()
} catch let signOutError as NSError {
  print("Error signing out: %@", signOutError)
}

วัตถุประสงค์-C

NSError *signOutError;
BOOL status = [[FIRAuth auth] signOut:&signOutError];
if (!status) {
  NSLog(@"Error signing out: %@", signOutError);
  return;
}

คุณอาจต้องการเพิ่มรหัสการจัดการข้อผิดพลาดสำหรับข้อผิดพลาดในการรับรองความถูกต้องทั้งหมด ดู การจัดการข้อผิดพลาด