SIMPLE FIREBASE AUTHENTICATION WITH FLUTTER on Android

Taufik Fadlurahman Fajari
5 min readFeb 13, 2021

The simplest way to do Firebase Authentication with Flutter

Firebase Authentication is a service created by google to make developer easier to manage user authenticate

In this tutorial, i want to show you the simple authentication using Firebase Authentication and integrate with Flutter Android using Email

  1. The first step you have to do is create new flutter project
Create new project Flutter

2. Create new Dart File named “login_and_register.dart” and change “main.dart” code same as code below

login_and_register
main.dart

The display of the UI will be like the picture’s i shared below

3. Copy library Firebase Authentication and Firebase core from https://pub.dev/packages/firebase_auth/install and https://pub.dev/packages/firebase_core/install

firebase auth
firebase_core

you can use the latest version of firebase_auth library, copy firebase_auth library to pubsec.yaml in flutter. Insert those library below the cupertino_icon library.

After copying those library into pubsec.yaml, just click the pub.get on the top right of the corner, or you can open the terminal in the bottom of android studio and type “flutter pub get”.

Set up firebase core in “main.dart” file

change code from

into

dont forget to import firebase core above main function

4. Set up the Firebase

Open Firebase website (https://firebase.google.com/?hl=id), click start, create new project

click Continue

click continue

Select default account for Firebase and then click create project

if Firebase Project successfully created, in your home page of Firebase you can clickthe Logo android as i show below

for the package name, you can see in folder <yourprojectname>/android/app/build.gradle.

Search “applicationId” in build.gradle file and copy without a quotes

For the name you can type name anything you want, its up to you.

click register application.

download google-services.json and copy that json file into <yourprojectname>/android/app

back to firebase, click next, and add line as i show below into <yourprojectname>/android/build.gradle

back to firebase and copy “ apply plugin: ‘com.google.gms.google-services’ ” into <yourprojectname>/android/app/build.gradle and change the minSDKversion in the same file from 16 to 21

change the min sdk version

after that, go back to firebase console. and click Authentication in the left of the Firebase Homepage

click start

click email/password and leave the check box same as i done above.

5. Create new dart file called “authservices.dart”

dont forget to import firebase auth library ( “ import ‘package:firebase_auth/firebase_auth.dart’; “ )

try and catch is a method to catch an error if something wrong happened and run the action if there is no something wrong in an action.

go to “login_and_register.dart” search for Register Button and add action as i bold below

Lets we try for register first

click register and go check terminal in the bottom of android studio app we have successfully registration our account to firebase

if you want to see account who has been register into firebase, you can check in firebase website, go to firebase website and search authentication menu

fadlurahmanf@gmail.com was successfully register to database

now we try some code that might go wrong

  • registration an email who has been register into firebase

go check terminal android studio and see the result

  • The correct email and the weak password

click register and see the result in terminal android studio

it will return code a weak password

6. create method for login in “ authservices.dart “ whitin AuthServices class

insert code above into AuthServices class

and add action in Login Button we created in “ login_and_register.dart “

type email we register before and go check the terminal for the result

Lets we try code we might go wrong

  • type email who have not register into firebase and see the result

it will return a user not found

  • correct email and wrong password

it will return a wrong password

Yeahhh. And thats it for simple tutorial firebase authentication integrate with flutter if you want the full code you can check in here for the code

https://github.com/fadlurahmanf/firebase_authentication

Thank you

--

--