GitHub जैसे OAuth प्रोवाइडर का इस्तेमाल करके, अपने उपयोगकर्ताओं को Firebase से पुष्टि करने की अनुमति दी जा सकती है. इसके लिए, Firebase SDK टूल का इस्तेमाल करके, अपने ऐप्लिकेशन में सामान्य OAuth लॉगिन को इंटिग्रेट करें. इससे, साइन-इन करने की पूरी प्रोसेस को पूरा किया जा सकता है.
शुरू करने से पहले
GitHub खातों का इस्तेमाल करके उपयोगकर्ताओं को साइन इन कराने के लिए, आपको सबसे पहले अपने Firebase प्रोजेक्ट के लिए, साइन-इन करने की सुविधा देने वाली सेवा के तौर पर GitHub को चालू करना होगा:
Firebase डिपेंडेंसी इंस्टॉल और मैनेज करने के लिए, Swift Package Manager का इस्तेमाल करें.
- Xcode में, अपना ऐप्लिकेशन प्रोजेक्ट खोलकर, फ़ाइल > पैकेज जोड़ें पर जाएं.
- जब कहा जाए, तब Firebase के Apple प्लैटफ़ॉर्म के SDK टूल का रिपॉज़िटरी जोड़ें:
- Firebase Authentication लाइब्रेरी चुनें.
- अपने टारगेट की बिल्ड सेटिंग के अन्य लिंकर फ़्लैग सेक्शन में
-ObjC
फ़्लैग जोड़ें. - प्रोसेस पूरी होने के बाद, Xcode बैकग्राउंड में आपकी डिपेंडेंसी को अपने-आप हल और डाउनलोड करना शुरू कर देगा.
https://github.com/firebase/firebase-ios-sdk.git
अब, कॉन्फ़िगरेशन के लिए यह तरीका अपनाएं:
- Firebase कंसोल में, Auth सेक्शन खोलें.
- साइन इन करने का तरीका टैब पर, GitHub की सेवा देने वाली कंपनी को चालू करें.
- सेवा देने वाली कंपनी के डेवलपर कंसोल से, क्लाइंट आईडी और क्लाइंट सीक्रेट को सेवा देने वाली कंपनी के कॉन्फ़िगरेशन में जोड़ें:
- GitHub पर डेवलपर ऐप्लिकेशन के तौर पर अपना ऐप्लिकेशन रजिस्टर करें और अपने ऐप्लिकेशन का OAuth 2.0 क्लाइंट आईडी और क्लाइंट सीक्रेट पाएं.
- पक्का करें कि आपके Firebase OAuth रीडायरेक्ट यूआरआई (उदाहरण के लिए,
my-app-12345.firebaseapp.com/__/auth/handler
) को, GitHub ऐप्लिकेशन के कॉन्फ़िगरेशन में, ऐप्लिकेशन के सेटिंग पेज पर अनुमति वाले कॉलबैक यूआरआई के तौर पर सेट किया गया हो.
- सेव करें पर क्लिक करें.
Firebase SDK टूल की मदद से साइन इन फ़्लो को मैनेज करना
Firebase के Apple प्लैटफ़ॉर्म के SDK टूल की मदद से, साइन-इन फ़्लो को मैनेज करने के लिए, यह तरीका अपनाएं:
अपने Xcode प्रोजेक्ट में कस्टम यूआरएल स्कीम जोड़ें:
- अपना प्रोजेक्ट कॉन्फ़िगरेशन खोलें: बाईं ओर मौजूद ट्री व्यू में, प्रोजेक्ट के नाम पर डबल क्लिक करें. टारगेट सेक्शन से अपना ऐप्लिकेशन चुनें. इसके बाद, जानकारी टैब चुनें और यूआरएल टाइप सेक्शन को बड़ा करें.
- + बटन पर क्लिक करें और कोड में बदले गए ऐप्लिकेशन आईडी को यूआरएल के तौर पर जोड़ें
स्कीम. आपको एन्कोड किया गया ऐप्लिकेशन आईडी, Firebase कंसोल के सामान्य सेटिंग पेज पर, अपने iOS ऐप्लिकेशन के सेक्शन में दिखेगा. अन्य फ़ील्ड खाली छोड़ दें.
कॉन्फ़िगरेशन पूरा होने के बाद, यह कुछ ऐसा दिखेगा (हालांकि, इसमें आपके ऐप्लिकेशन के हिसाब से वैल्यू होंगी):
सेवा देने वाली कंपनी के आईडी github.com का इस्तेमाल करके, OAuthProvider का एक इंस्टेंस बनाएं.
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 दस्तावेज़ देखें. ध्यान दें कि
setCustomParameters
के साथ, Firebase के लिए ज़रूरी पैरामीटर पास नहीं किए जा सकते. ये पैरामीटर client_id, redirect_uri, response_type, scope, और state हैं.ज़रूरी नहीं: बुनियादी प्रोफ़ाइल के अलावा, OAuth 2.0 के अन्य स्कोप की जानकारी दें. इन स्कोप के लिए, आपको पुष्टि करने वाली सेवा देने वाली कंपनी से अनुरोध करना है. अगर आपके ऐप्लिकेशन को GitHub के एपीआई से उपयोगकर्ता के निजी डेटा को ऐक्सेस करने की ज़रूरत है, तो आपको GitHub डेवलपर कंसोल में एपीआई की अनुमतियां सेक्शन में जाकर, 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 के स्कोप के दस्तावेज़ देखें.
ज़रूरी नहीं: अगर आपको उपयोगकर्ता को reCAPTCHA दिखाते समय, अपने ऐप्लिकेशन में
SFSafariViewController
याUIWebView
को अपनी पसंद के मुताबिक दिखाना है, तो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 को कॉल किया जा सकता है.
उदाहरण के लिए, प्रोफ़ाइल की बुनियादी जानकारी पाने के लिए,
Authorization
हेडर में ऐक्सेस टोकन पास करके, REST API को कॉल किया जा सकता है: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; }
पुष्टि करने से जुड़ी सभी गड़बड़ियों के लिए, गड़बड़ी को हैंडल करने वाला कोड भी जोड़ा जा सकता है. गड़बड़ियां मैनेज करना लेख पढ़ें.