Google Authentication in Android App
Google Authentication in Android App
Complete Tutorial in Hindi — LearnWithRehan
1. Google Authentication क्या होता है?
Google Authentication एक ऐसी process है, जिसकी मदद से user अपने Google Account का इस्तेमाल करके Android application में login कर सकता है।
इसमें user को app के लिए अलग username और password बनाने की जरूरत नहीं होती। वह Google account चुनता है और app authentication service के माध्यम से उसकी identity verify करता है।
Example:
मान लीजिए आपने एक Android app बनाया है। उसमें दो options हैं:
Login with Email and Password
Sign in with Google
जब user Sign in with Google पर click करेगा, तो Google account चुनने का interface दिखाई देगा। User account select करेगा और authentication सफल होने पर app उसे Home Screen पर भेज देगा।
2. Valid Gmail और Google Authentication में अंतर
यह बहुत महत्वपूर्ण concept है।
सिर्फ email का format सही होना यह साबित नहीं करता कि account असली है।
उदाहरण:
rehan123@gmail.com
यह एक valid-looking email format है, लेकिन केवल इसका format देखकर यह verify नहीं किया जा सकता कि account मौजूद है या user उसका मालिक है।
Google Authentication में Google account की identity verify करता है। Firebase Authentication इस authentication result के आधार पर user को signed-in मानता है।
याद रखें: Google sign-in का मतलब सिर्फ Gmail text check करना नहीं है। यह वास्तविक account authentication है।
Google sign-in के लिए Google Account जरूरी है। हर Google Account का address @gmail.com पर खत्म हो, यह जरूरी नहीं है; कुछ accounts में दूसरी email address भी इस्तेमाल हो सकती है।
3. Google Authentication के लिए क्या-क्या चाहिए?
Android Studio
Java programming language
XML layout
Firebase project
Firebase Authentication
Google sign-in provider
Credential Manager
Google ID library
Android app का सही package name
SHA-1 certificate fingerprint
इन सभी components को सही तरीके से configure करने पर Google login implement किया जा सकता है।
4. Google Login का Working Flow
Google Authentication का flow इस प्रकार होता है:
Step 1: User Android app खोलता है।
Step 2: Login Screen दिखाई देती है।
Step 3: User Sign in with Google button पर click करता है।
Step 4: Credential Manager Google account select करने का interface दिखाता है।
Step 5: User अपना Google account select करता है।
Step 6: Google authentication credential या ID token देता है।
Step 7: App उस credential को Firebase Authentication के साथ verify कराता है।
Step 8: Authentication सफल होने पर Firebase user information उपलब्ध कराता है।
Step 9: App user को Home Screen पर भेज देता है।
अगर authentication fail हो जाता है, तो user को error message दिखाया जाता है और उसे login screen पर ही रखा जाता है।
5. Step 1 — Firebase Project बनाना
सबसे पहले Firebase Console खोलें:
https://console.firebase.google.com/
इसके बाद:
Google account से login करें।
Create a Project पर click करें।
Project का नाम डालें, जैसे
LearnWithRehanLogin।Firebase project create करें।
Android app add करें।
Android app का सही package name डालें।
Register App पर click करें।
google-services.jsonfile download करें।इस file को Android Studio के
appfolder में रखें।
Example:
MyProject/app/google-services.json
ध्यान दें कि Firebase में दिया गया package name और Android app का application ID एक-दूसरे से match होना चाहिए।
Official guide:
https://firebase.google.com/docs/android/setup
6. Step 2 — Firebase Authentication Enable करना
Firebase Console में अपना project खोलें।
अब:
Authentication section खोलें।
Sign-in method या Sign-in providers पर जाएँ।
Google provider चुनें।
Google sign-in को Enable करें।
Project की support email configure करें।
Changes को Save करें।
अब Firebase project में Google Authentication provider enable हो चुका है।
लेकिन सिर्फ provider enable करना पर्याप्त नहीं है। Android app में भी sign-in flow implement करना होगा।
Official guide:
https://firebase.google.com/docs/auth/android/google-signin
7. Step 3 — SHA-1 Fingerprint Configure करना
Android app की Google authentication configuration में SHA-1 certificate fingerprint की जरूरत पड़ सकती है।
Android Studio के Terminal में यह command चलाकर Gradle signing report देख सकते हैं:
gradlew signingReportWindows पर project directory में यह command चलाएँ।
Output में अपने app variant का SHA-1 fingerprint देखें।
इसके बाद:
Firebase Console खोलें।
Project Settings में जाएँ।
Your apps के अंतर्गत Android app चुनें।
SHA certificate fingerprints section में SHA-1 add करें।
Configuration के बाद जरूरत पड़ने पर updated
google-services.jsonडाउनलोड करें।
Debug और release builds के signing certificates अलग हो सकते हैं। Production में release certificate का fingerprint भी configure करना पड़ सकता है।
8. Step 4 — Android Studio में Dependencies जोड़ना
अब Android Studio में app-level Gradle file खोलें।
Firebase Authentication और Credential Manager के लिए आवश्यक dependencies configure करें।
Official Firebase documentation में दिए गए उदाहरण के अनुसार dependencies में ये libraries शामिल होती हैं:
Firebase Authentication
AndroidX Credentials
Credentials Play Services Authentication
Google ID library
Groovy Gradle का उदाहरण:
dependencies {
implementation platform(
"com.google.firebase:firebase-bom:VERSION"
)
implementation "com.google.firebase:firebase-auth"
implementation "androidx.credentials:credentials:VERSION"
implementation "androidx.credentials:credentials-play-services-auth:VERSION"
implementation "com.google.android.libraries.identity.googleid:googleid:VERSION"
}यह configuration का उदाहरण है। VERSION को संबंधित official documentation में दिए गए compatible versions से बदलें। Firebase BoM की version Firebase libraries को manage करती है; Credential Manager और Google ID libraries की अपनी versions होती हैं।
इसके बाद Gradle Sync करें।
9. Step 5 — Login Screen बनाना
अब XML में Login Screen बनाएँ।
Login Screen में ये elements रख सकते हैं:
App logo
Welcome message
Sign in with Google button
Progress indicator
Error message या status text
Example UI text:
Welcome to LearnWithRehan App
Continue with your Google Account.
[ Sign in with Google ]
जब user button पर click करेगा, तो Java code Credential Manager के माध्यम से Google sign-in request शुरू करेगा।
Google की current recommended Android implementation में Credential Manager का उपयोग किया जाता है।
Documentation:
https://developer.android.com/identity/sign-in/credential-manager-siwg-implementation
10. Step 6 — Credential Manager से Google Account चुनना
अब Java code में Credential Manager configure किया जाता है।
इसका काम:
Google ID sign-in request बनाना।
Credential Manager request तैयार करना।
Google account selection flow शुरू करना।
Selected account से प्राप्त credential को handle करना।
Errors और account cancellation को संभालना।
Google account select होने पर app को Google ID token मिल सकता है।
यह token authentication process का एक महत्वपूर्ण हिस्सा है।
ध्यान दें: अगर पहले से authorized account उपलब्ध नहीं है, तो app को सभी उपलब्ध Google accounts चुनने का विकल्प भी देना चाहिए। केवल previously authorized accounts वाला filter इस्तेमाल करने पर नए user को account selection न मिल पाए।
11. Step 7 — Firebase Authentication से Login करना
Google ID token मिलने के बाद Firebase Authentication को credential दिया जाता है।
Firebase Java API में authentication का मूल concept इस प्रकार है:
AuthCredential credential =
GoogleAuthProvider.getCredential(idToken, null);
FirebaseAuth.getInstance()
.signInWithCredential(credential)
.addOnCompleteListener(this, task -> {
if (task.isSuccessful()) {
FirebaseUser user =
FirebaseAuth.getInstance().getCurrentUser();
// Authentication successful.
// Open Home Screen here.
} else {
// Authentication failed.
// Show an error message.
}
});महत्वपूर्ण: यह authentication flow का एक हिस्सा है, पूरा working login project नहीं। idToken को Credential Manager से सही तरीके से प्राप्त करना, dependencies configure करना और error handling implement करना भी जरूरी है।
Authentication सफल होने पर Firebase user object से नाम, email और UID जैसी जानकारी प्राप्त की जा सकती है।
12. Step 8 — केवल Successful Login पर Home Screen खोलना
Authentication सफल होने पर ही user को Home Screen पर भेजें।
उदाहरण के लिए:
LoginActivity — Google login screen
HomeActivity — App की home screen
जब Firebase authentication सफल हो, तब HomeActivity start करें और LoginActivity को back stack से हटाने के लिए उचित navigation flags लगाएँ।
यदि login fail हो जाता है, तो HomeActivity open नहीं होनी चाहिए।
13. Step 9 — User का नाम और Email प्राप्त करना
Firebase Authentication सफल होने पर user information प्राप्त कर सकते हैं।
Java में:
FirebaseUser user =
FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
String name = user.getDisplayName();
String email = user.getEmail();
String uid = user.getUid();
}इन values का उपयोग Home Screen पर welcome message दिखाने के लिए किया जा सकता है।
Example:
Welcome, Rehan!
Email: rehan@example.com
यह केवल उदाहरण है; वास्तविक values selected account से प्राप्त होंगी।
14. Step 10 — App को दोबारा खोलने पर Login Check करना
हर बार app खुलने पर user से दोबारा login करवाना जरूरी नहीं है।
Firebase Authentication की current session check कर सकते हैं।
FirebaseUser user =
FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
// User already signed in.
// Open Home Screen.
} else {
// No signed-in user.
// Open Login Screen.
}यह check Activity के appropriate lifecycle point पर करना चाहिए।
यदि user sign out कर देता है, तो उसे वापस Login Screen पर भेजें।
15. Step 11 — Logout कैसे करें?
Firebase Authentication से sign out करने के लिए:
FirebaseAuth.getInstance().signOut();इससे Firebase की current authentication session sign out हो जाती है।
यदि आपने Credential Manager के माध्यम से Google sign-in implement किया है, तो जरूरत के अनुसार Credential Manager की credential state भी clear करें। इससे भविष्य में account selection का अनुभव बेहतर हो सकता है।
16. Google Login में होने वाली Common Problems
Problem 1: Google account selection नहीं खुल रहा
Credential Manager dependencies, device पर Google account, Play Services और error logs check करें।
Problem 2: Firebase Authentication failed
Google provider enable है या नहीं, package name सही है या नहीं और Firebase configuration check करें।
Problem 3: Developer error या OAuth error
OAuth client configuration, package name, SHA fingerprints और Web Client ID की configuration verify करें।
Problem 4: User login के बाद Home Screen पर नहीं जा रहा
signInWithCredential() का result check करें। Navigation केवल successful authentication पर करें।
Problem 5: Invalid email को लेकर confusion
Email का format validate करना वास्तविक Google authentication का विकल्प नहीं है। Google account की identity authentication provider से verify कराएँ।
Problem 6: किसी भी email को allow करना है या सिर्फ Gmail को?
Google Authentication Google Account की identity verify करता है। यदि आपके business rule के अनुसार सिर्फ @gmail.com address allow करना है, तो authenticated email मिलने के बाद domain restriction अलग से लागू कर सकते हैं। लेकिन domain check अकेले identity verification नहीं है।
17. Security के Important Rules
केवल email string देखकर user को authenticated न मानें।
Authentication fail होने पर user को login न कराएँ।
Firebase ID token या Google ID token को plain email से replace न करें।
Sensitive backend APIs में Firebase ID token की server-side verification करें।
Password, private keys या service-account credentials को Android app में hardcode न करें।
User को sign out करने का विकल्प दें।
App की permissions और user data को सुरक्षित रखें।
18. Final Revision
Google Authentication का पूरा flow याद रखें:
Firebase Project → Google Provider Enable → SHA Configuration → Gradle Dependencies → Credential Manager → Google Account Selection → Firebase Authentication → Home Screen.
इस process को सही तरीके से implement करने पर Android application में Google account से login कराया जा सकता है।
19. Conclusion
इस tutorial में हमने सीखा कि Android Studio में Java और XML की मदद से Google Authentication कैसे implement किया जाता है।
हमने Firebase project setup, Google provider, SHA-1 fingerprint, Credential Manager, Firebase Authentication, user information, session checking और logout के concepts समझे।
अगर आप Android Development, Java, Flutter और Firebase के आसान Hindi tutorials सीखना चाहते हैं, तो LearnWithRehan को subscribe करें।
LearnWithRehan — Learn Coding Step by Step!
Comments
Post a Comment