Firebase authentication

Firebase authentication:
Part-1: Create Firebase Project and connect it to your app.

check...

Part-2: Registration.

1. In firebase, there are many ways of registration.if we go firebase console, we can see these ways..
we should enable one of their Sign-in methods first.



2. Design UI for Signup Page:



3. Now we should do some code for receiving data/email & password from those EditText and write some code for Firebase authentication by Email & Password.

Declare & Initialize  mAuth.

Pass the Email & Password to Firebase.


4. Above 1st we get the Email & Password from input Field. then pass them as a parameter of
mAuth.createUserWithEmailAndPassword(email, password) method.

here is the full java class:


       

package com.nirjhor3029.firebaseauthentication;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
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;


public class Signup extends AppCompatActivity {

    private EditText ET_SignupEmail, ET_SignupPassword;
    private Button BTN_Signup;
    private ProgressBar progressBar;

    private FirebaseAuth mAuth;

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

        ET_SignupEmail = (EditText) findViewById(R.id.ET_SignupEmail);
        ET_SignupPassword = (EditText) findViewById(R.id.ET_SignupPassword);
        progressBar = (ProgressBar) findViewById(R.id.progressbar);

        mAuth = FirebaseAuth.getInstance();

        BTN_Signup = (Button) findViewById(R.id.BTN_Signup);
        BTN_Signup.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View view) {
                registerUser();
            }
        });
    }

    private void registerUser() {
        String email = ET_SignupEmail.getText().toString().trim();
        String password = ET_SignupPassword.getText().toString().trim();
        if (email.isEmpty()) {
            ET_SignupEmail.setError("Email is required");
            ET_SignupEmail.requestFocus();
            return; //prevent code to go below. stop here.        }

        if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) //import android.util.Patterns;        {
            ET_SignupEmail.setError("Please enter a Valid Email !");
            ET_SignupEmail.requestFocus();
            return;
        }

        if (email.isEmpty()) {
            ET_SignupPassword.setError("Password is required");
            ET_SignupPassword.requestFocus();
            return;
        }
        if (password.length() < 6) {
            ET_SignupPassword.setError("Minimum length of password should be 6 .");
            ET_SignupPassword.requestFocus();
            return;
        }

        //if code run and come here it means all the above condition is false means everything ok ;)        //TODO: So here we can write code for registration
        progressBar.setVisibility(View.VISIBLE);

        mAuth.createUserWithEmailAndPassword(email, password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            Toast.makeText(getApplicationContext(), "User Registered Successful", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(getApplicationContext(), "Failed", Toast.LENGTH_LONG).show();
                        }
                        progressBar.setVisibility(View.GONE);
                    }
                });

    }

    public void goToLogin(View view) {
        startActivity(new Intent(this, MainActivity.class));
    }
}

Signup.java:


5. Now Login Part.







       

    public void login() {
    String email = ET_LoginEmail.getText().toString().trim();
    String password = ET_LoginPassword.getText().toString().trim();

    progressBar.setVisibility(View.VISIBLE);

    if (!formValidation(email, password)) {
        return;
    }

    //TODO:
        mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            progressBar.setVisibility(View.GONE);
            if (task.isSuccessful()) {

                Toast.makeText(MainActivity.this, "User Login Successfull", Toast.LENGTH_SHORT).show();

                Intent intent = new Intent(getApplicationContext(), ProfileActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //it will clear all the previously open activity from top of stack
                startActivity(intent);
            } else {
                Toast.makeText(MainActivity.this, "Failed: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
            }
        }
    });


}

Login Page full class / code




6. Saving User Information:














মন্তব্যসমূহ

এই ব্লগটি থেকে জনপ্রিয় পোস্টগুলি

API (Application Programming Interface) .

Add Gif file in Android xml file