Apple प्लैटफ़ॉर्म पर Twitter का इस्तेमाल करके पुष्टि करें

OAuth की सेवा देने वाली कंपनियों का इस्तेमाल करके, अपने उपयोगकर्ताओं को Firebase की मदद से पुष्टि करने की अनुमति दी जा सकती है Firebase SDK टूल की मदद से अपने ऐप्लिकेशन में सामान्य OAuth लॉगिन जोड़कर Twitter करें. आखिर तक साइन-इन फ़्लो लागू करना.

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

Firebase डिपेंडेंसी इंस्टॉल और मैनेज करने के लिए, Swift पैकेज मैनेजर का इस्तेमाल करें.

  1. Xcode में, अपना ऐप्लिकेशन प्रोजेक्ट खोलने के लिए, फ़ाइल > पैकेज जोड़ना.
  2. जब कहा जाए, तब Firebase Apple प्लैटफ़ॉर्म SDK टूल का रिपॉज़िटरी जोड़ें:
  3.   https://github.com/firebase/firebase-ios-sdk.git
  4. Firebase Authentication लाइब्रेरी चुनें.
  5. अपने टारगेट की बिल्ड सेटिंग के अन्य लिंकर फ़्लैग सेक्शन में -ObjC फ़्लैग जोड़ें.
  6. यह काम पूरा होने पर, Xcode अपने-आप रिज़ॉल्व और डाउनलोड होना शुरू कर देगा पर निर्भर करता है.

Twitter खातों से साइन इन करने के लिए, आपको पहले Twitter को साइन इन के तौर पर चालू करना होगा कंपनी:

  1. अपने Apple प्रोजेक्ट में Firebase जोड़ें.

  2. अपने Podfile में ये पॉड शामिल करें:

    pod 'FirebaseAuth'
  3. Firebase कंसोल में, पुष्टि सेक्शन खोलें.
  4. साइन इन करने का तरीका टैब पर, Twitter की सेवा देने वाली कंपनी को चालू करें.
  5. उस कंपनी के डेवलपर कंसोल से एपीआई पासकोड और एपीआई सीक्रेट को प्रोवाइडर कॉन्फ़िगरेशन:
    1. अपना ऐप्लिकेशन रजिस्टर करना को भी इंस्टॉल करें और अपने ऐप्लिकेशन की OAuth एपीआई पासकोड पाएं और एपीआई सीक्रेट शामिल है.
    2. पक्का करें कि आपका Firebase OAuth रीडायरेक्ट यूआरआई (उदाहरण, my-app-12345.firebaseapp.com/__/auth/handler) को आपके ऐप्लिकेशन के सेटिंग पेज में ऑथराइज़ेशन कॉलबैक यूआरएल के तौर पर सेट किया गया है Twitter ऐप्लिकेशन का कॉन्फ़िगरेशन.
  6. सेव करें पर क्लिक करें.

Firebase SDK टूल की मदद से साइन-इन की प्रोसेस को मैनेज करना

Firebase Apple प्लैटफ़ॉर्म SDK टूल से साइन-इन फ़्लो को मैनेज करने के लिए, यह तरीका अपनाएं:

  1. अपने Xcode प्रोजेक्ट में कस्टम यूआरएल स्कीम जोड़ें:

    1. अपना प्रोजेक्ट कॉन्फ़िगरेशन खोलें: इसमें प्रोजेक्ट के नाम पर दो बार क्लिक करें बायां ट्री व्यू. टारगेट सेक्शन में जाकर अपना ऐप्लिकेशन चुनें. इसके बाद, जानकारी टैब चुनकर यूआरएल के टाइप सेक्शन को बड़ा करें.
    2. + बटन पर क्लिक करें और कोड में बदले गए अपने ऐप्लिकेशन आईडी को यूआरएल के तौर पर जोड़ें स्कीम. अपना एन्कोडेड ऐप्लिकेशन आईडी यहां देखा जा सकता है: सामान्य Firebase कंसोल की सेटिंग पेज. यह आपके iOS के सेक्शन में है. दूसरे फ़ील्ड खाली छोड़ दें.

      पूरा होने पर, आपका कॉन्फ़िगरेशन कुछ ऐसा दिखेगा (लेकिन आपके ऐप्लिकेशन-विशिष्ट मानों के साथ):

      Xcode के कस्टम यूआरएल स्कीम के सेटअप इंटरफ़ेस का स्क्रीनशॉट

  2. प्रोवाइडर आईडी का इस्तेमाल करके OAuthProvider का इंस्टेंस बनाएं twitter.com पर सेव करें.

    SwiftObjective-C
        var provider = OAuthProvider(providerID: "twitter.com")
        
        FIROAuthProvider *provider = [FIROAuthProvider providerWithProviderID:@"twitter.com"];
        
  3. ज़रूरी नहीं: ऐसे अतिरिक्त कस्टम OAuth पैरामीटर तय करें जिन्हें आपको इस्तेमाल करना है OAuth अनुरोध के साथ भेजें.

    SwiftObjective-C
        provider.customParameters = [
          "lang": "fr"
          ]
        
        [provider setCustomParameters:@{@"lang": @"fr"}];
        

    Twitter पर इस्तेमाल किए जाने वाले पैरामीटर के बारे में जानने के लिए, Twitter पर OAuth के दस्तावेज़. ध्यान दें कि आप Firebase के लिए ज़रूरी पैरामीटर को setCustomParameters. ये पैरामीटर client_id हैं, redirect_uri, response_type, दायरा, और state.

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

  5. OAuth प्रोवाइडर ऑब्जेक्ट का इस्तेमाल करके, Firebase से पुष्टि करें.

    SwiftObjective-C
        provider.getCredentialWith(nil) { credential, error in
          if error != nil {
            // Handle error.
          }
          if credential != nil {
            Auth.auth().signIn(with: credential) { authResult, error in
              if error != nil {
                // Handle error.
              }
              // User is signed in.
              // IdP data available in authResult.additionalUserInfo.profile.
              // Twitter OAuth access token can also be retrieved by:
              // (authResult.credential as? OAuthCredential)?.accessToken
              // Twitter OAuth ID token can be retrieved by calling:
              // (authResult.credential as? OAuthCredential)?.idToken
              // Twitter OAuth secret can be retrieved by calling:
              // (authResult.credential as? OAuthCredential)?.secret
            }
          }
        }
        
        [provider getCredentialWithUIDelegate:nil
                                   completion:^(FIRAuthCredential *_Nullable credential, NSError *_Nullable error) {
          if (error) {
           // Handle error.
          }
          if (credential) {
            [[FIRAuth auth] signInWithCredential:credential
                                      completion:^(FIRAuthDataResult *_Nullable authResult, NSError *_Nullable error) {
              if (error) {
                // Handle error.
              }
              // User is signed in.
              // IdP data available in authResult.additionalUserInfo.profile.
              // Twitter OAuth access token can also be retrieved by:
              // authResult.credential.accessToken
              // Twitter OAuth ID token can be retrieved by calling:
              // authResult.credential.idToken
              // Twitter OAuth secret can be retrieved by calling:
              // authResult.credential.secret
            }];
          }
        }];
        

    OAuth ऐक्सेस टोकन का इस्तेमाल करके, Twitter API.

    उदाहरण के लिए, प्रोफ़ाइल की बुनियादी जानकारी पाने के लिए, REST API को कॉल करें, Authorization हेडर में ऐक्सेस टोकन पास करना:

    https://api.twitter.com/labs/1/users?usernames=TwitterDev
  6. ऊपर दिए गए उदाहरण, साइन-इन करने के फ़्लो पर फ़ोकस करते हैं. हालांकि, आपके पास Twitter प्रोवाइडर को किसी मौजूदा उपयोगकर्ता से लिंक करने की सुविधा. उदाहरण के लिए, आपके पास कई सेवा देने वाली कंपनियों को एक ही उपयोगकर्ता से लिंक करें, जिससे वे दोनों में से किसी एक से साइन इन कर सकें.

    SwiftObjective-C
        Auth().currentUser.link(withCredential: credential) { authResult, error in
          if error != nil {
            // Handle error.
          }
          // Twitter credential is linked to the current user.
          // IdP data available in authResult.additionalUserInfo.profile.
          // Twitter OAuth access token can also be retrieved by:
          // (authResult.credential as? OAuthCredential)?.accessToken
          // Twitter OAuth ID token can be retrieved by calling:
          // (authResult.credential as? OAuthCredential)?.idToken
          // Twitter OAuth secret can be retrieved by calling:
          // (authResult.credential as? OAuthCredential)?.secret
        }
        
        [[FIRAuth auth].currentUser
            linkWithCredential:credential
                    completion:^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) {
          if (error) {
            // Handle error.
          }
          // Twitter credential is linked to the current user.
          // IdP data available in authResult.additionalUserInfo.profile.
          // Twitter OAuth access token is can also be retrieved by:
          // ((FIROAuthCredential *)authResult.credential).accessToken
          // Twitter OAuth ID token can be retrieved by calling:
          // ((FIROAuthCredential *)authResult.credential).idToken
          // Twitter OAuth secret can be retrieved by calling:
          // ((FIROAuthCredential *)authResult.credential).secret
        }];
        
  7. reauthenticateWithCredential के साथ भी इसी पैटर्न का इस्तेमाल किया जा सकता है, जो का इस्तेमाल, संवेदनशील कार्रवाइयों के लिए नए क्रेडेंशियल हासिल करने के लिए किया जा सकता है. हाल ही का लॉगिन.

    SwiftObjective-C
        Auth().currentUser.reauthenticateWithCredential(withCredential: credential) { authResult, error in
          if error != nil {
            // Handle error.
          }
          // User is re-authenticated with fresh tokens minted and
          // should be able to perform sensitive operations like account
          // deletion and email or password update.
          // IdP data available in result.additionalUserInfo.profile.
          // Additional OAuth access token is can also be retrieved by:
          // (authResult.credential as? OAuthCredential)?.accessToken
          // Twitter OAuth ID token can be retrieved by calling:
          // (authResult.credential as? OAuthCredential)?.idToken
          // Twitter OAuth secret can be retrieved by calling:
          // (authResult.credential as? OAuthCredential)?.secret
        }
        
        [[FIRAuth auth].currentUser
            reauthenticateWithCredential:credential
                              completion:^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) {
          if (error) {
            // Handle error.
          }
          // User is re-authenticated with fresh tokens minted and
          // should be able to perform sensitive operations like account
          // deletion and email or password update.
          // IdP data available in result.additionalUserInfo.profile.
          // Additional OAuth access token is can also be retrieved by:
          // ((FIROAuthCredential *)authResult.credential).accessToken
          // Twitter OAuth ID token can be retrieved by calling:
          // ((FIROAuthCredential *)authResult.credential).idToken
          // Twitter OAuth secret can be retrieved by calling:
          // ((FIROAuthCredential *)authResult.credential).secret
        }];
        

अगर आपने Firebase कंसोल में, हर ईमेल पते के लिए एक खाता सेटिंग चालू की है, तो जब कोई उपयोगकर्ता किसी ऐसे ईमेल से, सेवा देने वाली किसी कंपनी (जैसे कि Twitter) में साइन इन करने की कोशिश करता है जो पहले से ही मौजूद है Firebase का इस्तेमाल करने वाले किसी दूसरे व्यक्ति को सेवा देने वाली कंपनी (जैसे, Google) के लिए मौजूद है, तो गड़बड़ी FIRAuthErrorCodeAccountExistsWithDifferentCredential को अस्थायी रूप से फेंक दिया गया है FIRAuthCredential ऑब्जेक्ट (Twitter क्रेडेंशियल). साइन इन करने के लिए, सेवा देने वाली मौजूदा कंपनी के लिए, उपयोगकर्ता को पहले मौजूदा कंपनी (Google) से साइन करना होगा और फिर पुराना FIRAuthCredential (Twitter क्रेडेंशियल). यह ऐसा दिखेगा, जैसा कि नीचे दिखाया गया है:

SwiftObjective-C
  // Sign-in with an OAuth credential.
  provider.getCredentialWith(nil) { credential, error in
    // An account with the same email already exists.
    if (error as NSError?)?.code == AuthErrorCode.accountExistsWithDifferentCredential.rawValue {
      // Get pending credential and email of existing account.
      let existingAcctEmail = (error! as NSError).userInfo[AuthErrorUserInfoEmailKey] as! String
      let pendingCred = (error! as NSError).userInfo[AuthErrorUserInfoUpdatedCredentialKey] as! AuthCredential
      // Lookup existing account identifier by the email.
      Auth.auth().fetchProviders(forEmail:existingAcctEmail) { providers, error in
        // Existing email/password account.
        if (providers?.contains(EmailAuthProviderID))! {
          // Existing password account for email. Ask user to provide the password of the
          // existing account.
          // Sign in with existing account.
          Auth.auth().signIn(withEmail:existingAcctEmail, password:password) { user, error in
            // Successfully signed in.
            if user != nil {
              // Link pending credential to account.
              Auth.auth().currentUser?.linkAndRetrieveData(with: pendingCred) { result, error in
                // ...
              }
            }
          }
        }
      }
      return
    }

    // Other errors.
    if error != nil {
      // handle the error.
      return
    }

    // Sign in with the credential.
    if credential != nil {
      Auth.auth().signInAndRetrieveData(with: credential!) { result, error in
        if error != nil {
          // handle the error.
          return
        }
      }
    }
  }

  
  // Sign-in with an OAuth credential.
  [provider getCredentialWithUIDelegate:nil
                             completion:^(FIRAuthCredential *_Nullable credential, NSError *_Nullable error) {
    // An account with the same email already exists.
    if (error.code == FIRAuthErrorCodeAccountExistsWithDifferentCredential) {
      // Get pending credential and email of existing account.
      NSString *existingAcctEmail = error.userInfo[FIRAuthErrorUserInfoEmailKey];
      FIRAuthCredential *pendingCred = error.userInfo[FIRAuthErrorUserInfoUpdatedCredentialKey];
      // Lookup existing account identifier by the email.
      [[FIRAuth auth] fetchProvidersForEmail:existingAcctEmail
                                 completion:^(NSArray<NSString *> *_Nullable providers,
                                              NSError *_Nullable error) {
        // Existing email/password account.
        if ( [providers containsObject:FIREmailAuthProviderID] ) {
          // Existing password account for email. Ask user to provide the password of the
          // existing account.

          // Sign in with existing account.
          [[FIRAuth auth] signInWithEmail:existingAcctEmail
                                 password:password
                               completion:^(FIRUser *user, NSError *error) {
            // Successfully signed in.
            if (user) {
              // Link pending credential to account.
              [[FIRAuth auth].currentUser linkWithCredential:pendingCred
                                                  completion:^(FIRUser *_Nullable user,
                                                               NSError *_Nullable error) {
                // ...
              }];
            }
          }];
        }
      }];
      return;
    }

    // Other errors.
    if (error) {
      // handle the error.
      return;
    }

    // Sign in with the credential.
    if (credential) {
      [[FIRAuth auth] signInAndRetrieveDataWithCredential:credential
          completion:^(FIRAuthDataResult *_Nullable authResult,
                       NSError *_Nullable error) {
        if (error) {
          // handle the error.
          return;
        }
      }];
    }
  }];
  

अगले चरण

किसी उपयोगकर्ता के पहली बार साइन इन करने के बाद, एक नया उपयोगकर्ता खाता बना दिया जाता है और आपके खाते के क्रेडेंशियल मौजूद हैं, जैसे कि उपयोगकर्ता नाम और पासवर्ड, या पुष्टि करने वाली कंपनी की जानकारी—उपयोगकर्ता ने जिससे साइन इन किया है. यह नया खाते को आपके Firebase प्रोजेक्ट के हिस्से के तौर पर सेव किया जाता है. साथ ही, इसका इस्तेमाल आपके प्रोजेक्ट के हर ऐप्लिकेशन में हर उपयोगकर्ता के लिए उपलब्ध होता है. भले ही, उपयोगकर्ता किसी भी तरह से साइन इन करता हो.

  • अपने ऐप्लिकेशन में, उपयोगकर्ता की बुनियादी प्रोफ़ाइल जानकारी को User ऑब्जेक्ट है. उपयोगकर्ताओं को मैनेज करना देखें.

  • आपके Firebase Realtime Database और Cloud Storage में सुरक्षा के नियम, ये काम किए जा सकते हैं auth वैरिएबल से साइन-इन किए हुए उपयोगकर्ता का यूनीक यूज़र आईडी पाएं, और इसका इस्तेमाल करके यह कंट्रोल किया जा सकता है कि उपयोगकर्ता कौनसा डेटा ऐक्सेस कर सकता है.

उपयोगकर्ताओं को, पुष्टि करने के एक से ज़्यादा तरीके का इस्तेमाल करके, अपने ऐप्लिकेशन में साइन इन करने की अनुमति दी जा सकती है पुष्टि करने वाले के क्रेडेंशियल जोड़कर मौजूदा उपयोगकर्ता खाते से लिंक किया जा सकता है.

उपयोगकर्ता को साइन आउट करने के लिए पर कॉल करें signOut:.

SwiftObjective-C
let firebaseAuth = Auth.auth()
do {
  try firebaseAuth.signOut()
} catch let signOutError as NSError {
  print("Error signing out: %@", signOutError)
}
NSError *signOutError;
BOOL status = [[FIRAuth auth] signOut:&signOutError];
if (!status) {
  NSLog(@"Error signing out: %@", signOutError);
  return;
}

ऐसा हो सकता है कि आप पुष्टि करने की पूरी रेंज के लिए, गड़बड़ी मैनेज करने वाला कोड भी जोड़ना चाहें गड़बड़ियां हैं. गड़बड़ियां मैनेज करना देखें.