आप अपने उपयोगकर्ताओं को उनके Google खातों का उपयोग करके Firebase से प्रमाणित करने दे सकते हैं।
शुरू करने से पहले
यदि आपने पहले से ऐसा नहीं किया है, तो अपने Android प्रोजेक्ट में Firebase जोड़ें ।
फायरबेस एंड्रॉइड बीओएम का उपयोग करके, अपने मॉड्यूल (ऐप-लेवल) ग्रैडल फ़ाइल (आमतौर पर
app/build.gradle
) में फायरबेस ऑथेंटिकेशन एंड्रॉइड लाइब्रेरी के लिए निर्भरता की घोषणा करें।साथ ही, Firebase प्रमाणीकरण सेट अप करने के भाग के रूप में, आपको अपने ऐप्लिकेशन में Google Play सेवाएं SDK जोड़ना होगा।
Java
dependencies { // Import the BoM for the Firebase platform implementation platform('com.google.firebase:firebase-bom:30.1.0') // Declare the dependency for the Firebase Authentication library // When using the BoM, you don't specify versions in Firebase library dependencies implementation 'com.google.firebase:firebase-auth'
// Also declare the dependency for the Google Play services library and specify its version implementation 'com.google.android.gms:play-services-auth:20.2.0' }फायरबेस एंड्रॉइड बीओएम का उपयोग करके, आपका ऐप हमेशा फायरबेस एंड्रॉइड लाइब्रेरी के संगत संस्करणों का उपयोग करेगा।
(वैकल्पिक) BoM . का उपयोग किए बिना फायरबेस लाइब्रेरी निर्भरता घोषित करें
यदि आप फायरबेस बीओएम का उपयोग नहीं करना चुनते हैं, तो आपको प्रत्येक फायरबेस लाइब्रेरी संस्करण को उसकी निर्भरता रेखा में निर्दिष्ट करना होगा।
ध्यान दें कि यदि आप अपने ऐप में एकाधिक फायरबेस लाइब्रेरी का उपयोग करते हैं, तो हम लाइब्रेरी संस्करणों को प्रबंधित करने के लिए BoM का उपयोग करने की अत्यधिक अनुशंसा करते हैं, जो सुनिश्चित करता है कि सभी संस्करण संगत हैं।
dependencies { // Declare the dependency for the Firebase Authentication library // When NOT using the BoM, you must specify versions in Firebase library dependencies implementation 'com.google.firebase:firebase-auth:21.0.6'
// Also declare the dependency for the Google Play services library and specify its version implementation 'com.google.android.gms:play-services-auth:20.2.0' }Kotlin+KTX
dependencies { // Import the BoM for the Firebase platform implementation platform('com.google.firebase:firebase-bom:30.1.0') // Declare the dependency for the Firebase Authentication library // When using the BoM, you don't specify versions in Firebase library dependencies implementation 'com.google.firebase:firebase-auth-ktx'
// Also declare the dependency for the Google Play services library and specify its version implementation 'com.google.android.gms:play-services-auth:20.2.0' }फायरबेस एंड्रॉइड बीओएम का उपयोग करके, आपका ऐप हमेशा फायरबेस एंड्रॉइड लाइब्रेरी के संगत संस्करणों का उपयोग करेगा।
(वैकल्पिक) BoM . का उपयोग किए बिना फायरबेस लाइब्रेरी निर्भरता घोषित करें
यदि आप फायरबेस बीओएम का उपयोग नहीं करना चुनते हैं, तो आपको प्रत्येक फायरबेस लाइब्रेरी संस्करण को उसकी निर्भरता रेखा में निर्दिष्ट करना होगा।
ध्यान दें कि यदि आप अपने ऐप में एकाधिक फायरबेस लाइब्रेरी का उपयोग करते हैं, तो हम लाइब्रेरी संस्करणों को प्रबंधित करने के लिए BoM का उपयोग करने की अत्यधिक अनुशंसा करते हैं, जो सुनिश्चित करता है कि सभी संस्करण संगत हैं।
dependencies { // Declare the dependency for the Firebase Authentication library // When NOT using the BoM, you must specify versions in Firebase library dependencies implementation 'com.google.firebase:firebase-auth-ktx:21.0.6'
// Also declare the dependency for the Google Play services library and specify its version implementation 'com.google.android.gms:play-services-auth:20.2.0' }यदि आपने अभी तक अपने ऐप का SHA-1 फ़िंगरप्रिंट निर्दिष्ट नहीं किया है, तो ऐसा Firebase कंसोल के सेटिंग पृष्ठ से करें। अपने ऐप का SHA-1 फ़िंगरप्रिंट कैसे प्राप्त करें, इसके विवरण के लिए अपने क्लाइंट को प्रमाणित करना देखें।
- Google को Firebase कंसोल में साइन-इन विधि के रूप में सक्षम करें:
- फायरबेस कंसोल में, प्रामाणिक अनुभाग खोलें।
- साइन इन विधि टैब पर, Google साइन-इन विधि को सक्षम करें और सहेजें पर क्लिक करें।
फायरबेस के साथ प्रमाणित करें
- उपयोगकर्ताओं को उनके सहेजे गए क्रेडेंशियल पृष्ठ के साथ साइन इन करने के चरणों का पालन करके Google One Tap साइन-इन को अपने ऐप में एकीकृत करें। जब आप
BeginSignInRequest
ऑब्जेक्ट को कॉन्फ़िगर करते हैं, तोsetGoogleIdTokenRequestOptions
पर कॉल करें:आपको अपना "सर्वर" क्लाइंट आईडीJava
signInRequest = BeginSignInRequest.builder() .setGoogleIdTokenRequestOptions(GoogleIdTokenRequestOptions.builder() .setSupported(true) // Your server's client ID, not your Android client ID. .setServerClientId(getString(R.string.default_web_client_id)) // Only show accounts previously used to sign in. .setFilterByAuthorizedAccounts(true) .build()) .build();
Kotlin+KTX
signInRequest = BeginSignInRequest.builder() .setGoogleIdTokenRequestOptions( BeginSignInRequest.GoogleIdTokenRequestOptions.builder() .setSupported(true) // Your server's client ID, not your Android client ID. .setServerClientId(getString(R.string.your_web_client_id)) // Only show accounts previously used to sign in. .setFilterByAuthorizedAccounts(true) .build()) .build()
setGoogleIdTokenRequestOptions
विधि में पास करना होगा। OAuth 2.0 क्लाइंट आईडी खोजने के लिए:- GCP कंसोल में क्रेडेंशियल पेज खोलें।
- वेब एप्लिकेशन प्रकार क्लाइंट आईडी आपके बैकएंड सर्वर का OAuth 2.0 क्लाइंट आईडी है।
Java
public class YourActivity extends AppCompatActivity { // ... private static final int REQ_ONE_TAP = 2; // Can be any integer unique to the Activity. private boolean showOneTapUI = true; // ... @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case REQ_ONE_TAP: try { SignInCredential credential = oneTapClient.getSignInCredentialFromIntent(data); String idToken = credential.getGoogleIdToken(); if (idToken != null) { // Got an ID token from Google. Use it to authenticate // with Firebase. Log.d(TAG, "Got ID token."); } } catch (ApiException e) { // ... } break; } } }
Kotlin+KTX
class YourActivity : AppCompatActivity() { // ... private val REQ_ONE_TAP = 2 // Can be any integer unique to the Activity private var showOneTapUI = true // ... override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) when (requestCode) { REQ_ONE_TAP -> { try { val credential = oneTapClient.getSignInCredentialFromIntent(data) val idToken = credential.googleIdToken when { idToken != null -> { // Got an ID token from Google. Use it to authenticate // with Firebase. Log.d(TAG, "Got ID token.") } else -> { // Shouldn't happen. Log.d(TAG, "No ID token!") } } } catch (e: ApiException) { // ... } } } // ... }
- अपनी साइन-इन गतिविधि की
onCreate
विधि में,FirebaseAuth
ऑब्जेक्ट का साझा इंस्टेंस प्राप्त करें:Java
private FirebaseAuth mAuth; // ... // Initialize Firebase Auth mAuth = FirebaseAuth.getInstance();
Kotlin+KTX
private lateinit var auth: FirebaseAuth // ... // Initialize Firebase Auth auth = Firebase.auth
- अपनी गतिविधि प्रारंभ करते समय, यह देखने के लिए जांचें कि उपयोगकर्ता वर्तमान में साइन इन है या नहीं:
Java
@Override public void onStart() { super.onStart(); // Check if user is signed in (non-null) and update UI accordingly. FirebaseUser currentUser = mAuth.getCurrentUser(); updateUI(currentUser); }
Kotlin+KTX
override fun onStart() { super.onStart() // Check if user is signed in (non-null) and update UI accordingly. val currentUser = auth.currentUser updateUI(currentUser) }
- अपने
onActivityResult()
हैंडलर में (चरण 1 देखें), उपयोगकर्ता का Google ID टोकन प्राप्त करें, इसे Firebase क्रेडेंशियल के लिए एक्सचेंज करें, और Firebase क्रेडेंशियल का उपयोग करके Firebase से प्रमाणित करें:यदिJava
SignInCredential googleCredential = oneTapClient.getSignInCredentialFromIntent(data); String idToken = googleCredential.getGoogleIdToken(); if (idToken != null) { // Got an ID token from Google. Use it to authenticate // with Firebase. AuthCredential firebaseCredential = GoogleAuthProvider.getCredential(idToken, null); mAuth.signInWithCredential(firebaseCredential) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if (task.isSuccessful()) { // Sign in success, update UI with the signed-in user's information Log.d(TAG, "signInWithCredential:success"); FirebaseUser user = mAuth.getCurrentUser(); updateUI(user); } else { // If sign in fails, display a message to the user. Log.w(TAG, "signInWithCredential:failure", task.getException()); updateUI(null); } } }); }
Kotlin+KTX
val googleCredential = oneTapClient.getSignInCredentialFromIntent(data) val idToken = googleCredential.googleIdToken when { idToken != null -> { // Got an ID token from Google. Use it to authenticate // with Firebase. val firebaseCredential = GoogleAuthProvider.getCredential(idToken, null) auth.signInWithCredential(firebaseCredential) .addOnCompleteListener(this) { task -> if (task.isSuccessful) { // Sign in success, update UI with the signed-in user's information Log.d(TAG, "signInWithCredential:success") val user = auth.currentUser updateUI(user) } else { // If sign in fails, display a message to the user. Log.w(TAG, "signInWithCredential:failure", task.exception) updateUI(null) } } } else -> { // Shouldn't happen. Log.d(TAG, "No ID token!") } }
signInWithCredential
की कॉल सफल होती है तो आप उपयोगकर्ता के खाते का डेटा प्राप्त करने के लिएgetCurrentUser
विधि का उपयोग कर सकते हैं।
अगले कदम
उपयोगकर्ता द्वारा पहली बार साइन इन करने के बाद, एक नया उपयोगकर्ता खाता बनाया जाता है और क्रेडेंशियल से लिंक किया जाता है—अर्थात, उपयोगकर्ता नाम और पासवर्ड, फ़ोन नंबर, या प्रमाणीकरण प्रदाता जानकारी—जिससे उपयोगकर्ता ने साइन इन किया है। यह नया खाता आपके फ़ायरबेस प्रोजेक्ट के हिस्से के रूप में संग्रहीत किया जाता है, और इसका उपयोग आपके प्रोजेक्ट में प्रत्येक ऐप में उपयोगकर्ता की पहचान करने के लिए किया जा सकता है, भले ही उपयोगकर्ता कैसे साइन इन करे।
अपने ऐप्स में, आप उपयोगकर्ता की मूलभूत प्रोफ़ाइल जानकारी
FirebaseUser
ऑब्जेक्ट से प्राप्त कर सकते हैं। उपयोगकर्ता प्रबंधित करें देखें।अपने फायरबेस रीयलटाइम डेटाबेस और क्लाउड स्टोरेज सुरक्षा नियमों में, आप
auth
चर से साइन-इन किए गए उपयोगकर्ता की अद्वितीय उपयोगकर्ता आईडी प्राप्त कर सकते हैं, और इसका उपयोग यह नियंत्रित करने के लिए कर सकते हैं कि उपयोगकर्ता किस डेटा तक पहुंच सकता है।
किसी उपयोगकर्ता को साइन आउट करने के लिए, signOut
पर कॉल करें:
Java
FirebaseAuth.getInstance().signOut();
Kotlin+KTX
Firebase.auth.signOut()