Apple प्लैटफ़ॉर्म पर, पुष्टि करने वाली एक से ज़्यादा कंपनियों को एक खाते से लिंक करना

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

वेब कंटेनर इंस्टॉल करने से पहले

अपने ऐप्लिकेशन में पुष्टि करने की सुविधा देने वाली दो या इससे ज़्यादा कंपनियों के लिए सहायता जोड़ें. इसमें पहचान छिपाकर पुष्टि करने की सुविधा भी शामिल हो सकती है.

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

  1. पुष्टि करने वाली किसी भी कंपनी या तरीके का इस्तेमाल करके, उपयोगकर्ता के खाते में साइन इन करें.
  2. पुष्टि करने वाली नई कंपनी के लिए, साइन-इन फ़्लो को पूरा करें. हालांकि, FIRAuth.signInWith के किसी एक तरीके का इस्तेमाल करके ऐसा करना ज़रूरी नहीं है. उदाहरण के लिए, उपयोगकर्ता का Google आईडी टोकन, Facebook का ऐक्सेस टोकन या ईमेल और पासवर्ड पाएं.
  3. पुष्टि करने वाली नई कंपनी के लिए, FIRAuthCredential डाउनलोड करें:

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

    Swift
        user.link(with: credential) { authResult, error in
      // ...
    }
    }
    
    Objective-C
        [[FIRAuth auth].currentUser linkWithCredential:credential
        completion:^(FIRAuthDataResult *result, NSError *_Nullable error) {
      // ...
    }];
    

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

    Swift

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

    Objective-C

    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: को कॉल पूरा होता है, तो उपयोगकर्ता अब किसी भी लिंक की गई पुष्टि करने की सेवा देने वाली कंपनी का इस्तेमाल करके साइन इन कर सकता है और उसी Firebase डेटा को ऐक्सेस कर सकता है.

आपके पास, पुष्टि करने वाली किसी कंपनी के खाते को अनलिंक करने का विकल्प होता है. ऐसा करने पर, उपयोगकर्ता उस कंपनी के खाते में साइन इन नहीं कर पाएगा.

पुष्टि करने वाली कंपनी को उपयोगकर्ता खाते से अनलिंक करने के लिए, प्रोवाइडर आईडी को unlink तरीके से पास करें. providerData प्रॉपर्टी से, उपयोगकर्ता को पुष्टि करने वाली कंपनियों के आईडी मिल सकते हैं.

Swift

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

Objective-C

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