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

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

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

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

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

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

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

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

    pod 'FirebaseAuth'
    

  3. Firebase कंसोल में, पुष्टि करें सेक्शन खोलें.
  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. + बटन पर क्लिक करें और अपने कोड में बदले गए ऐप्लिकेशन आईडी को यूआरएल स्कीम के तौर पर जोड़ें. अपने iOS ऐप्लिकेशन के सेक्शन में, Firebase कंसोल के सामान्य सेटिंग पेज पर, कोड में बदला गया अपना ऐप्लिकेशन आईडी देखा जा सकता है. दूसरे फ़ील्ड खाली छोड़ दें.

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

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

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

    Swift

        var provider = OAuthProvider(providerID: "twitter.com")
        

    Objective-C

        FIROAuthProvider *provider = [FIROAuthProvider providerWithProviderID:@"twitter.com"];
        
  3. ज़रूरी नहीं: ऐसे अतिरिक्त कस्टम OAuth पैरामीटर डालें जिन्हें आपको OAuth अनुरोध की मदद से भेजना है.

    Swift

        provider.customParameters = [
          "lang": "fr"
          ]
        

    Objective-C

        [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 से पुष्टि करें.

    Swift

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

    Objective-C

        [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 सेवा देने वाली कंपनी को किसी मौजूदा उपयोगकर्ता से लिंक करने की सुविधा भी है. उदाहरण के लिए, एक ही उपयोगकर्ता को कई कंपनियों से लिंक किया जा सकता है, ताकि वे दोनों में से किसी एक से साइन इन कर सकें.

    Swift

        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
        }
        

    Objective-C

        [[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 के साथ भी इस्तेमाल किया जा सकता है. इसकी मदद से, संवेदनशील कार्रवाइयों के लिए नए क्रेडेंशियल हासिल किए जा सकते हैं. ऐसा उन संवेदनशील कार्रवाइयों के लिए किया जाता है जिनमें हाल ही में लॉगिन करने की ज़रूरत होती है.

    Swift

        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
        }
        

    Objective-C

        [[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 प्रोजेक्ट के हिस्से के तौर पर सेव किया जाता है. साथ ही, इसका इस्तेमाल आपके प्रोजेक्ट के हर ऐप्लिकेशन में किसी उपयोगकर्ता की पहचान करने के लिए किया जा सकता है. भले ही, उपयोगकर्ता ने किसी भी तरह से साइन इन किया हो.

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

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

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

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

Swift

let firebaseAuth = Auth.auth()
do {
  try firebaseAuth.signOut()
} catch let signOutError as NSError {
  print("Error signing out: %@", signOutError)
}

Objective-C

NSError *signOutError;
BOOL status = [[FIRAuth auth] signOut:&signOutError];
if (!status) {
  NSLog(@"Error signing out: %@", signOutError);
  return;
}

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