Firebase Authentication Services in Flutter

Introduction

In this blog, we shall learn about how we can connect our flutter app with firebase and implement authentication using firebase, we shall also learn how we can handle the errors on failure events.

Connect the flutter app with firebase:

  • 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

After clicking on the android icon, you will land upon the below screen:

  • 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
  • Add the following line in your <project>/<app-module>/build.gradle file.
  • 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.
  • 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

Now we have enabled the firebase email password sign-in authentication and now we will implement it in our flutter app.

Installing firebase_auth package:

Add firebase_auth package inside your pubspec.yaml

This package provides us with various methods for signup, sign-in, changing passwords, etc.

Now we will look at who can perform authentication using this package:

  • Sign-up Method

To sign up with an email and password firebase_auth package provides us createUserWithEmailAndPassword methods that take two parameters email, and password .

  • Sign-in Method

For sign in firebase_auth package provides us signInWithEmailAndPassword the method that also takes emailand passwordas parameters.

  • Sign-up Error And Success handling

To handle errors of sign up and sign in we will use onErrormethod of the Future. When we initialize the create user method we will use the then method for success code execution and onError method for error handling. To display the error message we will use snackBar .

sign-in error and success handling:

  • Delete User

To delete the user we first need to re-authenticate the user and to re-authenticate the user we need the current user credentials.

FirebaseAuth provides us currentUser , using this user object we can re-authenticate the user using reauthenticateWithCredential method, this method takes AuthCredentialas an input.

Using EmailAuthProvider class we can get the AuthCredential .

Now we will re-authenticate the user and delete the user.

Method:

Error and Success Handling:

  • Change Password

To change the password firebase_auth package provides us sendPasswordResetEmail method that sends the email on the user's email and from there we can change the password.

Error and Success Handling:

  • SignOut

Error and Success Handling:

Thanks for reading this article ❤

If I got something wrong? Let me know in the comments. I would love to improve.

Clap 👏 If this article helps you.

If we got something wrong? Let me know in the comments. we would love to improve.

--

--

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