Learn Authentication using with FirebaseAuth in Flutter
To use Firebase Auth, you need to create a Firebase project first. Go to the Firebase console, create a new project, and associate it with your Flutter application. Then enable Firebase Auth in the project settings.
Step 1: Firebase Project Setup In the Firebase console, select your project and go to “Project Settings”. In the “General” tab, under the “Your apps” section, select Flutter and copy the SDK snippet. Step 2: Install Project Dependencies Open the pubspec.yaml file of your Flutter project. Add the firebase_core and firebase_auth packages to the dependencies section. Open the terminal and run the command “flutter pub get” to install the dependencies. Step 3: Firebase Operations Open the main.dart file in your Flutter application. Add the following code to the main() function to initialize Firebase:
import 'package:firebase_core/firebase_core.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
Step 4: Authentication Operations Open the file where your MyApp class is defined. Create a class to perform Firebase Auth operations. Create a method initFirebaseAuth() to initialize the Firebase Auth instance. Define the necessary methods for user registration and login. Step 5: Create User Interface Create pages to perform user registration and login. Add forms like TextFormField or TextField to collect the required information from the user. Use Firebase Auth methods to perform user registration and login.
By following these steps, you can use Firebase Auth in your Flutter application. Firebase Auth allows you to easily manage user authentication, account creation, email verification, social media login, and more.
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
class AuthScreen extends StatefulWidget {
@override
_AuthScreenState createState() => _AuthScreenState();
}
class _AuthScreenState extends State<AuthScreen> {
final FirebaseAuth _auth = FirebaseAuth.instance;
final TextEditingController _emailController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Firebase Auth Demo'),
),
body: Padding(
padding: EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
TextField(
controller: _emailController,
decoration: InputDecoration(
labelText: 'Email',
),
),
SizedBox(height: 16.0),
TextField(
controller: _passwordController,
decoration: InputDecoration(
labelText: 'Password',
),
obscureText: true,
),
SizedBox(height: 16.0),
ElevatedButton(
onPressed: () {
_createUser();
},
child: Text('Create User'),
),
SizedBox(height: 8.0),
ElevatedButton(
onPressed: () {
_loginUser();
},
child: Text('Login'),
),
],
),
),
);
}
Future<void> _createUser() async {
try {
UserCredential userCredential = await _auth.createUserWithEmailAndPassword(
email: _emailController.text,
password: _passwordController.text,
);
User? user = userCredential.user;
if (user != null) {
print('User created: ${user.email}');
} else {
print('Failed to create user');
}
} catch (e) {
print(e.toString());
}
}
Future<void> _loginUser() async {
try {
UserCredential userCredential = await _auth.signInWithEmailAndPassword(
email: _emailController.text,
password: _passwordController.text,
);
User? user = userCredential.user;
if (user != null) {
print('User logged in: ${user.email}');
} else {
print('Failed to login');
}
} catch (e) {
print(e.toString());
}
}
}
By following these steps, you can use Firebase Auth in your Flutter application. Firebase Auth allows you to easily manage user authentication, account creation, email verification, social media login, and more.