आपके पास उपयोगकर्ताओं को अपने ऐप्लिकेशन में साइन इन करने की अनुमति देने का विकल्प होता है. इसके लिए, वे पुष्टि करने की सुविधा देने वाली कई कंपनियों का इस्तेमाल कर सकते हैं. इसके लिए, पुष्टि करने की सुविधा देने वाली कंपनी के क्रेडेंशियल को किसी मौजूदा उपयोगकर्ता खाते से लिंक करना होगा. उपयोगकर्ताओं की पहचान, एक ही Firebase यूज़र आईडी से की जाती है. भले ही, उन्होंने साइन इन करने के लिए, पुष्टि करने वाली किसी भी सेवा का इस्तेमाल किया हो. उदाहरण के लिए, पासवर्ड से साइन इन करने वाला उपयोगकर्ता, Google खाते को लिंक कर सकता है. इसके बाद, वह आने वाले समय में दोनों में से किसी भी तरीके से साइन इन कर सकता है. इसके अलावा, कोई अज्ञात उपयोगकर्ता अपने Facebook खाते को लिंक कर सकता है. इसके बाद, वह आपके ऐप्लिकेशन का इस्तेमाल जारी रखने के लिए, Facebook से साइन इन कर सकता है.
शुरू करने से पहले
अपने ऐप्लिकेशन में, पुष्टि करने की सुविधा देने वाली दो या इससे ज़्यादा कंपनियों को जोड़ें. इनमें, पहचान छिपाकर पुष्टि करने की सुविधा देने वाली कंपनी भी शामिल हो सकती है.
पुष्टि करने की सेवा देने वाली कंपनी के क्रेडेंशियल को उपयोगकर्ता खाते से लिंक करना
पुष्टि करने की सेवा देने वाली कंपनी के क्रेडेंशियल को किसी मौजूदा उपयोगकर्ता खाते से लिंक करने के लिए:
- पुष्टि करने की सेवा देने वाली किसी भी कंपनी या तरीके का इस्तेमाल करके, उपयोगकर्ता को साइन इन करें.
- पुष्टि करने की सेवा देने वाली नई कंपनी के लिए, साइन-इन करने की प्रोसेस पूरी करें. हालांकि, इसमें
FIRAuth.signInWithके किसी भी तरीके को कॉल करना शामिल नहीं है. उदाहरण के लिए, उपयोगकर्ता का Google आईडी टोकन, Facebook ऐक्सेस टोकन या ईमेल पता और पासवर्ड पाना. पुष्टि करने की सेवा देने वाली नई कंपनी के लिए
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 Login
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];
साइन इन किए हुए उपयोगकर्ता के
linkWithCredential:completion:तरीके में,FIRAuthCredentialऑब्जेक्ट को पास करें: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 का वही डेटा ऐक्सेस कर सकता है.
किसी उपयोगकर्ता खाते से, पुष्टि करने वाली सेवा देने वाली कंपनी को अनलिंक करना
एक Firebase उपयोगकर्ता खाते से, पुष्टि करने वाली कई सेवाएं लिंक की जा सकती हैं. जैसे, ईमेल/पासवर्ड, Google, Facebook. इससे उपयोगकर्ता, अलग-अलग तरीकों से एक ही 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) { // ... }];
समस्या का हल
अगर आपको एक से ज़्यादा खाते लिंक करने में गड़बड़ियां आ रही हैं, तो पुष्टि किए गए ईमेल पतों के बारे में दस्तावेज़ देखें.