9

Firebase Authentication In Android - Ravi Rupareliya

 2 years ago
source link: https://www.ravirupareliya.com/blog/firebase-authentication-in-android/?amp%3Butm_medium=rss&%3Butm_campaign=firebase-authentication-in-android
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Firebase Authentication In Android

Authentication is the process to provide uniqueness to each user of your application. It will be the identity of particular user.

If your application requires user’s data to be securely stored, you need to authenticate each and every user.

In earlier days we need to have some back-end service developed either in PHP, Java or some other platform. We need some web developer to do that kind of things.

Firebase Authentication provides a way to directly authenticate a user via email and password and store that user to cloud.

Firebase Authentication also support social media authentication like G+, Facebook, Twitter, Github. It means now its a easy task to perform login with facebook or login with g+ task.

Types of Authentication Firebase supports :

  • Email and password based authentication – Authentication with email and password
  • Federated identity provider integration – Authentication with social media(G+, Facebook etc)
  • Custom auth system integration
  • Anonymous auth – Creates an anonymous account.

In today’s post we will see how basic authentication works.

Setup Firebase Authentication

Step 1 : Create a new project on Firebase Console. Enter you project name and select country, than click CREATE PROJECT

Firebase Authentication

Step 2 : After creation of project click on Add Firebase to your Android app. Enter package name, App nickname(optional) and SHA-1 key(optional) and click on ADD APP.

Firebase Authentication

Step 3 : You will receive google-services.json file, put it inside app/ folder of your project.

Firebase Authentication

Step 4 : Add dependency to your root level build.gradle file.

dependencies {
    classpath 'com.google.gms:google-services:3.0.0'

Step 5 : Add dependency to your app/module level build.gradle file.

dependencies {
    compile 'com.google.firebase:firebase-auth:10.2.0'

Step 6 : Add plugin at the bottom of your app-level build.gradle file.

apply plugin: 'com.google.gms.google-services'

Step 7 : That’s it, sync your project to apply all these effect of dependencies.

Initialize FirebaseAuth in your Activity or Application class.

private FirebaseAuth mFirebaseAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
    mFirebaseAuth = FirebaseAuth.getInstance();

Register a User

mFirebaseAuth.createUserWithEmailAndPassword(email, password)
             .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
    @Override
    public void onComplete(@NonNull Task<AuthResult> task) {
        if (task.isSuccessful()) {
            Toast.makeText(RegisterActivity.this, "User registered successfully", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(RegisterActivity.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();

Login a User

mFirebaseAuth.signInWithEmailAndPassword(email,password)
             .addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
    @Override
    public void onComplete(@NonNull Task<AuthResult> task) {
        if(task.isSuccessful()){
            Toast.makeText(LoginActivity.this, "Logged in successfully", Toast.LENGTH_SHORT).show();
        else{
            Toast.makeText(LoginActivity.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();

You can get current user object either from Task or by using FirebaseAuth object

@Override
public void onComplete(@NonNull Task<AuthResult> task) {
    if(task.isSuccessful()){
        FirebaseUser firebaseUser = task.getResult().getUser();
        FirebaseUser firebaseUser = mFirebaseAuth.getCurrentUser();

Change Password

firebaseUser.updatePassword(password)
            .addOnCompleteListener(ChangePasswordActivity.this, new OnCompleteListener<Void>() {
    @Override
    public void onComplete(@NonNull Task<Void> task) {
        if(task.isSuccessful()){
            Toast.makeText(ChangePasswordActivity.this, "Password changed successfully", Toast.LENGTH_SHORT).show();
            return;
        if(task.getException() instanceof FirebaseAuthRecentLoginRequiredException){
            //you need to reauthenticate user in order to change the password.
            //user might have logged in before a while, and firebase is not able to authenticate that user.

Reset Password

mFirebaseAuth.sendPasswordResetEmail(email)
             .addOnCompleteListener(LoginActivity.this, new OnCompleteListener<Void>() {
    @Override
    public void onComplete(@NonNull Task<Void> task) {
        if(task.isSuccessful()){
            Toast.makeText(LoginActivity.this, "An email has been sent.", Toast.LENGTH_SHORT).show();
        else{
            Toast.makeText(LoginActivity.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();

Update Current User Profile

UserProfileChangeRequest userProfileChangeRequest = new UserProfileChangeRequest.Builder()
                .setDisplayName(display name)
                .setPhotoUri(Uri.parse(photo uri))
                .build();
firebaseUser.updateProfile(userProfileChangeRequest)
            .addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
    public void onComplete(@NonNull Task<Void> task) {
        if(task.isSuccessful()){
            Toast.makeText(ProfileActivity.this, "Profile updated.", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(ProfileActivity.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();

Logout User

mFirebaseAuth.signOut();

Isn’t it looks so easy? Yes it is, have your own application with authentication and that also without having help of web developer or any heavy servers.

Ravi Rupareliya

He loves to explore new technologies and have worked on Android, React Native, Action on Google and Flutter.

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK