ตรวจสอบสิทธิ์โดยใช้ Apple

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

ก่อนเริ่มต้น

หากต้องการให้ผู้ใช้ลงชื่อเข้าใช้โดยใช้ Apple ก่อนอื่นให้กำหนดค่าการลงชื่อเข้าใช้ด้วย Apple ในเว็บไซต์สำหรับนักพัฒนาแอปของ Apple แล้วเปิดใช้ Apple เป็นผู้ให้บริการลงชื่อเข้าใช้สำหรับโปรเจ็กต์ Firebase

เข้าร่วมโปรแกรม Developer Program ของ Apple

สมาชิกโปรแกรมนักพัฒนาแอปของ Apple เท่านั้นที่กำหนดค่าการลงชื่อเข้าใช้ด้วย Apple ได้

กำหนดค่าการลงชื่อเข้าใช้ด้วย Apple

  1. เปิดใช้การลงชื่อเข้าใช้ด้วย Apple สำหรับแอปในหน้า ใบรับรอง ตัวระบุ และโปรไฟล์ ของเว็บไซต์สำหรับนักพัฒนาแอปของ Apple
  2. เชื่อมโยงเว็บไซต์กับแอปตามที่อธิบายไว้ในส่วนแรก ของ กำหนดค่าการลงชื่อเข้าใช้ด้วย Apple สำหรับเว็บ เมื่อได้รับข้อความแจ้ง ให้ลงทะเบียน URL ต่อไปนี้เป็น URL สำหรับส่งคืน
    https://YOUR_FIREBASE_PROJECT_ID.firebaseapp.com/__/auth/handler
    คุณดูรหัสโปรเจ็กต์ Firebase ได้ในหน้าการตั้งค่าคอนโซลFirebase เมื่อเสร็จแล้ว ให้จดรหัสบริการใหม่ ซึ่งคุณจะต้องใช้ในส่วนถัดไป
  3. สร้าง คีย์ส่วนตัวสำหรับการลงชื่อเข้าใช้ด้วย Apple คุณจะต้องใช้คีย์ส่วนตัวใหม่และรหัสคีย์ในส่วนถัดไป
  4. หากคุณใช้ฟีเจอร์ใดๆ ของ Firebase Authentication ที่ส่งอีเมลถึงผู้ใช้ ซึ่งรวมถึงการลงชื่อเข้าใช้ด้วยลิงก์อีเมล การยืนยันอีเมล การเพิกถอนการเปลี่ยนแปลงบัญชี และอื่นๆ กำหนดค่าบริการส่งต่ออีเมลส่วนตัวของ Apple และจดทะเบียน noreply@YOUR_FIREBASE_PROJECT_ID.firebaseapp.com (หรือโดเมนเทมเพลตอีเมลที่กำหนดเอง) เพื่อให้ Apple ส่งต่ออีเมลที่ส่งโดย Firebase Authentication ไปยังอีเมล Apple ที่ไม่ระบุชื่อได้

เปิดใช้ Apple เป็นผู้ให้บริการลงชื่อเข้าใช้

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

ปฏิบัติตามข้อกำหนดด้านข้อมูลที่ไม่ระบุชื่อของ Apple

การลงชื่อเข้าใช้ด้วย Apple ช่วยให้ผู้ใช้มีตัวเลือกในการลบข้อมูลระบุตัวบุคคลของตน ซึ่งรวมถึงอีเมลเมื่อลงชื่อเข้าใช้ ผู้ใช้ที่เลือกตัวเลือกนี้จะมีอีเมลที่มีโดเมน privaterelay.appleid.com เมื่อคุณใช้การลงชื่อเข้าใช้ด้วย Apple ในแอป คุณต้องปฏิบัติตามนโยบายหรือข้อกำหนดสำหรับนักพัฒนาแอปที่เกี่ยวข้องจาก Apple เกี่ยวกับ Apple ID ที่ไม่ระบุชื่อเหล่านี้

ซึ่งรวมถึงการขอความยินยอมจากผู้ใช้ที่จำเป็นก่อนที่จะเชื่อมโยงข้อมูลส่วนบุคคลที่ระบุตัวตนได้โดยตรงกับ Apple ID ที่ไม่ระบุชื่อ เมื่อใช้การตรวจสอบสิทธิ์ Firebase การดำเนินการนี้อาจรวมถึงการดำเนินการต่อไปนี้

  • ลิงก์ที่อยู่อีเมลกับ Apple ID ที่ไม่ระบุชื่อหรือในทางกลับกัน
  • ลิงก์หมายเลขโทรศัพท์กับ Apple ID ที่ไม่ระบุชื่อหรือในทางกลับกัน
  • ลิงก์ข้อมูลเข้าสู่ระบบโซเชียลที่ไม่ระบุชื่อ (Facebook, Google ฯลฯ) กับ Apple ID ที่ไม่ระบุชื่อหรือในทางกลับกัน

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

ลงชื่อเข้าใช้ด้วย Apple และตรวจสอบสิทธิ์ด้วย Firebase

หากต้องการตรวจสอบสิทธิ์ด้วยบัญชี Apple ก่อนอื่นให้ลงชื่อเข้าใช้บัญชี Apple ของผู้ใช้โดยใช้เฟรมเวิร์ก AuthenticationServices ของ Apple, แล้วใช้โทเค็นรหัสจากการตอบกลับของ Apple เพื่อสร้างออบเจ็กต์ AuthCredential ของ Firebase โดยทำดังนี้

  1. สร้างสตริงแบบสุ่ม ซึ่งก็คือ "Nonce" สำหรับคำขอลงชื่อเข้าใช้ทุกรายการ ซึ่งคุณจะใช้เพื่อให้แน่ใจว่าโทเค็นรหัสที่คุณได้รับนั้นได้รับอนุญาตเป็นการตอบกลับคำขอการตรวจสอบสิทธิ์ของแอปโดยเฉพาะ ขั้นตอนนี้มีความสำคัญในการป้องกันการโจมตีแบบ Replay

    คุณสามารถสร้าง Nonce ที่ปลอดภัยด้วยการเข้ารหัสลับด้วย SecRandomCopyBytes(_:_:_) ดังตัวอย่างต่อไปนี้

    Swift

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

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

    คุณจะส่งแฮช SHA256 ของ Nonce พร้อมกับคำขอลงชื่อเข้าใช้ ซึ่ง Apple จะส่งต่อในการตอบกลับโดยไม่มีการเปลี่ยนแปลง Firebase จะตรวจสอบการตอบกลับโดยการแฮช Nonce เดิมและเปรียบเทียบกับค่าที่ Apple ส่งมา

    Swift

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

    Objective-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;
    }
        
  2. เริ่มโฟลว์การลงชื่อเข้าใช้ของ Apple โดยใส่แฮช SHA256 ของ Nonce และคลาสผู้รับมอบสิทธิ์ที่จะจัดการการตอบกลับของ Apple (ดูขั้นตอนถัดไป) ไว้ในคำขอ

    Swift

    import CryptoKit
    
    // Unhashed nonce.
    fileprivate var currentNonce: String?
    
    @available(iOS 13, *)
    func startSignInWithAppleFlow() {
      let nonce = randomNonceString()
      currentNonce = nonce
      let appleIDProvider = ASAuthorizationAppleIDProvider()
      let request = appleIDProvider.createRequest()
      request.requestedScopes = [.fullName, .email]
      request.nonce = sha256(nonce)
    
      let authorizationController = ASAuthorizationController(authorizationRequests: [request])
      authorizationController.delegate = self
      authorizationController.presentationContextProvider = self
      authorizationController.performRequests()
    }
    

    Objective-C

    @import CommonCrypto;
    
    - (void)startSignInWithAppleFlow {
      NSString *nonce = [self randomNonce:32];
      self.currentNonce = nonce;
      ASAuthorizationAppleIDProvider *appleIDProvider = [[ASAuthorizationAppleIDProvider alloc] init];
      ASAuthorizationAppleIDRequest *request = [appleIDProvider createRequest];
      request.requestedScopes = @[ASAuthorizationScopeFullName, ASAuthorizationScopeEmail];
      request.nonce = [self stringBySha256HashingString:nonce];
    
      ASAuthorizationController *authorizationController =
          [[ASAuthorizationController alloc] initWithAuthorizationRequests:@[request]];
      authorizationController.delegate = self;
      authorizationController.presentationContextProvider = self;
      [authorizationController performRequests];
    }
    
  3. จัดการการตอบกลับของ Apple ในการใช้งาน ASAuthorizationControllerDelegate หากการลงชื่อเข้าใช้สำเร็จ ให้ใช้โทเค็นรหัสจากการตอบกลับของ Apple พร้อมกับ Nonce ที่ไม่ได้แฮชเพื่อตรวจสอบสิทธิ์ด้วย Firebase โดยทำดังนี้

    Swift

    @available(iOS 13.0, *)
    extension MainViewController: ASAuthorizationControllerDelegate {
    
      func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
        if let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential {
          guard let nonce = currentNonce else {
            fatalError("Invalid state: A login callback was received, but no login request was sent.")
          }
          guard let appleIDToken = appleIDCredential.identityToken else {
            print("Unable to fetch identity token")
            return
          }
          guard let idTokenString = String(data: appleIDToken, encoding: .utf8) else {
            print("Unable to serialize token string from data: \(appleIDToken.debugDescription)")
            return
          }
          // Initialize a Firebase credential, including the user's full name.
          let credential = OAuthProvider.appleCredential(withIDToken: idTokenString,
                                                            rawNonce: nonce,
                                                            fullName: appleIDCredential.fullName)
          // Sign in with Firebase.
          Auth.auth().signIn(with: credential) { (authResult, error) in
            if error {
              // Error. If error.code == .MissingOrInvalidNonce, make sure
              // you're sending the SHA256-hashed nonce as a hex string with
              // your request to Apple.
              print(error.localizedDescription)
              return
            }
            // User is signed in to Firebase with Apple.
            // ...
          }
        }
      }
    
      func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
        // Handle error.
        print("Sign in with Apple errored: \(error)")
      }
    
    }
    

    Objective-C

    - (void)authorizationController:(ASAuthorizationController *)controller
       didCompleteWithAuthorization:(ASAuthorization *)authorization API_AVAILABLE(ios(13.0)) {
      if ([authorization.credential isKindOfClass:[ASAuthorizationAppleIDCredential class]]) {
        ASAuthorizationAppleIDCredential *appleIDCredential = authorization.credential;
        NSString *rawNonce = self.currentNonce;
        NSAssert(rawNonce != nil, @"Invalid state: A login callback was received, but no login request was sent.");
    
        if (appleIDCredential.identityToken == nil) {
          NSLog(@"Unable to fetch identity token.");
          return;
        }
    
        NSString *idToken = [[NSString alloc] initWithData:appleIDCredential.identityToken
                                                  encoding:NSUTF8StringEncoding];
        if (idToken == nil) {
          NSLog(@"Unable to serialize id token from data: %@", appleIDCredential.identityToken);
        }
    
        // Initialize a Firebase credential, including the user's full name.
        FIROAuthCredential *credential = [FIROAuthProvider appleCredentialWithIDToken:IDToken
                                                                             rawNonce:self.appleRawNonce
                                                                             fullName:appleIDCredential.fullName];
    
        // Sign in with Firebase.
        [[FIRAuth auth] signInWithCredential:credential
                                  completion:^(FIRAuthDataResult * _Nullable authResult,
                                               NSError * _Nullable error) {
          if (error != nil) {
            // Error. If error.code == FIRAuthErrorCodeMissingOrInvalidNonce,
            // make sure you're sending the SHA256-hashed nonce as a hex string
            // with your request to Apple.
            return;
          }
          // Sign-in succeeded!
        }];
      }
    }
    
    - (void)authorizationController:(ASAuthorizationController *)controller
               didCompleteWithError:(NSError *)error API_AVAILABLE(ios(13.0)) {
      NSLog(@"Sign in with Apple errored: %@", error);
    }
    

Apple ไม่ได้ให้ URL รูปภาพ ซึ่งแตกต่างจากผู้ให้บริการรายอื่นๆ ที่ Firebase Auth รองรับ

นอกจากนี้ เมื่อผู้ใช้เลือกที่จะไม่แชร์อีเมลกับแอป Apple จะจัดเตรียมอีเมลที่ไม่ซ้ำกันสำหรับผู้ใช้รายนั้น (ในรูปแบบ xyz@privaterelay.appleid.com) ซึ่งจะแชร์กับแอปของคุณ หากคุณกำหนดค่าบริการส่งต่ออีเมลส่วนตัว Apple จะส่งต่ออีเมลที่ส่งไปยังอีเมลที่ไม่ระบุชื่อไปยังอีเมลจริงของผู้ใช้

การตรวจสอบสิทธิ์อีกครั้งและการลิงก์บัญชี

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

Swift

// Initialize a fresh Apple credential with Firebase.
let credential = OAuthProvider.credential(
  withProviderID: "apple.com",
  IDToken: appleIdToken,
  rawNonce: rawNonce
)
// Reauthenticate current Apple user with fresh Apple credential.
Auth.auth().currentUser.reauthenticate(with: credential) { (authResult, error) in
  guard error != nil else { return }
  // Apple user successfully re-authenticated.
  // ...
}

Objective-C

FIRAuthCredential *credential = [FIROAuthProvider credentialWithProviderID:@"apple.com",
                                                                   IDToken:appleIdToken,
                                                                  rawNonce:rawNonce];
[[FIRAuth auth].currentUser
    reauthenticateWithCredential:credential
                      completion:^(FIRAuthDataResult * _Nullable authResult,
                                   NSError * _Nullable error) {
  if (error) {
    // Handle error.
  }
  // Apple user successfully re-authenticated.
  // ...
}];

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

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

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

ตัวอย่างเช่น หากต้องการลิงก์บัญชี Facebook กับบัญชี Firebase ปัจจุบัน ให้ใช้โทเค็นเพื่อการเข้าถึงที่คุณได้รับจากการลงชื่อเข้าใช้ Facebook ของผู้ใช้

Swift

// Initialize a Facebook credential with Firebase.
let credential = FacebookAuthProvider.credential(
  withAccessToken: AccessToken.current!.tokenString
)
// Assuming the current user is an Apple user linking a Facebook provider.
Auth.auth().currentUser.link(with: credential) { (authResult, error) in
  // Facebook credential is linked to the current Apple user.
  // The user can now sign in with Facebook or Apple to the same Firebase
  // account.
  // ...
}

Objective-C

// Initialize a Facebook credential with Firebase.
FacebookAuthCredential *credential = [FIRFacebookAuthProvider credentialWithAccessToken:accessToken];
// Assuming the current user is an Apple user linking a Facebook provider.
[FIRAuth.auth linkWithCredential:credential completion:^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) {
  // Facebook credential is linked to the current Apple user.
  // The user can now sign in with Facebook or Apple to the same Firebase
  // account.
  // ...
}];

การเพิกถอนโทเค็น

Apple กำหนดให้แอปที่รองรับการสร้างบัญชีต้องอนุญาตให้ผู้ใช้เริ่ม การลบบัญชีของตนเองภายในแอปตามที่อธิบายไว้ใน หลักเกณฑ์การตรวจสอบ App Store

หากต้องการเป็นไปตามข้อกำหนดนี้ ให้ทำตามขั้นตอนต่อไปนี้

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

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

    Swift

    private var currentNonce: String?
    
    private func deleteCurrentUser() {
      do {
        let nonce = try CryptoUtils.randomNonceString()
        currentNonce = nonce
        let appleIDProvider = ASAuthorizationAppleIDProvider()
        let request = appleIDProvider.createRequest()
        request.requestedScopes = [.fullName, .email]
        request.nonce = CryptoUtils.sha256(nonce)
    
        let authorizationController = ASAuthorizationController(authorizationRequests: [request])
        authorizationController.delegate = self
        authorizationController.presentationContextProvider = self
        authorizationController.performRequests()
      } catch {
        // In the unlikely case that nonce generation fails, show error view.
        displayError(error)
      }
    }
  3. รับรหัสการให้สิทธิ์จาก ASAuthorizationAppleIDCredential แล้วใช้รหัสดังกล่าวเพื่อเรียก Auth.auth().revokeToken(withAuthorizationCode:) เพื่อเพิกถอนโทเค็นของผู้ใช้

    Swift

    private var user: User?
    
    func authorizationController(controller: ASAuthorizationController,
                                 didCompleteWithAuthorization authorization: ASAuthorization) {
      guard let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential
      else {
        print("Unable to retrieve AppleIDCredential")
        return
      }
    
      guard let _ = currentNonce else {
        fatalError("Invalid state: A login callback was received, but no login request was sent.")
      }
    
      guard let appleAuthCode = appleIDCredential.authorizationCode else {
        print("Unable to fetch authorization code")
        return
      }
    
      guard let authCodeString = String(data: appleAuthCode, encoding: .utf8) else {
        print("Unable to serialize auth code string from data: \(appleAuthCode.debugDescription)")
        return
      }
    
      Task {
        do {
          try await Auth.auth().revokeToken(withAuthorizationCode: authCodeString)
          try await user?.delete()
          self.updateUI()
        } catch {
          self.displayError(error)
        }
      }
    }
  4. สุดท้าย ให้ลบบัญชีผู้ใช้ (และข้อมูลทั้งหมดที่เชื่อมโยง)

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

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

  • ในแอป คุณสามารถรับข้อมูลโปรไฟล์พื้นฐานของผู้ใช้ได้จากออบเจ็กต์ User ดูจัดการผู้ใช้

  • ใน Firebase Realtime Database และ Cloud Storage กฎความปลอดภัย คุณสามารถ รับรหัสผู้ใช้ที่ไม่ซ้ำกันของผู้ใช้ที่ลงชื่อเข้าใช้ได้จากตัวแปร auth และใช้รหัสดังกล่าวเพื่อควบคุมข้อมูลที่ผู้ใช้เข้าถึงได้

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

หากต้องการออกจากระบบของผู้ใช้ ให้เรียก 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;
}

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