Firebase Firestore A complete tutorial || PART 2 || Signup interface using Firebase authentication & registering a new User

Hey Whats up guys welcome back to the second part of the series in the last part we learn how to log in user using firebase and start this series in this part we will register the user using firebase So lets start.

Firebase Firestore A complete tutorial || PART 2 || Signup interface using Firebase authentication & registering a new User

Here is the video tutorial for this part-







So open the project.

And open your SignupActivity xml file which we create in the last part.

And add the UI .

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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.SignupActivity"
    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"/>
    <EditText
        android:layout_marginTop="10dp"
        android:id="@+id/repasswordFiled"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Reenter Password"
        android:layout_marginStart="20dp"
        android:layout_marginEnd="20dp"
        android:textColor="#000"
        android:textStyle="bold"/>

    <Button
        android:id="@+id/register"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:text="Register"
        android:textAllCaps="false"
        android:backgroundTint="#c90751"
        android:textStyle="bold"
        android:textColor="#fff"
        android:layout_marginTop="20dp"/>
    <Button
        android:id="@+id/signinbutton"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:text="Alredy have an account Log in here"
        android:textAllCaps="false"
        android:backgroundTint="#c93407"
        android:textStyle="bold"
        android:textColor="#fff"
        android:layout_marginTop="20dp"/>

</LinearLayout>
Thats look awesome now we need to add this to the java file so open the java file and paste the below code which can register the user using firebase.




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 SignupActivity extends AppCompatActivity {


    private EditText emailFiled,passwordFiled,checkpasswordFiled;
    private Button register,signin;

    private FirebaseAuth mAuth;

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

        emailFiled = findViewById(R.id.emailFiled);
        passwordFiled = findViewById(R.id.passwordFiled);
        checkpasswordFiled = findViewById(R.id.repasswordFiled);

        register = findViewById(R.id.register);
        signin = findViewById(R.id.signinbutton);

        mAuth = FirebaseAuth.getInstance();

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

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

                String email = emailFiled.getText().toString();
                String password = passwordFiled.getText().toString();
                String repasswprd = checkpasswordFiled.getText().toString();

                if(!TextUtils.isEmpty(email)&!TextUtils.isEmpty(password)&&!TextUtils.isEmpty(repasswprd)){

                    if(password.equals(repasswprd)){

                        mAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {

                                if(task.isSuccessful()){
                                    startActivity(new Intent(SignupActivity.this,MainActivity.class));
                                    finish();
                                }else {
                                    Toast.makeText(SignupActivity.this,"Error: "+task.getException().getMessage(),Toast.LENGTH_SHORT).show();
                                }

                            }
                        });

                    }else {
                        Toast.makeText(SignupActivity.this,"Your password is not match",Toast.LENGTH_SHORT).show();
                    }

                }else {
                    Toast.makeText(SignupActivity.this,"All fileds are important",Toast.LENGTH_SHORT).show();
                }

            }
        });

    }
}
Thats it we done with the registering user now we need to sign out the user so open the activity_main.xml file and add a button like below-




<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".MainActivity">

    <Button
        android:id="@+id/signout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="Sign Out"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>
Now open the MainActivity and add this changes -

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();
            }
        });
        
        
    }
}
Now you can run the project and see how its work...




So thanks for your time I will come back with the third part for now comment and tell me what to change in the next part...



Here is the video tutorial for this part-



Comments