Firebase सेटअप और प्रमाणीकरण
अपने Flutter प्रोजेक्ट में Firebase सेट अप करें और ईमेल/पासवर्ड, Google Sign-In तथा अन्य प्रदाताओं से उपयोगकर्ता प्रमाणीकरण लागू करें।
Firebase सेटअप और प्रमाणीकरण, CoddyKit पर Flutter मोबाइल विकास का एक निःशुल्क पाठ है। यह 4 में से 1वाँ पाठ है। इस अध्ययन पथ के 3 तक कोई भी पाठ पूरा पढ़ना निःशुल्क है — इसके बाद CoddyKit PRO हर पाठ अनलॉक करता है, साथ ही अंतर्निर्मित कोड संपादक और चौबीसों घंटे एआई शिक्षक के साथ व्यावहारिक अभ्यास भी उपलब्ध कराता है। यह Flutter मोबाइल विकास सीखने के मार्ग का हिस्सा है और आपकी प्रगति वेब तथा CoddyKit ऐप पर सिंक होती रहती है। Flutter मोबाइल विकास पाठ्यक्रम में कुल 4 पाठ शामिल हैं।
Flutter Apps के लिए Firebase
Firebase integration में आपका स्वागत है! Firebase Google का एक शक्तिशाली platform है, जो आपके mobile और web applications के लिए कई backend services उपलब्ध कराता है।
यह Backend-as-a-Service (BaaS) के रूप में काम करता है, यानी यह server-side operations संभालता है ताकि आप अपने app के frontend को बनाने पर ध्यान दे सकें।
इस lesson में हम Flutter project में Firebase सेट करेंगे और basic user authentication लागू करेंगे।
Firebase Project सेट करना
सबसे पहले, आपको एक Firebase project की आवश्यकता है। Firebase Console पर जाएँ और इन steps का पालन करें:
- 'Add project' पर click करें और दिए गए निर्देशों का पालन करें।
- Project बन जाने के बाद, 'Add app' button (Android या iOS icon) पर click करें।
- Package name (Android) या bundle ID (iOS) देकर अपने app को register करें।
- Configuration file download करें: Android के लिए
google-services.jsonऔर iOS के लिएGoogleService-Info.plist।
Flutter में Firebase जोड़ना
Firebase Console में अपना project सेट करने के बाद, आपको अपने Flutter project की pubspec.yaml file में आवश्यक dependencies जोड़नी होंगी।
Firebase को initialize करने के लिए firebase_core और authentication के लिए firebase_auth जोड़ेंगे।
फिर packages प्राप्त करने के लिए अपने terminal में flutter pub get चलाएँ।
Firebase Core Initialize करना
किसी भी Firebase service का उपयोग करने से पहले, आपको अपने Flutter app में Firebase को initialize करना होगा, आमतौर पर अपने main() function में।
Flutter की binding initialize हो जाए, यह सुनिश्चित करने के लिए Firebase.initializeApp() से पहले WidgetsFlutterBinding.ensureInitialized() call करना सुनिश्चित करें।
FlutterFire CLI आसान platform-specific setup के लिए firebase_options.dart generate कर सकता है।
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
// import 'firebase_options.dart'; // Generated by FlutterFire CLI
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
// options: DefaultFirebaseOptions.currentPlatform, // Use if generated
);
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Firebase App',
home: Scaffold(
appBar: AppBar(title: Text('Firebase Init')),
body: Center(child: Text('Firebase initialized!')),
),
);
}
}कॉन्फ़िगरेशन फ़ाइल का स्थान
डाउनलोड की गई कॉन्फ़िगरेशन फ़ाइलों को उनके सही स्थानों पर रखें:
- Android:
google-services.jsonको अपने Flutter प्रोजेक्ट कीandroid/app/निर्देशिका में ले जाएँ। - iOS: अपने Flutter प्रोजेक्ट को Xcode (
ios/Runner.xcworkspace) में खोलें, फिरGoogleService-Info.plistको 'Runner' फ़ोल्डर में खींचकर छोड़ें। 'Copy items if needed' चुनना और सभी लक्ष्यों में जोड़ना सुनिश्चित करें।
ईमेल/पासवर्ड से पंजीकरण
Firebase Authentication विभिन्न साइन-इन विधियों का समर्थन करता है। आइए ईमेल और पासवर्ड से शुरुआत करें।
पहले, Firebase Console में 'Authentication' -> 'Sign-in method' के अंतर्गत 'Email/Password' प्रदाता सक्षम करें।
इसके बाद, नए उपयोगकर्ताओं को पंजीकृत करने के लिए FirebaseAuth.instance.createUserWithEmailAndPassword का उपयोग करें।
import 'package:firebase_auth/firebase_auth.dart';
class AuthService {
final FirebaseAuth _auth = FirebaseAuth.instance;
Future<User?> signUp(String email, String password) async {
try {
UserCredential result = await _auth.createUserWithEmailAndPassword(
email: email,
password: password,
);
return result.user;
} catch (e) {
print(e.toString());
return null;
}
}
// This main is for demonstration of the function logic.
// In a real app, Firebase.initializeApp() would be called once at app start.
static void main() async {
// Simulate Firebase being initialized
// await Firebase.initializeApp();
AuthService auth = AuthService();
User? user = await auth.signUp("test@example.com", "password123");
if (user != null) {
print("Signed up user: ${user.email}");
} else {
print("Sign up failed.");
}
}
}ईमेल/पासवर्ड से साइन इन
उपयोगकर्ता के पंजीकृत हो जाने के बाद, वह अपने ईमेल और पासवर्ड का उपयोग करके साइन इन कर सकता है। यह कार्य signInWithEmailAndPassword विधि से किया जाता है।
पंजीकरण और साइन-इन, दोनों विधियाँ एक UserCredential ऑब्जेक्ट लौटाती हैं, जिसमें सफल होने पर User ऑब्जेक्ट होता है।
import 'package:firebase_auth/firebase_auth.dart';
class AuthService {
final FirebaseAuth _auth = FirebaseAuth.instance;
Future<User?> signIn(String email, String password) async {
try {
UserCredential result = await _auth.signInWithEmailAndPassword(
email: email,
password: password,
);
return result.user;
} catch (e) {
print(e.toString());
return null;
}
}
// This main is for demonstration of the function logic.
// In a real app, Firebase.initializeApp() would be called once at app start.
static void main() async {
// Simulate Firebase being initialized
// await Firebase.initializeApp();
AuthService auth = AuthService();
User? user = await auth.signIn("test@example.com", "password123");
if (user != null) {
print("Signed in user: ${user.email}");
} else {
print("Sign in failed.");
}
}
}प्रमाणीकरण स्थिति की निगरानी
यह जानना बहुत महत्वपूर्ण है कि कोई उपयोगकर्ता वर्तमान में साइन इन है या साइन आउट। Firebase Auth authStateChanges() नामक एक स्ट्रीम उपलब्ध कराता है, जो प्रमाणीकरण स्थिति बदलने पर एक नया User ऑब्जेक्ट भेजती है।
उपयोगकर्ता की लॉगिन स्थिति के आधार पर अपने UI को गतिशील रूप से अपडेट करने के लिए इस स्ट्रीम का उपयोग StreamBuilder विजेट के साथ किया जा सकता है।
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Auth State App',
home: StreamBuilder<User?>(
stream: FirebaseAuth.instance.authStateChanges(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return CircularProgressIndicator();
}
if (snapshot.hasData) {
return HomeScreen(user: snapshot.data!); // User is signed in
} else {
return LoginScreen(); // User is signed out
}
},
),
);
}
}
class HomeScreen extends StatelessWidget {
final User user;
HomeScreen({required this.user});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Welcome')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Logged in as: ${user.email}'),
ElevatedButton(
onPressed: () async {
await FirebaseAuth.instance.signOut();
},
child: Text('Sign Out'),
),
],
),
),
);
}
}Google साइन-इन सेटअप
Google साइन-इन एक लोकप्रिय और उपयोगकर्ता-अनुकूल प्रमाणीकरण विधि है। इसे सक्षम करने के लिए:
- Firebase Console में 'Authentication' -> 'Sign-in method' पर जाएँ और 'Google' सक्षम करें।
google_sign_inपैकेज को अपनेpubspec.yamlमें जोड़ें।- Android: सुनिश्चित करें कि आपका SHA-1 फ़िंगरप्रिंट Firebase प्रोजेक्ट की सेटिंग में पंजीकृत है।
- iOS: Reverse Client ID को अपनी
Info.plistफ़ाइल में जोड़ें (यहGoogleService-Info.plistसे प्राप्त होता है)।
Google साइन-इन लागू करना
सेटअप के बाद, उपयोगकर्ता के क्रेडेंशियल प्राप्त करने के लिए GoogleSignIn पैकेज का उपयोग करके Google साइन-इन लागू कर सकते हैं, और फिर उन क्रेडेंशियल के साथ Firebase में साइन इन कर सकते हैं।
इसके लिए GoogleSignIn().signIn() का उपयोग करना और फिर FirebaseAuth.instance.signInWithCredential() में देने के लिए GoogleAuthProvider.credential बनाना शामिल है।
import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';
class GoogleAuthService {
final FirebaseAuth _auth = FirebaseAuth.instance;
final GoogleSignIn _googleSignIn = GoogleSignIn();
Future<User?> signInWithGoogle() async {
try {
final GoogleSignInAccount? googleUser = await _googleSignIn.signIn();
if (googleUser == null) return null; // User cancelled the sign-in
final GoogleSignInAuthentication googleAuth = await googleUser.authentication;
final AuthCredential credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
UserCredential result = await _auth.signInWithCredential(credential);
return result.user;
} catch (e) {
print(e.toString());
return null;
}
}
// This main is for demonstration of the function logic.
// In a real app, Firebase.initializeApp() would be called once at app start.
static void main() async {
// Simulate Firebase being initialized
// await Firebase.initializeApp();
GoogleAuthService auth = GoogleAuthService();
User? user = await auth.signInWithGoogle();
if (user != null) {
print("Signed in with Google: ${user.email}");
} else {
print("Google Sign-In failed or cancelled.");
}
}
}Firebase Auth त्वरित जाँच
सभी Firebase सेवाएँ तैयार रहें, यह सुनिश्चित करने के लिए Flutter एप्लिकेशन में Firebase.initializeApp() को कब कॉल करना चाहिए?
पुनरावलोकन और अगले चरण
बधाई हो! आपने अपने Flutter ऐप में Firebase को एकीकृत करने और प्रमाणीकरण सेट अप करने की आवश्यक बातें सीख ली हैं।
हमने इन विषयों को शामिल किया:
- Firebase प्रोजेक्ट सेट अप करना और अपना Flutter ऐप जोड़ना।
- Firebase निर्भरताएँ जोड़ना और Firebase Core को आरंभ करना।
- ईमेल/पासवर्ड से पंजीकरण और साइन-इन लागू करना।
- उपयोगकर्ता की प्रमाणीकरण स्थिति में होने वाले बदलावों की निगरानी करना।
- Google साइन-इन सेट अप करना और लागू करना।
Firebase के साथ सुरक्षित और इंटरैक्टिव Flutter एप्लिकेशन बनाने के लिए ये मूलभूत चरण हैं। आगे आप डेटाबेस एकीकरण के लिए Cloud Firestore का अध्ययन करेंगे!
एआई शिक्षक के साथ Dart सीखें — निःशुल्क
अपने ब्राउज़र में वास्तविक कोड लिखें और चलाएँ, चौबीसों घंटे एआई शिक्षक से तुरंत सहायता पाएँ, और वेब या ऐप पर वहीं से शुरू करें जहाँ आपने छोड़ा था।
- पाठ्यक्रम
- 22
- पाठ
- 88
अक्सर पूछे जाने वाले प्रश्न
क्या “Firebase सेटअप और प्रमाणीकरण” पाठ निःशुल्क है?
हाँ — Flutter मोबाइल विकास अध्ययन पथ के 3 तक कोई भी पाठ, जिसमें “Firebase सेटअप और प्रमाणीकरण” भी शामिल है, यहाँ वेब पर पूरा पढ़ना निःशुल्क है। इसके बाद CoddyKit PRO हर पाठ अनलॉक करता है, साथ ही अंतर्निर्मित कोड संपादक और चौबीसों घंटे एआई शिक्षक के साथ इंटरैक्टिव अभ्यास भी उपलब्ध कराता है। Flutter मोबाइल विकास पाठ्यक्रम में कुल 4 पाठ शामिल हैं।
“Firebase सेटअप और प्रमाणीकरण” में मैं क्या सीखूँगा?
अपने Flutter प्रोजेक्ट में Firebase सेट अप करें और ईमेल/पासवर्ड, Google Sign-In तथा अन्य प्रदाताओं से उपयोगकर्ता प्रमाणीकरण लागू करें। आप ब्राउज़र में सीधे चलाए जाने वाले व्यावहारिक कोड के साथ Flutter मोबाइल विकास का अभ्यास करते हैं, और पाठ पूरा करते समय 24/7 एआई ट्यूटर आपके प्रश्नों के उत्तर देता है।
क्या Flutter मोबाइल विकास शुरू करने के लिए मुझे किसी अनुभव की आवश्यकता है?
पहले के अनुभव की आवश्यकता नहीं है। CoddyKit पर Flutter मोबाइल विकास शुरुआती से लेकर उन्नत शिक्षार्थियों तक सभी के लिए व्यवस्थित किया गया है, इसलिए आप यहीं से या शुरुआत से सीखना शुरू कर सकते हैं और अपनी गति से आगे बढ़ सकते हैं। यह 4 में से 1वाँ पाठ है।
“Firebase सेटअप और प्रमाणीकरण” पाठ पूरा करने में कितना समय लगता है?
CoddyKit का अधिकांश पाठ लगभग 5–10 मिनट में पूरा हो जाता है। हर पाठ छोटा और संवादात्मक है, इसलिए आप लगातार प्रगति करते हैं और वेब या ऐप पर वहीं से सीखना जारी रख सकते हैं जहाँ आपने छोड़ा था।
क्या मैं इस Flutter मोबाइल विकास पाठ में कोड लिख और चला सकता हूँ?
हाँ। हर Flutter मोबाइल विकास पाठ में एक अंतर्निर्मित कोड संपादक शामिल है, जिससे आप सीधे अपने ब्राउज़र में वास्तविक कोड लिख और चला सकते हैं और तुरंत एआई प्रतिक्रिया पा सकते हैं—स्थानीय सेटअप की आवश्यकता नहीं है।
इस पाठ्यक्रम के सभी पाठ
- Firebase सेटअप और प्रमाणीकरण
- Cloud Firestore डेटाबेस
- Cloud Storage और Functions
- Firebase Cloud Messaging और सूचनाएँ