OAuth की सेवा देने वाली कंपनियों का इस्तेमाल करके, अपने उपयोगकर्ताओं को Firebase की मदद से पुष्टि करने की अनुमति दी जा सकती है, जैसे कि Firebase SDK टूल इस्तेमाल करके GitHub में, सामान्य OAuth लॉगिन को अपने ऐप्लिकेशन में जोड़ें आखिर तक साइन-इन फ़्लो लागू करना.
शुरू करने से पहले
GitHub खातों का इस्तेमाल करने वाले उपयोगकर्ताओं को साइन इन करने के लिए, पहले आपको GitHub को साइन इन के तौर पर चालू करना होगा कंपनी:
Firebase डिपेंडेंसी इंस्टॉल और मैनेज करने के लिए, Swift पैकेज मैनेजर का इस्तेमाल करें.
- Xcode में, अपना ऐप्लिकेशन प्रोजेक्ट खोलने के लिए, फ़ाइल > पैकेज जोड़ना.
- जब कहा जाए, तब Firebase Apple प्लैटफ़ॉर्म SDK टूल का रिपॉज़िटरी जोड़ें:
- Firebase Authentication लाइब्रेरी चुनें.
- अपने टारगेट की बिल्ड सेटिंग के अन्य लिंकर फ़्लैग सेक्शन में
-ObjC
फ़्लैग जोड़ें. - यह काम पूरा होने पर, Xcode अपने-आप रिज़ॉल्व और डाउनलोड होना शुरू कर देगा पर निर्भर करता है.
https://github.com/firebase/firebase-ios-sdk.git
अब, कॉन्फ़िगरेशन के कुछ चरण पूरे करें:
- Firebase कंसोल में, पुष्टि सेक्शन खोलें.
- साइन इन करने का तरीका टैब पर, GitHub सेवा देने वाली कंपनी को चालू करें.
- उस कंपनी के डेवलपर कंसोल से क्लाइंट आईडी और क्लाइंट सीक्रेट जोड़ें
प्रोवाइडर कॉन्फ़िगरेशन:
- अपना ऐप्लिकेशन रजिस्टर करना GitHub पर डेवलपर ऐप्लिकेशन के तौर पर जोड़ें और अपने ऐप्लिकेशन का OAuth 2.0 क्लाइंट आईडी पाएं और क्लाइंट सीक्रेट शामिल हैं.
- पक्का करें कि आपका Firebase OAuth रीडायरेक्ट यूआरआई (उदाहरण,
my-app-12345.firebaseapp.com/__/auth/handler
) को आपके ऐप्लिकेशन के सेटिंग पेज में ऑथराइज़ेशन कॉलबैक यूआरएल के तौर पर सेट किया गया है GitHub ऐप्लिकेशन का कॉन्फ़िगरेशन.
- सेव करें पर क्लिक करें.
Firebase SDK टूल की मदद से साइन-इन की प्रोसेस को मैनेज करना
Firebase Apple प्लैटफ़ॉर्म SDK टूल से साइन-इन फ़्लो को मैनेज करने के लिए, यह तरीका अपनाएं:
अपने Xcode प्रोजेक्ट में कस्टम यूआरएल स्कीम जोड़ें:
- अपना प्रोजेक्ट कॉन्फ़िगरेशन खोलें: इसमें प्रोजेक्ट के नाम पर दो बार क्लिक करें बायां ट्री व्यू. टारगेट सेक्शन में जाकर अपना ऐप्लिकेशन चुनें. इसके बाद, जानकारी टैब चुनकर यूआरएल के टाइप सेक्शन को बड़ा करें.
- + बटन पर क्लिक करें और कोड में बदले गए अपने ऐप्लिकेशन आईडी को यूआरएल के तौर पर जोड़ें
स्कीम. अपना एन्कोडेड ऐप्लिकेशन आईडी यहां देखा जा सकता है:
सामान्य
Firebase कंसोल की सेटिंग पेज. यह आपके iOS के सेक्शन में
है. दूसरे फ़ील्ड खाली छोड़ दें.
पूरा होने पर, आपका कॉन्फ़िगरेशन कुछ ऐसा दिखेगा (लेकिन आपके ऐप्लिकेशन-विशिष्ट मानों के साथ):
प्रोवाइडर आईडी का इस्तेमाल करके OAuthProvider का इंस्टेंस बनाएं github.com पर जाएं.
var provider = OAuthProvider(providerID: "github.com")
FIROAuthProvider *provider = [FIROAuthProvider providerWithProviderID:@"github.com"];
ज़रूरी नहीं: ऐसे अतिरिक्त कस्टम OAuth पैरामीटर तय करें जिन्हें आपको इस्तेमाल करना है OAuth अनुरोध के साथ भेजें.
provider.customParameters = [ "allow_signup": "false" ]
[provider setCustomParameters:@{@"allow_signup": @"false"}];
GitHub पर काम करने वाले पैरामीटर के लिए, यहां देखें GitHub OAuth दस्तावेज़. ध्यान दें कि आप Firebase के लिए ज़रूरी पैरामीटर को
setCustomParameters
. ये पैरामीटर client_id हैं, redirect_uri, response_type, दायरा, और state.ज़रूरी नहीं: बेसिक प्रोफ़ाइल के अलावा, OAuth 2.0 के अन्य दायरे तय करें. पुष्टि करने की सेवा देने वाली कंपनी से अनुरोध करें. अगर आपका आवेदन को GitHub API से निजी उपयोगकर्ता डेटा का ऐक्सेस चाहिए, तो आपको इसमें एपीआई की अनुमतियों के तहत, GitHub API को ऐक्सेस करने की अनुमतियों का अनुरोध करें GitHub डेवलपर कंसोल. अनुरोध किए गए OAuth के दायरे इससे पूरी तरह मेल खाने चाहिए एपीआई की अनुमतियों में पहले से कॉन्फ़िगर की गई अनुमतियां शामिल होती हैं.
// 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 के दायरे से जुड़ा दस्तावेज़.
ज़रूरी नहीं: अगर आपको अपने ऐप्लिकेशन के दिखने का तरीका अपनी पसंद के मुताबिक बनाना है, तो
SFSafariViewController
याUIWebView
जब उपयोगकर्ता को reCAPTCHA दिखाएं, शर्तों के मुताबिक कस्टम क्लास बनाएंAuthUIDelegate
प्रोटोकॉल से कनेक्ट करें और उसेcredentialWithUIDelegate
.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.
उदाहरण के लिए, प्रोफ़ाइल की बुनियादी जानकारी पाने के लिए, REST API को कॉल करें,
Authorization
हेडर में ऐक्सेस टोकन पास करना:https://api.github.com/user
ऊपर दिए गए उदाहरण, साइन-इन करने के फ़्लो पर फ़ोकस करते हैं. हालांकि, आपके पास 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 }];
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; } }]; } }];
अगले चरण
किसी उपयोगकर्ता के पहली बार साइन इन करने के बाद, एक नया उपयोगकर्ता खाता बना दिया जाता है और आपके खाते के क्रेडेंशियल मौजूद हैं, जैसे कि उपयोगकर्ता नाम और पासवर्ड, या पुष्टि करने वाली कंपनी की जानकारी—उपयोगकर्ता ने जिससे साइन इन किया है. यह नया खाते को आपके Firebase प्रोजेक्ट के हिस्से के तौर पर सेव किया जाता है. साथ ही, इसका इस्तेमाल आपके प्रोजेक्ट के हर ऐप्लिकेशन में हर उपयोगकर्ता के लिए उपलब्ध होता है. भले ही, उपयोगकर्ता किसी भी तरह से साइन इन करता हो.
-
अपने ऐप्लिकेशन में, उपयोगकर्ता की बुनियादी प्रोफ़ाइल जानकारी को
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; }
ऐसा हो सकता है कि आप पुष्टि करने की पूरी रेंज के लिए, गड़बड़ी मैनेज करने वाला कोड भी जोड़ना चाहें गड़बड़ियां हैं. गड़बड़ियां मैनेज करना देखें.