Firebase Firestore A complete tutorial || PART 1 || Login interface using Firebase authentication

Hey whats up guys in this post I am going to start a new series for Firebase Firecloude in this full series we will work for Firebase authentication, Firebase Firestore Storing data, Retrieving data, Searching in Firebase Firestore Database and many more I dont know how many parts it will take to complete but I will try to make all the parts and try to explain everything. just share the post and comment so I can get inspire and make next parts...

Firebase Firestore A complete tutorial || PART 1 || Login interface using Firebase authentication

In this first part it is a initial part so it will contain creating firebase project and then we will create the login interface and then log in the users...

Here is the video tutorial for this part-






So first thing first lets start with a new project after creating the project you need to change the dependency to 27 as we are not working on 28 SDK. So after changing the version open the browser and create a new project in firebase.

So if you dont know how to create a firebase project then click the below link and read that first-

https://www.monstertechnocodes.com/2018/10/2018-tutorial-with-updated-dependency.html

Now after creating the firebase project and adding it to the project we need to create a package name as authentication under this package we are going to add login activity and signup activity so create a new empty activity under this authentication package.

Now open the xml file of the activity and paste this code for the UI.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".authentication.LoginActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center">
        <EditText
            android:id="@+id/emailFiled"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Enter Email"
            android:layout_marginStart="20dp"
            android:layout_marginEnd="20dp"
            android:textColor="#000"
            android:textStyle="bold"/>
        <EditText
            android:layout_marginTop="10dp"
            android:id="@+id/passwordFiled"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Enter Password"
            android:layout_marginStart="20dp"
            android:layout_marginEnd="20dp"
            android:textColor="#000"
            android:textStyle="bold"/>

        <Button
            android:id="@+id/signinButton"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:text="Sign in"
            android:textAllCaps="false"
            android:backgroundTint="#c90751"
            android:textStyle="bold"
            android:textColor="#fff"
            android:layout_marginTop="20dp"/>
        <Button
            android:id="@+id/signupButton"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:text="Create a new account"
            android:textAllCaps="false"
            android:backgroundTint="#c93407"
            android:textStyle="bold"
            android:textColor="#fff"
            android:layout_marginTop="20dp"/>


    </LinearLayout>

</RelativeLayout>
 After that open app level build file and add the firebase auth dependency like that




dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    //Firebase auth
    implementation 'com.google.firebase:firebase-auth:11.8.0'
}
Now open the login activity java file and add this code.

package com.monstertechno.firestoretutorial.authentication;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.monstertechno.firestoretutorial.MainActivity;
import com.monstertechno.firestoretutorial.R;

public class LoginActivity extends AppCompatActivity {

    private EditText emailAdress,password;
    private Button signin,signup;

    private FirebaseAuth mAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        emailAdress = findViewById(R.id.emailFiled);
        password = findViewById(R.id.passwordFiled);
        signin = findViewById(R.id.signinButton);
        signup = findViewById(R.id.signupButton);

        signup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(LoginActivity.this,SignupActivity.class));
            }
        });

        mAuth = FirebaseAuth.getInstance();

        signin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String email = emailAdress.getText().toString();
                String Pass = password.getText().toString();

                if(!TextUtils.isEmpty(email)&&!TextUtils.isEmpty(Pass)){
                    mAuth.signInWithEmailAndPassword(email,Pass).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            if(task.isSuccessful()){
                                startActivity(new Intent(LoginActivity.this,MainActivity.class));
                                finish();
                            }else {
                                Toast.makeText(LoginActivity.this,"Error: "+task.getException().getMessage(),Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }else {
                    Toast.makeText(LoginActivity.this,"You forget to put some data",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
Thats it now we need to add the internet permission so open the manifest file and add the user permission

   <uses-permission android:name="android.permission.INTERNET"/>
Now open the MainActivity here we need to check that if the user is logged in or not if logged in then we don't do anything but if the user is not logged in then it send the user to the LoginActivity..




package com.monstertechno.firestoretutorial;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.google.firebase.auth.FirebaseAuth;
import com.monstertechno.firestoretutorial.authentication.LoginActivity;

public class MainActivity extends AppCompatActivity {

    private FirebaseAuth mAuth;
    private String curent_user_id;
    private Button signout;

    @Override
    protected void onStart() {
        super.onStart();

        if(curent_user_id==null){
            startActivity(new Intent(MainActivity.this,LoginActivity.class));
            finish();
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mAuth = FirebaseAuth.getInstance();

        curent_user_id = mAuth.getUid();

        signout = findViewById(R.id.signout);
        signout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mAuth.signOut();
                startActivity(new Intent(MainActivity.this,LoginActivity.class));
                finish();
            }
        });
    }
}
Thats it now we can run this application and log in the user but wait how as the user dont have any account and the user can not create a new one as we dont build the signup activity.

So open the firebase console and open authentication then click set up signing method


Then enable email/password and save it..


Now add a new user then try to log in using this credentials.





Thats it now you are logged in so thanks for your time I will come back with the second part for now comment and tell me what to change in the next part...

Here is the video tutorial for this part-



Comments