Firebase Authentication – How to Sign Up, Sign In, Sign Out, Verify Email | Android

Firebase Authentication provides backend services, easy-to-use SDKs, and ready-made UI libraries to authenticate users to your app. In this tutorial, we’re gonna build a simple register/login Android App using Firebase Email & Password authentication.

More practice:
Firebase Authentication – Send Reset Password Email / Forgot Password | Android

Related post:
Firebase Authentication – Google Sign In | Android

I. Way to do

0. Add Firebase to Android App
0.1 Create Firebase Project and Add Firebase Config file

firebaseauth-register-app

Follow this guide to create Firebase Project and generate google-services.json file and move it into your Android App root directory. You don’t need to get SHA-1 Key in this example.

0.2 Add dependencies

build.gradle (project-level):

build.gradle (App-level):

1. Enabling Firebase Auth

Go to Your Firebase Project Console -> Authentication -> SIGN-IN METHOD -> Enable Email/Password:
firebaseauth-enable-email

2. FirebaseAuth object makes things simple
2.1 Initialize the FirebaseAuth instance

In the Activity onCreate() method:

2.2 Check if the user is currently signed in

In Activity onStart() method:

2.3 Sign up a new user

Creates new user with the createUserWithEmailAndPassword() method:

2.4 Sign in an existing user

Login with signInWithEmailAndPassword() method:

2.5 Access user information

Get Information of a user from FirebaseUser object:

2.6 Sign out current user

Log out with signOut() method:

2.7 Send Email Verification

We can send an email to verify User with sendEmailVerification() method. If the task is successful, the email was sent.

This email template is shown on the Email Templates tab in the Authentication section of the Firebase console:
firebaseauth-email-verification-template

II. Practice

1. Goal

We will build an Android App that can:
– create Account, sign in/sign out, send email verification.
– display current status/user information with verified email status.

firebaseauth-email-password-demo

2. Technology

– Gradle 2.3.3
– Android Studio 2.x
– Firebase Android SDK 11.x

3. Project Structure

firebaseauth-email-password-structure

4. Step by step
4.1 Create Android Project

– Generate new Android Project with package com.javasampleapproach.firebaseauth.
build.gradle (project-level):

build.gradle (App-level):

4.2 Create Firebase Project & Add Firebase Config file

– Follow this guide to generate google-services.json file and move it into your Android App root directory. You don’t need to have SHA-1 Key in this example, just leave it blank.

– Make sure that package_name in google-services.json has a correct value according to:
+ applicationId in build.gradle (App-level).
+ package in AndroidManifest.xml.
In this case, it is com.javasampleapproach.firebaseauth.

4.3 Enabling Firebase Auth

Go to Your Firebase Project Console -> Authentication -> SIGN-IN METHOD -> Enable Email/Password:
firebaseauth-enable-email

4.4 Activity

4.5 Set main launcher activity

Open AndroidManifest.xml and make the LoginActivity.java as launcher activity (set it as the first screen).

4.6 Run & Check result

– Use Android Studio, build and Run your Android App.

– Fill in Email & Password field, then click CREATE ACCOUNT button:
firebaseauth-email-password-mail-sign-in-not-verify

– Open Firebase Project Console to check user:
firebaseauth-email-password-users

– Click VERIFY EMAIL button:
firebaseauth-email-password-mail-sign-in-click-verify

Then check email:
firebaseauth-email-password-mail-verification

– Click link in the email to verify, the browser shows:
firebaseauth-verify-email-web-response

– Go back to Android App, sign out, then sign in, the account is verified now:
firebaseauth-email-password-acc-verify-true

III. Source code

FirebaseAuthDemo

By JavaSampleApproach | August 4, 2017.


Related Posts


2 thoughts on “Firebase Authentication – How to Sign Up, Sign In, Sign Out, Verify Email | Android”

  1. Hello, thank you for this awesome tutorial.

    I want to know that “HOW TO BLOCK USER FROM LOGIN, UNTIL HE VERIFY HIS EMAIL”.
    Hope you got the question, please kindly reply soon…

    1. Dear Muaaz Shaikh,

      Using FirebaseUser.isEmailVerified(), you can check if a User is verified or not, then you can control your Android App UI Flow in updateUI() function:

      Best Regards,
      JSA.

Got Something To Say:

Your email address will not be published. Required fields are marked *

*