Apple प्लेटफ़ॉर्म पर एकाधिक प्रामाणिक प्रदाताओं को एक खाते से लिंक करें

आप किसी मौजूदा उपयोगकर्ता खाते से प्रमाणीकरण प्रदाता क्रेडेंशियल्स को लिंक करके उपयोगकर्ताओं को एकाधिक प्रमाणीकरण प्रदाताओं का उपयोग करके अपने ऐप में साइन इन करने की अनुमति दे सकते हैं। उपयोगकर्ताओं को उसी फायरबेस उपयोगकर्ता आईडी से पहचाना जा सकता है, चाहे वे साइन इन करने के लिए किसी भी प्रमाणीकरण प्रदाता का उपयोग करें। उदाहरण के लिए, पासवर्ड से साइन इन करने वाला उपयोगकर्ता Google खाता लिंक कर सकता है और भविष्य में किसी भी विधि से साइन इन कर सकता है। या, कोई अनाम उपयोगकर्ता फेसबुक अकाउंट लिंक कर सकता है और फिर, बाद में, आपके ऐप का उपयोग जारी रखने के लिए फेसबुक से साइन इन कर सकता है।

शुरू करने से पहले

अपने ऐप में दो या अधिक प्रमाणीकरण प्रदाताओं (संभवतः अनाम प्रमाणीकरण सहित) के लिए समर्थन जोड़ें।

प्रमाणीकरण प्रदाता क्रेडेंशियल को किसी मौजूदा उपयोगकर्ता खाते से लिंक करने के लिए:

  1. किसी भी प्रमाणीकरण प्रदाता या विधि का उपयोग करके उपयोगकर्ता में साइन इन करें।
  2. नए प्रमाणीकरण प्रदाता के लिए साइन-इन प्रवाह को FIRAuth.signInWith विधियों में से किसी एक को कॉल करने तक पूरा करें, लेकिन इसमें शामिल नहीं है। उदाहरण के लिए, उपयोगकर्ता का Google ID टोकन, Facebook एक्सेस टोकन, या ईमेल और पासवर्ड प्राप्त करें।
  3. नए प्रमाणीकरण प्रदाता के लिए FIRAuthCredential प्राप्त करें:

    गूगल साइन-इन
    तीव्र
    guard
      let authentication = user?.authentication,
      let idToken = authentication.idToken
    else {
      return
    }
    
    let credential = GoogleAuthProvider.credential(withIDToken: idToken,
                                                   accessToken: authentication.accessToken)
    
    उद्देश्य सी
    FIRAuthCredential *credential =
    [FIRGoogleAuthProvider credentialWithIDToken:result.user.idToken.tokenString
                                     accessToken:result.user.accessToken.tokenString];
    
    फेसबुक लॉग इन
    तीव्र
    let credential = FacebookAuthProvider
      .credential(withAccessToken: AccessToken.current!.tokenString)
    
    उद्देश्य सी
    FIRAuthCredential *credential = [FIRFacebookAuthProvider
        credentialWithAccessToken:[FBSDKAccessToken currentAccessToken].tokenString];
    
    ईमेल-पासवर्ड साइन-इन
    तीव्र
    let credential = EmailAuthProvider.credential(withEmail: email, password: password)
    
    उद्देश्य सी
    FIRAuthCredential *credential =
        [FIREmailAuthProvider credentialWithEmail:email
                                                 password:password];
    
  4. FIRAuthCredential ऑब्जेक्ट को साइन-इन किए गए उपयोगकर्ता के linkWithCredential:completion: विधि से पास करें:

    तीव्र
        user.link(with: credential) { authResult, error in
      // ...
    }
    }
    
    उद्देश्य सी
        [[FIRAuth auth].currentUser linkWithCredential:credential
        completion:^(FIRAuthDataResult *result, NSError *_Nullable error) {
      // ...
    }];
    

    यदि क्रेडेंशियल पहले से ही किसी अन्य उपयोगकर्ता खाते से लिंक हैं तो linkWithCredential:completion: पर कॉल विफल हो जाएगी। इस स्थिति में, आपको अपने ऐप के लिए उपयुक्त खातों और संबंधित डेटा को मर्ज करना होगा:

    तीव्र

    let prevUser = Auth.auth().currentUser
    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
        // ...
    }
                // Merge prevUser and currentUser accounts and data
                // ...
            }
    

    उद्देश्य सी

    FIRUser *prevUser = [FIRAuth auth].currentUser;
    [[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;
      // ...
    }];
                                        // Merge prevUser and currentUser accounts and data
                                        // ...
                                    }];
    

यदि linkWithCredential:completion: पर कॉल सफल होती है, तो उपयोगकर्ता अब किसी भी लिंक किए गए प्रमाणीकरण प्रदाता का उपयोग करके साइन इन कर सकता है और उसी फायरबेस डेटा तक पहुंच सकता है।

आप किसी प्रमाणीकरण प्रदाता को किसी खाते से अनलिंक कर सकते हैं, ताकि उपयोगकर्ता अब उस प्रदाता के साथ साइन इन न कर सके।

किसी उपयोगकर्ता खाते से प्रमाणीकरण प्रदाता को अनलिंक करने के लिए, प्रदाता आईडी को unlink विधि में पास करें। आप providerData प्रॉपर्टी से किसी उपयोगकर्ता से जुड़े प्रामाणिक प्रदाताओं की प्रदाता आईडी प्राप्त कर सकते हैं।

तीव्र

Auth.auth().currentUser?.unlink(fromProvider: providerID!) { user, error in
  // ...
}

उद्देश्य सी

[[FIRAuth auth].currentUser unlinkFromProvider:providerID
                                    completion:^(FIRUser *_Nullable user, NSError *_Nullable error) {
  // ...
}];