ตรวจสอบสิทธิ์โดยใช้การเข้าสู่ระบบ Facebook บนแพลตฟอร์ม Apple

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

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

ใช้ Swift Package Manager เพื่อติดตั้งและจัดการการพึ่งพา Firebase

  1. ใน Xcode เมื่อโปรเจ็กต์แอปของคุณเปิดอยู่ ให้ไปที่ File > Add Package
  2. เมื่อได้รับแจ้ง ให้เพิ่มพื้นที่เก็บข้อมูล SDK แพลตฟอร์ม Firebase Apple:
  3.   https://github.com/firebase/firebase-ios-sdk.git
  4. เลือกไลบรารี Firebase Authentication
  5. เพิ่มแฟล็ก -ObjC ไปยังส่วน Other Linker Flags ของการตั้งค่า build ของเป้าหมายของคุณ
  6. เมื่อเสร็จแล้ว Xcode จะเริ่มแก้ไขและดาวน์โหลดการอ้างอิงของคุณโดยอัตโนมัติในเบื้องหลัง

จากนั้น ทำตามขั้นตอนการกำหนดค่าบางอย่าง:

  1. บนไซต์ Facebook สำหรับนักพัฒนา ให้รับ ID แอพ และ ความลับของแอพ สำหรับแอพของคุณ
  2. เปิดใช้งานการเข้าสู่ระบบ Facebook:
    1. ใน คอนโซล Firebase ให้เปิดส่วน การตรวจสอบสิทธิ์
    2. บน แท็บวิธีการลงชื่อเข้า ใช้ ให้เปิดใช้งานวิธีการลงชื่อเข้าใช้ ด้วย Facebook แล้วระบุ App ID และ App Secret ที่คุณได้รับจาก Facebook
    3. จากนั้นตรวจสอบให้แน่ใจว่า URI การเปลี่ยนเส้นทาง OAuth ของคุณ (เช่น my-app-12345.firebaseapp.com/__/auth/handler ) อยู่ในรายการเป็นหนึ่งใน URI การเปลี่ยนเส้นทาง OAuth ของคุณในหน้าการตั้งค่าแอพ Facebook ของคุณบนเว็บไซต์ Facebook สำหรับนักพัฒนา ใน ผลิตภัณฑ์ การตั้งค่า > การกำหนดค่าการเข้าสู่ระบบ Facebook

ใช้การเข้าสู่ระบบ Facebook

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

  1. รวมการเข้าสู่ระบบด้วย Facebook เข้ากับแอพของคุณโดยปฏิบัติตาม เอกสารของนักพัฒนา เมื่อคุณเตรียมใช้งานออบเจ็กต์ FBSDKLoginButton ให้ตั้งค่าผู้รับมอบสิทธิ์เพื่อรับเหตุการณ์การเข้าสู่ระบบและออกจากระบบ ตัวอย่างเช่น:

    สวิฟท์

    let loginButton = FBSDKLoginButton()
    loginButton.delegate = self
    

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

    FBSDKLoginButton *loginButton = [[FBSDKLoginButton alloc] init];
    loginButton.delegate = self;
    
    ในผู้รับมอบสิทธิ์ของคุณ ให้ใช้ didCompleteWithResult:error:

    สวิฟท์

    func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
      if let error = error {
        print(error.localizedDescription)
        return
      }
      // ...
    }
    

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

    - (void)loginButton:(FBSDKLoginButton *)loginButton
        didCompleteWithResult:(FBSDKLoginManagerLoginResult *)result
                        error:(NSError *)error {
      if (error == nil) {
        // ...
      } else {
        NSLog(error.localizedDescription);
      }
    }
    
  2. นำเข้าโมดูล FirebaseCore ใน UIApplicationDelegate ของคุณ รวมถึง โมดูล Firebase อื่นๆ ที่แอปของคุณมอบหมายใช้ ตัวอย่างเช่น หากต้องการใช้ Cloud Firestore และการตรวจสอบสิทธิ์ ให้ทำดังนี้

    SwiftUI

    import SwiftUI
    import FirebaseCore
    import FirebaseFirestore
    import FirebaseAuth
    // ...
          

    สวิฟท์

    import FirebaseCore
    import FirebaseFirestore
    import FirebaseAuth
    // ...
          

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

    @import FirebaseCore;
    @import FirebaseFirestore;
    @import FirebaseAuth;
    // ...
          
  3. กำหนดค่าอินสแตนซ์ที่ใช้ร่วมกันของ FirebaseApp ใน application(_:didFinishLaunchingWithOptions:) ของผู้รับมอบสิทธิ์แอปของคุณ:

    SwiftUI

    // Use Firebase library to configure APIs
    FirebaseApp.configure()

    สวิฟท์

    // Use Firebase library to configure APIs
    FirebaseApp.configure()

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

    // Use Firebase library to configure APIs
    [FIRApp configure];
  4. หากคุณใช้ SwiftUI คุณต้องสร้างผู้แทนแอปพลิเคชันและแนบไปกับโครงสร้าง App ของคุณผ่าน UIApplicationDelegateAdaptor หรือ NSApplicationDelegateAdaptor คุณต้องปิดใช้งานการสลับตัวแทนของแอปด้วย สำหรับข้อมูลเพิ่มเติม โปรดดู คำแนะนำ SwiftUI

    SwiftUI

    @main
    struct YourApp: App {
      // register app delegate for Firebase setup
      @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
    
      var body: some Scene {
        WindowGroup {
          NavigationView {
            ContentView()
          }
        }
      }
    }
          
  5. หลังจากที่ผู้ใช้ลงชื่อเข้าใช้สำเร็จแล้ว ในการใช้งาน didCompleteWithResult:error: ให้รับโทเค็นการเข้าถึงสำหรับผู้ใช้ที่ลงชื่อเข้าใช้และแลกเปลี่ยนเป็นข้อมูลรับรอง Firebase:

    สวิฟท์

    let credential = FacebookAuthProvider
      .credential(withAccessToken: AccessToken.current!.tokenString)
    

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

    FIRAuthCredential *credential = [FIRFacebookAuthProvider
        credentialWithAccessToken:[FBSDKAccessToken currentAccessToken].tokenString];
    

ใช้การเข้าสู่ระบบ Facebook Limited

หากต้องการใช้การเข้าสู่ระบบแบบจำกัดด้วย Facebook แทนการเข้าสู่ระบบด้วย Facebook แบบ "คลาสสิก" ให้ทำตามขั้นตอนต่อไปนี้

  1. รวมการเข้าสู่ระบบ Facebook Limited เข้ากับแอพของคุณโดยปฏิบัติตาม เอกสารของนักพัฒนา
  2. สำหรับคำขอลงชื่อเข้าใช้ทุกครั้ง ให้สร้างสตริงสุ่มที่ไม่ซ้ำกัน ซึ่งเรียกว่า "nonce" ซึ่งคุณจะใช้เพื่อให้แน่ใจว่าโทเค็น ID ที่คุณได้รับนั้นได้รับการอนุญาตโดยเฉพาะเพื่อตอบสนองต่อคำขอตรวจสอบสิทธิ์ของแอปของคุณ ขั้นตอนนี้มีความสำคัญในการป้องกันการโจมตีซ้ำ คุณสามารถสร้าง nonce ที่ปลอดภัยด้วยการเข้ารหัสด้วย SecRandomCopyBytes(_:_:_) ดังตัวอย่างต่อไปนี้:

    สวิฟท์

    private func randomNonceString(length: Int = 32) -> String {
      precondition(length > 0)
      var randomBytes = [UInt8](repeating: 0, count: length)
      let errorCode = SecRandomCopyBytes(kSecRandomDefault, randomBytes.count, &randomBytes)
      if errorCode != errSecSuccess {
        fatalError(
          "Unable to generate nonce. SecRandomCopyBytes failed with OSStatus \(errorCode)"
        )
      }
    
      let charset: [Character] =
        Array("0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._")
    
      let nonce = randomBytes.map { byte in
        // Pick a random character from the set, wrapping around if needed.
        charset[Int(byte) % charset.count]
      }
    
      return String(nonce)
    }
    
            

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

    // Adapted from https://auth0.com/docs/api-auth/tutorials/nonce#generate-a-cryptographically-random-nonce
    - (NSString *)randomNonce:(NSInteger)length {
      NSAssert(length > 0, @"Expected nonce to have positive length");
      NSString *characterSet = @"0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._";
      NSMutableString *result = [NSMutableString string];
      NSInteger remainingLength = length;
    
      while (remainingLength > 0) {
        NSMutableArray *randoms = [NSMutableArray arrayWithCapacity:16];
        for (NSInteger i = 0; i < 16; i++) {
          uint8_t random = 0;
          int errorCode = SecRandomCopyBytes(kSecRandomDefault, 1, &random);
          NSAssert(errorCode == errSecSuccess, @"Unable to generate nonce: OSStatus %i", errorCode);
    
          [randoms addObject:@(random)];
        }
    
        for (NSNumber *random in randoms) {
          if (remainingLength == 0) {
            break;
          }
    
          if (random.unsignedIntValue < characterSet.length) {
            unichar character = [characterSet characterAtIndex:random.unsignedIntValue];
            [result appendFormat:@"%C", character];
            remainingLength--;
          }
        }
      }
    
      return [result copy];
    }
            
    คุณจะส่งแฮช SHA-256 ของ nonce ไปพร้อมกับคำขอลงชื่อเข้าใช้ของคุณ ซึ่ง Facebook จะส่งผ่านโดยไม่มีการเปลี่ยนแปลงในการตอบกลับ Firebase ตรวจสอบการตอบสนองโดยการแฮช nonce ดั้งเดิมและเปรียบเทียบกับค่าที่ส่งผ่านโดย Facebook

    สวิฟท์

    @available(iOS 13, *)
    private func sha256(_ input: String) -> String {
      let inputData = Data(input.utf8)
      let hashedData = SHA256.hash(data: inputData)
      let hashString = hashedData.compactMap {
        String(format: "%02x", $0)
      }.joined()
    
      return hashString
    }
    
            

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

    - (NSString *)stringBySha256HashingString:(NSString *)input {
      const char *string = [input UTF8String];
      unsigned char result[CC_SHA256_DIGEST_LENGTH];
      CC_SHA256(string, (CC_LONG)strlen(string), result);
    
      NSMutableString *hashed = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH * 2];
      for (NSInteger i = 0; i < CC_SHA256_DIGEST_LENGTH; i++) {
        [hashed appendFormat:@"%02x", result[i]];
      }
      return hashed;
    }
            
  3. เมื่อคุณตั้งค่า FBSDKLoginButton ให้ตั้งค่าผู้รับมอบสิทธิ์เพื่อรับเหตุการณ์การเข้าสู่ระบบและออกจากระบบ ตั้งค่าโหมดการติดตามเป็น FBSDKLoginTrackingLimited และแนบ nonce ตัวอย่างเช่น:

    สวิฟท์

    func setupLoginButton() {
        let nonce = randomNonceString()
        currentNonce = nonce
        loginButton.delegate = self
        loginButton.loginTracking = .limited
        loginButton.nonce = sha256(nonce)
    }
            

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

    - (void)setupLoginButton {
      NSString *nonce = [self randomNonce:32];
      self.currentNonce = nonce;
      self.loginButton.delegate = self;
      self.loginButton.loginTracking = FBSDKLoginTrackingLimited
      self.loginButton.nonce = [self stringBySha256HashingString:nonce];
    }
            
    ในผู้รับมอบสิทธิ์ของคุณ ให้ใช้ didCompleteWithResult:error:

    สวิฟท์

    func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
      if let error = error {
        print(error.localizedDescription)
        return
      }
      // ...
    }
            

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

    - (void)loginButton:(FBSDKLoginButton *)loginButton
        didCompleteWithResult:(FBSDKLoginManagerLoginResult *)result
                        error:(NSError *)error {
      if (error == nil) {
        // ...
      } else {
        NSLog(error.localizedDescription);
      }
    }
            
  4. นำเข้าโมดูล FirebaseCore ใน UIApplicationDelegate ของคุณ รวมถึง โมดูล Firebase อื่นๆ ที่แอปของคุณมอบหมายใช้ ตัวอย่างเช่น หากต้องการใช้ Cloud Firestore และการตรวจสอบสิทธิ์ ให้ทำดังนี้

    SwiftUI

    import SwiftUI
    import FirebaseCore
    import FirebaseFirestore
    import FirebaseAuth
    // ...
          

    สวิฟท์

    import FirebaseCore
    import FirebaseFirestore
    import FirebaseAuth
    // ...
          

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

    @import FirebaseCore;
    @import FirebaseFirestore;
    @import FirebaseAuth;
    // ...
          
  5. กำหนดค่าอินสแตนซ์ที่ใช้ร่วมกันของ FirebaseApp ใน application(_:didFinishLaunchingWithOptions:) ของผู้รับมอบสิทธิ์แอปของคุณ:

    SwiftUI

    // Use Firebase library to configure APIs
    FirebaseApp.configure()

    สวิฟท์

    // Use Firebase library to configure APIs
    FirebaseApp.configure()

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

    // Use Firebase library to configure APIs
    [FIRApp configure];
  6. หากคุณใช้ SwiftUI คุณต้องสร้างผู้แทนแอปพลิเคชันและแนบไปกับโครงสร้าง App ของคุณผ่าน UIApplicationDelegateAdaptor หรือ NSApplicationDelegateAdaptor คุณต้องปิดใช้งานการสลับตัวแทนของแอปด้วย สำหรับข้อมูลเพิ่มเติม โปรดดู คำแนะนำ SwiftUI

    SwiftUI

    @main
    struct YourApp: App {
      // register app delegate for Firebase setup
      @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
    
      var body: some Scene {
        WindowGroup {
          NavigationView {
            ContentView()
          }
        }
      }
    }
          
  7. หลังจากที่ผู้ใช้ลงชื่อเข้าใช้สำเร็จแล้ว ในการใช้งาน didCompleteWithResult:error: ของคุณ ให้ใช้โทเค็น ID จากการตอบกลับของ Facebook ด้วย nonce ที่ไม่ได้แฮชเพื่อรับข้อมูลรับรอง Firebase:

    สวิฟท์

    // Initialize a Firebase credential.
    let idTokenString = AuthenticationToken.current?.tokenString
    let nonce = currentNonce
    let credential = OAuthProvider.credential(withProviderID: "facebook.com",
                                              idToken: idTokenString!,
                                              rawNonce: nonce)
            

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

    // Initialize a Firebase credential.
    NSString *idTokenString = FBSDKAuthenticationToken.currentAuthenticationToken.tokenString;
    NSString *rawNonce = self.currentNonce;
    FIROAuthCredential *credential = [FIROAuthProvider credentialWithProviderID:@"facebook.com"
                                                                        IDToken:idTokenString
                                                                       rawNonce:rawNonce];
            

ตรวจสอบสิทธิ์กับ Firebase

สุดท้าย ตรวจสอบสิทธิ์กับ Firebase โดยใช้ข้อมูลรับรอง Firebase:

สวิฟท์

Auth.auth().signIn(with: credential) { authResult, error in
    if let error = error {
      let authError = error as NSError
      if isMFAEnabled, authError.code == AuthErrorCode.secondFactorRequired.rawValue {
        // The user is a multi-factor user. Second factor challenge is required.
        let resolver = authError
          .userInfo[AuthErrorUserInfoMultiFactorResolverKey] as! MultiFactorResolver
        var displayNameString = ""
        for tmpFactorInfo in resolver.hints {
          displayNameString += tmpFactorInfo.displayName ?? ""
          displayNameString += " "
        }
        self.showTextInputPrompt(
          withMessage: "Select factor to sign in\n\(displayNameString)",
          completionBlock: { userPressedOK, displayName in
            var selectedHint: PhoneMultiFactorInfo?
            for tmpFactorInfo in resolver.hints {
              if displayName == tmpFactorInfo.displayName {
                selectedHint = tmpFactorInfo as? PhoneMultiFactorInfo
              }
            }
            PhoneAuthProvider.provider()
              .verifyPhoneNumber(with: selectedHint!, uiDelegate: nil,
                                 multiFactorSession: resolver
                                   .session) { verificationID, error in
                if error != nil {
                  print(
                    "Multi factor start sign in failed. Error: \(error.debugDescription)"
                  )
                } else {
                  self.showTextInputPrompt(
                    withMessage: "Verification code for \(selectedHint?.displayName ?? "")",
                    completionBlock: { userPressedOK, verificationCode in
                      let credential: PhoneAuthCredential? = PhoneAuthProvider.provider()
                        .credential(withVerificationID: verificationID!,
                                    verificationCode: verificationCode!)
                      let assertion: MultiFactorAssertion? = PhoneMultiFactorGenerator
                        .assertion(with: credential!)
                      resolver.resolveSignIn(with: assertion!) { authResult, error in
                        if error != nil {
                          print(
                            "Multi factor finanlize sign in failed. Error: \(error.debugDescription)"
                          )
                        } else {
                          self.navigationController?.popViewController(animated: true)
                        }
                      }
                    }
                  )
                }
              }
          }
        )
      } else {
        self.showMessagePrompt(error.localizedDescription)
        return
      }
      // ...
      return
    }
    // User is signed in
    // ...
}
    

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

[[FIRAuth auth] signInWithCredential:credential
                          completion:^(FIRAuthDataResult * _Nullable authResult,
                                       NSError * _Nullable error) {
    if (isMFAEnabled && error && error.code == FIRAuthErrorCodeSecondFactorRequired) {
      FIRMultiFactorResolver *resolver = error.userInfo[FIRAuthErrorUserInfoMultiFactorResolverKey];
      NSMutableString *displayNameString = [NSMutableString string];
      for (FIRMultiFactorInfo *tmpFactorInfo in resolver.hints) {
        [displayNameString appendString:tmpFactorInfo.displayName];
        [displayNameString appendString:@" "];
      }
      [self showTextInputPromptWithMessage:[NSString stringWithFormat:@"Select factor to sign in\n%@", displayNameString]
                           completionBlock:^(BOOL userPressedOK, NSString *_Nullable displayName) {
       FIRPhoneMultiFactorInfo* selectedHint;
       for (FIRMultiFactorInfo *tmpFactorInfo in resolver.hints) {
         if ([displayName isEqualToString:tmpFactorInfo.displayName]) {
           selectedHint = (FIRPhoneMultiFactorInfo *)tmpFactorInfo;
         }
       }
       [FIRPhoneAuthProvider.provider
        verifyPhoneNumberWithMultiFactorInfo:selectedHint
        UIDelegate:nil
        multiFactorSession:resolver.session
        completion:^(NSString * _Nullable verificationID, NSError * _Nullable error) {
          if (error) {
            [self showMessagePrompt:error.localizedDescription];
          } else {
            [self showTextInputPromptWithMessage:[NSString stringWithFormat:@"Verification code for %@", selectedHint.displayName]
                                 completionBlock:^(BOOL userPressedOK, NSString *_Nullable verificationCode) {
             FIRPhoneAuthCredential *credential =
                 [[FIRPhoneAuthProvider provider] credentialWithVerificationID:verificationID
                                                              verificationCode:verificationCode];
             FIRMultiFactorAssertion *assertion = [FIRPhoneMultiFactorGenerator assertionWithCredential:credential];
             [resolver resolveSignInWithAssertion:assertion completion:^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) {
               if (error) {
                 [self showMessagePrompt:error.localizedDescription];
               } else {
                 NSLog(@"Multi factor finanlize sign in succeeded.");
               }
             }];
           }];
          }
        }];
     }];
    }
  else if (error) {
    // ...
    return;
  }
  // User successfully signed in. Get user data from the FIRUser object
  if (authResult == nil) { return; }
  FIRUser *user = authResult.user;
  // ...
}];
    

ขั้นตอนถัดไป

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

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