Apple प्लैटफ़ॉर्म पर Facebook लॉगिन का इस्तेमाल करके पुष्टि करें (original) (raw)

सीधे मुख्य कॉन्टेंट पर जाएं

अपने ऐप्लिकेशन में Facebook Login या Facebook Limited Login को इंटिग्रेट करके, उपयोगकर्ताओं को Firebase के साथ अपने Facebook खातों से पुष्टि करने की अनुमति दी जा सकती है.

शुरू करने से पहले

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

  1. Xcode में, अपना ऐप्लिकेशन प्रोजेक्ट खोलें. इसके बाद, File > Add Packages पर जाएं.
  2. जब आपसे कहा जाए, तब Firebase Apple प्लैटफ़ॉर्म SDK टूल की रिपॉज़िटरी जोड़ें:

https://github.com/firebase/firebase-ios-sdk.git

  1. Firebase Authentication लाइब्रेरी चुनें.
  2. टारगेट की बिल्ड सेटिंग के Other Linker Flags सेक्शन में -ObjC फ़्लैग जोड़ें.
  3. इसके बाद, Xcode आपके पैकेज की डिपेंडेंसी से जुड़ी समस्या को हल करना शुरू कर देगा और उन्हें बैकग्राउंड में डाउनलोड करेगा.

इसके बाद, कॉन्फ़िगरेशन से जुड़े कुछ चरण पूरे करें:

  1. Facebook for Developers साइट पर जाकर, अपने ऐप्लिकेशन के लिए ऐप्लिकेशन आईडी और ऐप्लिकेशन सीक्रेट पाएं.
  2. Facebook से लॉगिन करने की सुविधा चालू करें:
    1. Firebase कंसोल में, Authentication सेक्शन खोलें.
    2. साइन इन करने का तरीका टैब पर, Facebook से साइन इन करने का तरीका चालू करें. साथ ही, Facebook से मिले ऐप्लिकेशन आईडी और ऐप्लिकेशन सीक्रेट की जानकारी दें.
    3. इसके बाद, पक्का करें कि आपका OAuth रीडायरेक्ट यूआरआई (जैसे कि my-app-12345.firebaseapp.com/__/auth/handler), Facebook for Developers साइट पर, Facebook ऐप्लिकेशन के सेटिंग पेज में, OAuth रीडायरेक्ट यूआरआई के तौर पर शामिल हो. इसके लिए, Product Settings > Facebook Login कॉन्फ़िगरेशन में जाएं.

Facebook Login लागू करना

"क्लासिक" Facebook Login का इस्तेमाल करने के लिए, यह तरीका अपनाएं. इसके अलावा, अगले सेक्शन में दिखाए गए तरीके से, Facebook Limited Login का इस्तेमाल किया जा सकता है.

  1. डेवलपर के दस्तावेज़ में दिए गए निर्देशों का पालन करके, Facebook Login को अपने ऐप्लिकेशन में इंटिग्रेट करें. FBSDKLoginButton ऑब्जेक्ट को शुरू करते समय, लॉगिन और लॉगआउट इवेंट पाने के लिए एक डेलिगेट सेट करें. उदाहरण के लिए:

Swift

let loginButton = FBSDKLoginButton()
loginButton.delegate = self

Objective-C

FBSDKLoginButton *loginButton = [[FBSDKLoginButton alloc] init];
loginButton.delegate = self;
आपने जिसे ईमेल खाते का ऐक्सेस दिया है उसके खाते में, didCompleteWithResult:error: लागू करें.

Swift

func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
if let error = error {
print(error.localizedDescription)
return
}
// ...
}

Objective-C

  1. अपने UIApplicationDelegate में FirebaseCore मॉड्यूल इंपोर्ट करें. साथ ही, अपने ऐप्लिकेशन डेलिगेट में इस्तेमाल किए जाने वाले अन्य Firebase मॉड्यूल भी इंपोर्ट करें. उदाहरण के लिए, Cloud Firestore और Authentication का इस्तेमाल करने के लिए:

SwiftUI

import SwiftUI
import FirebaseCore
import FirebaseFirestore
import FirebaseAuth
// ...

Swift

import FirebaseCore
import FirebaseFirestore
import FirebaseAuth
// ...

Objective-C

@import FirebaseCore;
@import FirebaseFirestore;
@import FirebaseAuth;
// ... 3. अपने ऐप्लिकेशन डेलिगेट के application(_:didFinishLaunchingWithOptions:) तरीके में, शेयर किए गए इंस्टेंस FirebaseApp को कॉन्फ़िगर करें:

SwiftUI

// Use Firebase library to configure APIs
FirebaseApp.configure()

Swift

// Use Firebase library to configure APIs
FirebaseApp.configure()

Objective-C

// Use Firebase library to configure APIs
[FIRApp configure]; 4. अगर SwiftUI का इस्तेमाल किया जा रहा है, तो आपको एक ऐप्लिकेशन डेलिगेट बनाना होगा. साथ ही, इसे UIApplicationDelegateAdaptor या NSApplicationDelegateAdaptor के ज़रिए अपनी App स्ट्रक्चर से जोड़ना होगा. आपको ऐप्लिकेशन के डेलिगेट स्विज़लिंग को भी बंद करना होगा. ज़्यादा जानकारी के लिए, SwiftUI के निर्देश देखें.

SwiftUI

@main
struct YourApp: App {
// register app delegate for Firebase setup
@UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
var body: some Scene {
WindowGroup {
NavigationView {
ContentView()
}
}
}
} 5. जब कोई उपयोगकर्ता साइन इन कर लेता है, तो didCompleteWithResult:error: को लागू करने के दौरान, साइन इन किए गए उपयोगकर्ता के लिए ऐक्सेस टोकन पाएं. इसके बाद, इसे Firebase क्रेडेंशियल के लिए बदलें:

Swift

let credential = FacebookAuthProvider
.credential(withAccessToken: AccessToken.current!.tokenString)

Objective-C

FIRAuthCredential *credential = [FIRFacebookAuthProvider
credentialWithAccessToken:[FBSDKAccessToken currentAccessToken].tokenString];

Facebook Limited Login को लागू करना

"क्लासिक" Facebook Login के बजाय Facebook Limited Login का इस्तेमाल करने के लिए, यह तरीका अपनाएं.

  1. डेवलपर के दस्तावेज़ में दिए गए निर्देशों का पालन करके, Facebook Limited Login को अपने ऐप्लिकेशन में इंटिग्रेट करें.
  2. साइन इन करने के हर अनुरोध के लिए, एक यूनीक रैंडम स्ट्रिंग जनरेट करें. इसे "नॉन्स" कहा जाता है. इसका इस्तेमाल यह पक्का करने के लिए किया जाता है कि आपको मिला आईडी टोकन, आपके ऐप्लिकेशन के पुष्टि करने के अनुरोध के जवाब में ही दिया गया हो. यह चरण, रीप्ले अटैक को रोकने के लिए ज़रूरी है. यहां दिए गए उदाहरण की तरह, SecRandomCopyBytes(_:_:_) की मदद से क्रिप्टोग्राफ़िक रूप से सुरक्षित नॉनस जनरेट किया जा सकता है:

Swift

private func randomNonceString(length: Int = 32) -> String {
precondition(length > 0)
var randomBytes = [UInt8](repeating: 0, count: length)
let errorCode = SecRandomCopyBytes(kSecRandomDefault, randomBytes.count, &randomBytes)
if errorCode != errSecSuccess {
fatalError(
"Unable to generate nonce. SecRandomCopyBytes failed with OSStatus (errorCode)"
)
}
let charset: [Character] =
Array("0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._")
let nonce = randomBytes.map { byte in
// Pick a random character from the set, wrapping around if needed.
charset[Int(byte) % charset.count]
}
return String(nonce)
}
#### Objective-C
// Adapted from https://auth0.com/docs/api-auth/tutorials/nonce#generate-a-cryptographically-random-nonce

आपको साइन-इन करने के अनुरोध के साथ, नॉन्स का SHA-256 हैश भेजना होगा. Facebook, जवाब में इसे बिना किसी बदलाव के पास करेगा. Firebase, जवाब की पुष्टि करता है. इसके लिए, वह ओरिजनल नॉनस को हैश करता है और उसकी तुलना Facebook से मिली वैल्यू से करता है.

Swift

@available(iOS 13, *)
private func sha256(_ input: String) -> String {
let inputData = Data(input.utf8)
let hashedData = SHA256.hash(data: inputData)
let hashString = hashedData.compactMap {
String(format: "%02x", $0)
}.joined()
return hashString
}
#### Objective-C

  1. FBSDKLoginButton सेट अप करते समय, लॉगिन और लॉग आउट इवेंट पाने के लिए किसी प्रतिनिधि को सेट करें. साथ ही, ट्रैकिंग मोड को FBSDKLoginTrackingLimited पर सेट करें और एक नॉनस अटैच करें. उदाहरण के लिए:

Swift

func setupLoginButton() {
let nonce = randomNonceString()
currentNonce = nonce
loginButton.delegate = self
loginButton.loginTracking = .limited
loginButton.nonce = sha256(nonce)
}

Objective-C

आपने जिसे ईमेल खाते का ऐक्सेस दिया है उसके खाते में, didCompleteWithResult:error: लागू करें.

Swift

func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
if let error = error {
print(error.localizedDescription)
return
}
// ...
}

Objective-C

  1. अपने UIApplicationDelegate में FirebaseCore मॉड्यूल इंपोर्ट करें. साथ ही, अपने ऐप्लिकेशन डेलिगेट में इस्तेमाल किए जाने वाले अन्य Firebase मॉड्यूल भी इंपोर्ट करें. उदाहरण के लिए, Cloud Firestore और Authentication का इस्तेमाल करने के लिए:

SwiftUI

import SwiftUI
import FirebaseCore
import FirebaseFirestore
import FirebaseAuth
// ...

Swift

import FirebaseCore
import FirebaseFirestore
import FirebaseAuth
// ...

Objective-C

@import FirebaseCore;
@import FirebaseFirestore;
@import FirebaseAuth;
// ... 5. अपने ऐप्लिकेशन डेलिगेट के application(_:didFinishLaunchingWithOptions:) तरीके में, शेयर किए गए इंस्टेंस FirebaseApp को कॉन्फ़िगर करें:

SwiftUI

// Use Firebase library to configure APIs
FirebaseApp.configure()

Swift

// Use Firebase library to configure APIs
FirebaseApp.configure()

Objective-C

// Use Firebase library to configure APIs
[FIRApp configure]; 6. अगर SwiftUI का इस्तेमाल किया जा रहा है, तो आपको एक ऐप्लिकेशन डेलिगेट बनाना होगा. साथ ही, इसे UIApplicationDelegateAdaptor या NSApplicationDelegateAdaptor के ज़रिए अपनी App स्ट्रक्चर से जोड़ना होगा. आपको ऐप्लिकेशन के डेलिगेट स्विज़लिंग को भी बंद करना होगा. ज़्यादा जानकारी के लिए, SwiftUI के निर्देश देखें.

SwiftUI

@main
struct YourApp: App {
// register app delegate for Firebase setup
@UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
var body: some Scene {
WindowGroup {
NavigationView {
ContentView()
}
}
}
} 7. जब कोई उपयोगकर्ता साइन इन कर लेता है, तब didCompleteWithResult:error: को लागू करने के दौरान, Firebase क्रेडेंशियल पाने के लिए, Facebook से मिले जवाब में मौजूद आईडी टोकन का इस्तेमाल करें. इसके साथ, बिना हैश किया गया नॉन्स भी इस्तेमाल करें:

Swift

// Initialize a Firebase credential.
let idTokenString = AuthenticationToken.current?.tokenString
let nonce = currentNonce
let credential = OAuthProvider.credential(withProviderID: "facebook.com",
idToken: idTokenString!,
rawNonce: nonce)

Objective-C

// Initialize a Firebase credential.
NSString *idTokenString = FBSDKAuthenticationToken.currentAuthenticationToken.tokenString;
NSString *rawNonce = self.currentNonce;
FIROAuthCredential *credential = [FIROAuthProvider credentialWithProviderID:@"facebook.com"
IDToken:idTokenString
rawNonce:rawNonce];

Firebase की मदद से पुष्टि करना

आखिर में, Firebase क्रेडेंशियल का इस्तेमाल करके Firebase से पुष्टि करें:

Swift

Auth.auth().signIn(with: credential) { authResult, error in if let error = error { let authError = error as NSError if isMFAEnabled, authError.code == AuthErrorCode.secondFactorRequired.rawValue { // The user is a multi-factor user. Second factor challenge is required. let resolver = authError .userInfo[AuthErrorUserInfoMultiFactorResolverKey] as! MultiFactorResolver var displayNameString = "" for tmpFactorInfo in resolver.hints { displayNameString += tmpFactorInfo.displayName ?? "" displayNameString += " " } self.showTextInputPrompt( withMessage: "Select factor to sign in\n(displayNameString)", completionBlock: { userPressedOK, displayName in var selectedHint: PhoneMultiFactorInfo? for tmpFactorInfo in resolver.hints { if displayName == tmpFactorInfo.displayName { selectedHint = tmpFactorInfo as? PhoneMultiFactorInfo } } PhoneAuthProvider.provider() .verifyPhoneNumber(with: selectedHint!, uiDelegate: nil, multiFactorSession: resolver .session) { verificationID, error in if error != nil { print( "Multi factor start sign in failed. Error: (error.debugDescription)" ) } else { self.showTextInputPrompt( withMessage: "Verification code for (selectedHint?.displayName ?? "")", completionBlock: { userPressedOK, verificationCode in let credential: PhoneAuthCredential? = PhoneAuthProvider.provider() .credential(withVerificationID: verificationID!, verificationCode: verificationCode!) let assertion: MultiFactorAssertion? = PhoneMultiFactorGenerator .assertion(with: credential!) resolver.resolveSignIn(with: assertion!) { authResult, error in if error != nil { print( "Multi factor finanlize sign in failed. Error: (error.debugDescription)" ) } else { self.navigationController?.popViewController(animated: true) } } } ) } } } ) } else { self.showMessagePrompt(error.localizedDescription) return } // ... return } // User is signed in // ... }

Objective-C

[[FIRAuth auth] signInWithCredential:credential completion:^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) { if (isMFAEnabled && error && error.code == FIRAuthErrorCodeSecondFactorRequired) { FIRMultiFactorResolver *resolver = error.userInfo[FIRAuthErrorUserInfoMultiFactorResolverKey]; NSMutableString *displayNameString = [NSMutableString string]; for (FIRMultiFactorInfo *tmpFactorInfo in resolver.hints) { [displayNameString appendString:tmpFactorInfo.displayName]; [displayNameString appendString:@" "]; } [self showTextInputPromptWithMessage:[NSString stringWithFormat:@"Select factor to sign in\n%@", displayNameString] completionBlock:^(BOOL userPressedOK, NSString _Nullable displayName) { FIRPhoneMultiFactorInfo selectedHint; for (FIRMultiFactorInfo *tmpFactorInfo in resolver.hints) { if ([displayName isEqualToString:tmpFactorInfo.displayName]) { selectedHint = (FIRPhoneMultiFactorInfo *)tmpFactorInfo; } } [FIRPhoneAuthProvider.provider verifyPhoneNumberWithMultiFactorInfo:selectedHint UIDelegate:nil multiFactorSession:resolver.session completion:^(NSString * _Nullable verificationID, NSError * _Nullable error) { if (error) { [self showMessagePrompt:error.localizedDescription]; } else { [self showTextInputPromptWithMessage:[NSString stringWithFormat:@"Verification code for %@", selectedHint.displayName] completionBlock:^(BOOL userPressedOK, NSString *_Nullable verificationCode) { FIRPhoneAuthCredential *credential = [[FIRPhoneAuthProvider provider] credentialWithVerificationID:verificationID verificationCode:verificationCode]; FIRMultiFactorAssertion *assertion = [FIRPhoneMultiFactorGenerator assertionWithCredential:credential]; [resolver resolveSignInWithAssertion:assertion completion:^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) { if (error) { [self showMessagePrompt:error.localizedDescription]; } else { NSLog(@"Multi factor finanlize sign in succeeded."); } }]; }]; } }]; }]; } else if (error) { // ... return; } // User successfully signed in. Get user data from the FIRUser object if (authResult == nil) { return; } FIRUser *user = authResult.user; // ... }];

अगले चरण

जब कोई उपयोगकर्ता पहली बार साइन इन करता है, तो एक नया उपयोगकर्ता खाता बनता है. यह खाता, उन क्रेडेंशियल से लिंक होता है जिनका इस्तेमाल करके उपयोगकर्ता ने साइन इन किया है. जैसे, उपयोगकर्ता का नाम और पासवर्ड, फ़ोन नंबर या पुष्टि करने वाली कंपनी की जानकारी. यह नया खाता, आपके Firebase प्रोजेक्ट के हिस्से के तौर पर सेव किया जाता है. इसका इस्तेमाल, आपके प्रोजेक्ट के हर ऐप्लिकेशन में किसी उपयोगकर्ता की पहचान करने के लिए किया जा सकता है. इससे कोई फ़र्क़ नहीं पड़ता कि उपयोगकर्ता ने किस तरह से साइन इन किया है.

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

किसी उपयोगकर्ता को साइन आउट करने के लिए, 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; }

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

जब तक कुछ अलग से न बताया जाए, तब तक इस पेज की सामग्री को Creative Commons Attribution 4.0 License के तहत और कोड के नमूनों को Apache 2.0 License के तहत लाइसेंस मिला है. ज़्यादा जानकारी के लिए, Google Developers साइट नीतियां देखें. Oracle और/या इससे जुड़ी हुई कंपनियों का, Java एक रजिस्टर किया हुआ ट्रेडमार्क है.

आखिरी बार 2025-12-10 (UTC) को अपडेट किया गया.