Firebase Authentication method in flutter
Prabesh Bhattarai
1 min read
Authentication in any application is one of the important part of the program. It helps to indentify a user of the particular application or not. It indentify a user by searching the user email and password on the firebase database. Firebase authentication is one of the easiest and simple to understand.
Here is the firebase authentication code to login and signout.
import 'package:firebase_auth/firebase_auth.dart';
class AuthController extends GetxController {
FirebaseAuth auth = FirebaseAuth.instance;
//textcontrollers
var emailController = TextEditingController();
var passwordController = TextEditingController();
//login method
Future<UserCredential?> loginMethod({context}) async {
UserCredential? userCredential;
try {
userCredential = await auth.signInWithEmailAndPassword(
email: emailController.text, password: passwordController.text);
} on FirebaseAuthException catch (e) {
VxToast.show(context, msg: e.toString());
}
return userCredential;
}
//signout method
signoutMethod(context) async {
try {
await auth.signOut();
} catch (e) {
VxToast.show(context, msg: e.toString());
}
}
}
For Signup
//signup method
Future<UserCredential?> signupMethod({email,password,context}) async{
UserCredential? userCredential;
try{
userCredential =
await auth.createUserWithEmailAndPassword(email:email,password:password);
} on FirebaseAuthException catch (e){
VxToast.show(context, msg: e.toString());
}return userCredential;
}
1
Subscribe to my newsletter
Read articles from Prabesh Bhattarai directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by