অ্যাপল প্ল্যাটফর্মে টুইটার ব্যবহার করে প্রমাণীকরণ করুন

আপনি আপনার ব্যবহারকারীদেরকে Twitter এর মত OAuth প্রদানকারী ব্যবহার করে Firebase-এর সাথে প্রমাণীকরণ করতে দিতে পারেন আপনার অ্যাপে জেনেরিক OAuth লগইনকে একীভূত করে Firebase SDK ব্যবহার করে সাইন-ইন প্রবাহ শেষ করতে।

আপনি শুরু করার আগে

ফায়ারবেস নির্ভরতা ইনস্টল এবং পরিচালনা করতে সুইফট প্যাকেজ ম্যানেজার ব্যবহার করুন।

  1. Xcode-এ, আপনার অ্যাপ প্রকল্প খোলার সাথে, ফাইল > প্যাকেজ যোগ করুন- এ নেভিগেট করুন।
  2. অনুরোধ করা হলে, Firebase Apple প্ল্যাটফর্ম SDK সংগ্রহস্থল যোগ করুন:
  3.   https://github.com/firebase/firebase-ios-sdk.git
  4. Firebase Authentication লাইব্রেরি বেছে নিন।
  5. আপনার লক্ষ্যের বিল্ড সেটিংসের অন্যান্য লিঙ্কার ফ্ল্যাগ বিভাগে -ObjC পতাকা যোগ করুন।
  6. শেষ হয়ে গেলে, Xcode স্বয়ংক্রিয়ভাবে পটভূমিতে আপনার নির্ভরতাগুলি সমাধান এবং ডাউনলোড করা শুরু করবে।

টুইটার অ্যাকাউন্ট ব্যবহার করে ব্যবহারকারীদের সাইন ইন করতে, আপনাকে প্রথমে আপনার Firebase প্রকল্পের জন্য একটি সাইন-ইন প্রদানকারী হিসাবে Twitter সক্ষম করতে হবে:

  1. আপনার Apple প্রকল্পে Firebase যোগ করুন

  2. আপনার Podfile নিম্নলিখিত পডগুলি অন্তর্ভুক্ত করুন:

    pod 'FirebaseAuth'
  3. Firebase কনসোলে , Auth বিভাগটি খুলুন।
  4. সাইন ইন পদ্ধতি ট্যাবে, টুইটার প্রদানকারী সক্ষম করুন।
  5. সেই প্রদানকারীর বিকাশকারী কনসোল থেকে প্রদানকারী কনফিগারেশনে API কী এবং API গোপন যোগ করুন:
    1. টুইটারে একটি বিকাশকারী অ্যাপ্লিকেশন হিসাবে আপনার অ্যাপটি নিবন্ধন করুন এবং আপনার অ্যাপের OAuth API কী এবং API গোপন পান৷
    2. নিশ্চিত করুন যে আপনার Firebase OAuth রিডাইরেক্ট URI (যেমন my-app-12345.firebaseapp.com/__/auth/handler ) আপনার Twitter অ্যাপের কনফিগারেশনে আপনার অ্যাপের সেটিংস পৃষ্ঠায় আপনার অনুমোদন কলব্যাক URL হিসেবে সেট করা আছে।
  6. Save এ ক্লিক করুন।

Firebase SDK দিয়ে সাইন-ইন ফ্লো পরিচালনা করুন

Firebase Apple প্ল্যাটফর্ম SDK-এর সাথে সাইন-ইন প্রবাহ পরিচালনা করতে, এই ধাপগুলি অনুসরণ করুন:

  1. আপনার Xcode প্রকল্পে কাস্টম URL স্কিম যোগ করুন:

    1. আপনার প্রজেক্ট কনফিগারেশন খুলুন: বাম ট্রি ভিউতে প্রজেক্টের নামে ডাবল ক্লিক করুন। লক্ষ্য বিভাগ থেকে আপনার অ্যাপটি নির্বাচন করুন, তারপর তথ্য ট্যাবটি নির্বাচন করুন এবং URL প্রকার বিভাগটি প্রসারিত করুন৷
    2. + বোতামে ক্লিক করুন এবং একটি URL স্কিম হিসাবে আপনার এনকোড করা অ্যাপ আইডি যোগ করুন। আপনি আপনার iOS অ্যাপের বিভাগে Firebase কনসোলের সাধারণ সেটিংস পৃষ্ঠায় আপনার এনকোড করা অ্যাপ আইডি খুঁজে পেতে পারেন। অন্যান্য ক্ষেত্রগুলি ফাঁকা রাখুন।

      সম্পন্ন হলে, আপনার কনফিগারেশনটি নিম্নলিখিতগুলির মতো দেখতে হবে (কিন্তু আপনার অ্যাপ্লিকেশন-নির্দিষ্ট মানগুলির সাথে):

      Xcode এর কাস্টম URL স্কিম সেটআপ ইন্টারফেসের স্ক্রিনশট

  2. প্রদানকারী আইডি twitter.com ব্যবহার করে একটি OAuthProvider- এর একটি উদাহরণ তৈরি করুন।

        var provider = OAuthProvider(providerID: "twitter.com")
        
        FIROAuthProvider *provider = [FIROAuthProvider providerWithProviderID:@"twitter.com"];
        
  3. ঐচ্ছিক : অতিরিক্ত কাস্টম OAuth প্যারামিটার নির্দিষ্ট করুন যা আপনি OAuth অনুরোধের সাথে পাঠাতে চান।

        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 দিয়ে প্রমাণীকরণ করুন।

        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 প্রদানকারীকে একটি বিদ্যমান ব্যবহারকারীর সাথে লিঙ্ক করার ক্ষমতাও রয়েছে৷ উদাহরণস্বরূপ, আপনি একই ব্যবহারকারীর সাথে একাধিক প্রদানকারীকে লিঙ্ক করতে পারেন যাতে তাদের উভয়ের সাথে সাইন ইন করতে পারেন৷

        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 এর সাথে ব্যবহার করা যেতে পারে যা সাম্প্রতিক লগইন প্রয়োজন এমন সংবেদনশীল ক্রিয়াকলাপের জন্য নতুন শংসাপত্র পুনরুদ্ধার করতে ব্যবহার করা যেতে পারে।

        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 কনসোলে প্রতি ইমেল ঠিকানা সেটিং-এ একটি অ্যাকাউন্ট সক্ষম করে থাকেন, যখন একজন ব্যবহারকারী একটি প্রদানকারীতে (যেমন টুইটার) সাইন ইন করার চেষ্টা করে এমন একটি ইমেল দিয়ে যা আগে থেকেই অন্য Firebase ব্যবহারকারীর প্রদানকারীর (যেমন Google) জন্য বিদ্যমান, ত্রুটি FIRAuthErrorCodeAccountExistsWithDifferentCredential একটি অস্থায়ী FIRAuthCredential অবজেক্ট (Twitter credential) সহ নিক্ষেপ করা হয়। উদ্দেশ্যপ্রণোদিত প্রদানকারীতে সাইন ইন সম্পূর্ণ করতে, ব্যবহারকারীকে প্রথমে বিদ্যমান প্রদানকারীতে (Google) সাইন ইন করতে হবে এবং তারপরে প্রাক্তন FIRAuthCredential (Twitter credential)-এর সাথে লিঙ্ক করতে হবে। এটি নীচের চিত্রিত হিসাবে দেখাবে:

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

পরবর্তী পদক্ষেপ

একজন ব্যবহারকারী প্রথমবার সাইন ইন করার পরে, একটি নতুন ব্যবহারকারীর অ্যাকাউন্ট তৈরি করা হয় এবং শংসাপত্রগুলির সাথে লিঙ্ক করা হয়—অর্থাৎ, ব্যবহারকারীর নাম এবং পাসওয়ার্ড, ফোন নম্বর, বা প্রমাণ প্রদানকারীর তথ্য — ব্যবহারকারী সাইন ইন করেছেন। এই নতুন অ্যাকাউন্টটি আপনার ফায়ারবেস প্রকল্পের অংশ হিসাবে সংরক্ষণ করা হয়েছে এবং ব্যবহারকারী কীভাবে সাইন ইন করুন না কেন, আপনার প্রকল্পের প্রতিটি অ্যাপ জুড়ে একজন ব্যবহারকারীকে শনাক্ত করতে ব্যবহার করা যেতে পারে।

  • আপনার অ্যাপে, আপনি User অবজেক্ট থেকে ব্যবহারকারীর মৌলিক প্রোফাইল তথ্য পেতে পারেন। ব্যবহারকারীদের পরিচালনা দেখুন।

  • আপনার Firebase Realtime Database এবং Cloud Storage সুরক্ষা নিয়মে , আপনি auth ভেরিয়েবল থেকে সাইন-ইন করা ব্যবহারকারীর অনন্য ব্যবহারকারী আইডি পেতে পারেন এবং ব্যবহারকারী কোন ডেটা অ্যাক্সেস করতে পারে তা নিয়ন্ত্রণ করতে এটি ব্যবহার করতে পারেন।

আপনি একটি বিদ্যমান ব্যবহারকারীর অ্যাকাউন্টে প্রমাণীকরণ প্রদানকারীর শংসাপত্র লিঙ্ক করে একাধিক প্রমাণীকরণ প্রদানকারী ব্যবহার করে ব্যবহারকারীদের আপনার অ্যাপে সাইন ইন করার অনুমতি দিতে পারেন।

একজন ব্যবহারকারীকে সাইন আউট করতে, signOut:

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

আপনি প্রমাণীকরণ ত্রুটির সম্পূর্ণ পরিসরের জন্য ত্রুটি হ্যান্ডলিং কোড যোগ করতে চাইতে পারেন। হ্যান্ডেল ত্রুটি দেখুন.