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

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

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

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

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

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

এখন, কিছু কনফিগারেশন পদক্ষেপ সম্পাদন করুন:

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

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

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

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

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

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

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

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

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

        provider.customParameters = [
          "allow_signup": "false"
        ]
        
        [provider setCustomParameters:@{@"allow_signup": @"false"}];
        

    GitHub সমর্থন করে প্যারামিটারগুলির জন্য, GitHub OAuth ডকুমেন্টেশন দেখুন। মনে রাখবেন যে আপনি setCustomParameters দিয়ে Firebase-প্রয়োজনীয় প্যারামিটার পাস করতে পারবেন না। এই প্যারামিটারগুলি হল client_id , redirect_uri , response_type , scope এবং state

  4. ঐচ্ছিক : মৌলিক প্রোফাইলের বাইরে অতিরিক্ত OAuth 2.0 স্কোপ নির্দিষ্ট করুন যা আপনি প্রমাণীকরণ প্রদানকারীর কাছ থেকে অনুরোধ করতে চান। যদি আপনার অ্যাপ্লিকেশনের GitHub APIs থেকে ব্যক্তিগত ব্যবহারকারীর ডেটা অ্যাক্সেসের প্রয়োজন হয়, তাহলে আপনাকে GitHub বিকাশকারী কনসোলে API অনুমতিগুলির অধীনে GitHub APIগুলি অ্যাক্সেস করার অনুমতির অনুরোধ করতে হবে। অনুরোধ করা OAuth স্কোপগুলিকে অবশ্যই অ্যাপের API অনুমতিগুলিতে প্রি-কনফিগার করাগুলির সাথে হুবহু মিলে যেতে হবে।

        // Request read access to a user's email addresses.
        // This must be preconfigured in the app's API permissions.
        provider.scopes = ["user:email"]
        
        // Request read access to a user's email addresses.
        // This must be preconfigured in the app's API permissions.
        [provider setScopes:@[@"user:email"]];
        

    আরও জানতে, GitHub স্কোপ ডকুমেন্টেশন পড়ুন।

  5. ঐচ্ছিক : ব্যবহারকারীর কাছে reCAPTCHA প্রদর্শন করার সময় আপনার অ্যাপ যেভাবে SFSafariViewController বা UIWebView উপস্থাপন করে তা কাস্টমাইজ করতে চাইলে, AuthUIDelegate প্রোটোকলের সাথে সামঞ্জস্যপূর্ণ একটি কাস্টম ক্লাস তৈরি করুন এবং এটিকে credentialWithUIDelegate এ পাস করুন।

  6. OAuth প্রদানকারী অবজেক্ট ব্যবহার করে Firebase দিয়ে প্রমাণীকরণ করুন।

        provider.getCredentialWith(nil) { credential, error in
          if error != nil {
            // Handle error.
          }
          if credential != nil {
            Auth().signIn(with: credential) { authResult, error in
              if error != nil {
                // Handle error.
              }
              // User is signed in.
              // IdP data available in authResult.additionalUserInfo.profile.
    
              guard let oauthCredential = authResult.credential as? OAuthCredential else { return }
              // GitHub OAuth access token can also be retrieved by:
              // oauthCredential.accessToken
              // GitHub OAuth ID token can be retrieved by calling:
              // oauthCredential.idToken
            }
          }
        }
        
        [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.
    
              FIROAuthCredential *oauthCredential = (FIROAuthCredential *)authResult.credential;
              // GitHub OAuth access token can also be retrieved by:
              // oauthCredential.accessToken
              // GitHub OAuth ID token can be retrieved by calling:
              // oauthCredential.idToken
            }];
          }
        }];
        

    OAuth অ্যাক্সেস টোকেন ব্যবহার করে, আপনি GitHub API কল করতে পারেন।

    উদাহরণস্বরূপ, মৌলিক প্রোফাইল তথ্য পেতে, আপনি Authorization শিরোনামে অ্যাক্সেস টোকেন পাস করে REST API কল করতে পারেন:

    https://api.github.com/user
  7. যদিও উপরের উদাহরণগুলি সাইন-ইন প্রবাহের উপর ফোকাস করে, আপনার কাছে একটি GitHub প্রদানকারীকে একটি বিদ্যমান ব্যবহারকারীর সাথে লিঙ্ক করার ক্ষমতাও রয়েছে। উদাহরণস্বরূপ, আপনি একই ব্যবহারকারীর সাথে একাধিক প্রদানকারীকে লিঙ্ক করতে পারেন যাতে তাদের উভয়ের সাথে সাইন ইন করতে পারেন৷

        Auth().currentUser.link(withCredential: credential) { authResult, error in
          if error != nil {
            // Handle error.
          }
          // GitHub credential is linked to the current user.
          // IdP data available in authResult.additionalUserInfo.profile.
          // GitHub OAuth access token can also be retrieved by:
          // (authResult.credential as? OAuthCredential)?.accessToken
          // GitHub OAuth ID token can be retrieved by calling:
          // (authResult.credential as? OAuthCredential)?.idToken
        }
        
        [[FIRAuth auth].currentUser
            linkWithCredential:credential
                    completion:^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) {
          if (error) {
            // Handle error.
          }
          // GitHub credential is linked to the current user.
          // IdP data available in authResult.additionalUserInfo.profile.
          // GitHub OAuth access token is can also be retrieved by:
          // ((FIROAuthCredential *)authResult.credential).accessToken
          // GitHub OAuth ID token can be retrieved by calling:
          // ((FIROAuthCredential *)authResult.credential).idToken
        }];
        
  8. একই প্যাটার্ন 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
          // GitHub OAuth ID token can be retrieved by calling:
          // (authResult.credential as? OAuthCredential)?.idToken
        }
        
        [[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
          // GitHub OAuth ID token can be retrieved by calling:
          // ((FIROAuthCredential *)authResult.credential).idToken
        }];
        

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

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

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