Firebase Authentication Services in Flutter

Introduction

  • The first step is to create a firebase project
  • Creating your firebase project you will land up on this screen, and click on the android icon.
Figure:1 Firebase dashboard
  • Enter your package name, which you can find in your app-level build.gradle file inside defaultConfig in applicationId , paste it and click on the Register App button.
Figure:2 Add package name and click on register app
  • Download the google-services.json file and add this file inside the app src folder.
Figure: 3Download google-services.json file
Figure:3 Add google-services.json file in your app folder
  • Add the following dependency in your <project>/build.gradle file
buildscript {
repositories {
// Check that you have the following line (if not, add it):
google() // Google's Maven repository
}
dependencies {
...
// Add this line
classpath 'com.google.gms:google-services:4.3.10'
}
}
allprojects {
...
repositories {
// Check that you have the following line (if not, add it):
google() // Google's Maven repository
...
}
}
  • Add the following line in your <project>/<app-module>/build.gradle file.
apply plugin: 'com.android.application'// Add this line
apply plugin: 'com.google.gms.google-services'
dependencies {
// Import the Firebase BoM
implementation platform('com.google.firebase:firebase-bom:29.0.1')
// Add the dependency for the Firebase SDK for Google Analytics
// When using the BoM, don't specify versions in Firebase dependencies
implementation 'com.google.firebase:firebase-analytics'
// Add the dependencies for any other desired Firebase products
// https://firebase.google.com/docs/android/setup#available-libraries
}
  • Firebase configuration in the flutter app is done, now we will initialize firebase inside our app. To initialize the firebase we need firebase_core package, it provides initializeApp() method to initialize firebase in the flutter app.
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyHomePage());
}
  • Go to the Authentication section and click on get started.
Figure:13
  • Click on the Sign-in method and choose the Email/password option.
Figure:14
  • Enable the Email/Password option and save.
Figure:15

Installing firebase_auth package:

firebase_auth: ^1.1.0
  • Sign-up Method
Future createUser({String email, String password}) async {
await FirebaseAuth.instance
.createUserWithEmailAndPassword(email: email, password: password);
}
  • Sign-in Method
Future signIn({String email, String password}) async {
await FirebaseAuth.instance
.signInWithEmailAndPassword(email: email, password: password);
}
  • Sign-up Error And Success handling
createUser(email: email, password: password)
.then((value) {
//navigate to new screen
})
.onError(
(error, stackTrace) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
error.code.toString(),
),
),
);
},
);
signIn(email: email, password: password)
.then((value) {
//navigate to new screen
})
.onError(
(error, stackTrace) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
error.code.toString(),
),
),
);
},
);
  • Delete User
User user = FirebaseAuth.instance.currentUser;
AuthCredential credentials =
EmailAuthProvider.credential(email: email, password: password);
UserCredential result =
await user.reauthenticateWithCredential(credentials);
await result.user.delete();
Future deleteUser(String email, String password) async {
User user = FirebaseAuth.instance.currentUser;
AuthCredential credentials =
EmailAuthProvider.credential(email: email, password: password);
UserCredential result =
await user.reauthenticateWithCredential(credentials);
await result.user.delete();
}
deleteUser(user.email, passwordController.text)
.then((value) {
//navigate to auth screen
}).onError((error, stackTrace) {
Navigator.of(context).pop();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(error.toString()),
),
);
});
  • Change Password
Future changePassword({String email}) async {
await FirebaseAuth.instance.sendPasswordResetEmail(email: email);
}
changePassword(email: user.email)
.then((value) => ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
"Password reset link sent to email: ${user.email}"),
),
))
.onError(
(error, stackTrace) =>
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(error.code.toString()),
),
),
);
  • SignOut
Future logOut() async {
await FirebaseAuth.instance.signOut();
}
logOut()
.then(
(value) => Navigator.of(context)
.popUntil((route) => route.isFirst),
)
.onError(
(error, stackTrace) =>
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
error.code.toString(),
),
),
),
);

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store