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

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

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

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

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

Twitter खातों का इस्तेमाल करके उपयोगकर्ताओं को साइन इन कराने के लिए, आपको अपने Firebase प्रोजेक्ट के लिए, साइन-इन की सुविधा देने वाली सेवा के तौर पर Twitter को चालू करना होगा:

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

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

    pod 'FirebaseAuth'
  3. Firebase कंसोल में, Auth सेक्शन खोलें.
  4. साइन इन करने का तरीका टैब पर, Twitter की सेवा देने वाली कंपनी को चालू करें.
  5. सेवा देने वाली कंपनी के डेवलपर कंसोल से, एपीआई पासकोड और एपीआई पासकोड को सेवा देने वाली कंपनी के कॉन्फ़िगरेशन में जोड़ें:
    1. Twitter पर डेवलपर ऐप्लिकेशन के तौर पर अपने ऐप्लिकेशन को रजिस्टर करें और अपने ऐप्लिकेशन का 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. प्रोवाइडर आईडी twitter.com का इस्तेमाल करके, OAuthProvider का एक इंस्टेंस बनाएं.

    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 दस्तावेज़ देखें. ध्यान दें कि setCustomParameters के साथ, Firebase के लिए ज़रूरी पैरामीटर पास नहीं किए जा सकते. ये पैरामीटर client_id, redirect_uri, response_type, scope, और state हैं.

  4. ज़रूरी नहीं: अगर आपको उपयोगकर्ता को reCAPTCHA दिखाते समय, अपने ऐप्लिकेशन में SFSafariViewController या UIWebView को अपनी पसंद के मुताबिक दिखाना है, तो 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 को कॉल किया जा सकता है.

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

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

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