0% found this document useful (0 votes)
92 views3 pages

Firebase Email Login Android App

This document defines a Login class that handles user authentication and login for an Android app. The class imports necessary Firebase and Android libraries, defines fields like the Firebase auth instance and edit text fields, and implements key login functionality like button clicks, authentication state listeners, and intent handling for navigation. On login, it signs the user in with email/password and verifies their email, navigating to another activity on success and displaying errors on failure.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views3 pages

Firebase Email Login Android App

This document defines a Login class that handles user authentication and login for an Android app. The class imports necessary Firebase and Android libraries, defines fields like the Firebase auth instance and edit text fields, and implements key login functionality like button clicks, authentication state listeners, and intent handling for navigation. On login, it signs the user in with email/password and verifies their email, navigating to another activity on success and displaying errors on failure.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

package com.example.

customer;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
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.google.firebase.auth.FirebaseUser;

public class Login extends AppCompatActivity {


EditText ed_cus_email, ed_cus_password;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener firebaseAuthlistner;

Button login;
TextView registration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);

ed_cus_email = findViewById(R.id.ed_name);
ed_cus_password = findViewById(R.id.ed_password);
mAuth = FirebaseAuth.getInstance();

firebaseAuthlistner = new FirebaseAuth.AuthStateListener() {


@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();


if (user!=null && mAuth.getCurrentUser().isEmailVerified() ){

Intent intent =new Intent(Login.this, Navigation.class);


startActivity(intent);

Toast.makeText(Login.this, "Successfully SignIN",


Toast.LENGTH_SHORT).show();
finish();
}
else
{

// Toast.makeText(Login.this, "Please LOGIN",


Toast.LENGTH_SHORT).show();
}

};
} // On Create Bracket Ended

@Override
public void onBackPressed()
{
final AlertDialog.Builder builder = new AlertDialog.Builder(Login.this);
builder.setMessage("Are You sure you want to EXIT ?");
builder.setCancelable(true);
builder.setNegativeButton("STAY", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.setPositiveButton("EXIT ", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory( Intent.CATEGORY_HOME );
homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();

} // OnBack Pressed

@Override
protected void onStart()
{
super.onStart();
mAuth.addAuthStateListener(firebaseAuthlistner);
} // On Start Bracket Ended

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

mAuth.removeAuthStateListener(firebaseAuthlistner);

} // On Stop Bracket Ended

public void Login_Btn(View view) {

Toast.makeText(Login.this, "Please Wait For a While",


Toast.LENGTH_SHORT).show();

String email = ed_cus_email.getText().toString();


String password = ed_cus_password.getText().toString();
if (TextUtils.isEmpty(email) || TextUtils.isEmpty(password))
{
Toast.makeText(this, "Please fill feild", Toast.LENGTH_SHORT).show();

return;
}

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

if (task.isSuccessful()){

if (mAuth.getCurrentUser() .isEmailVerified()){

Intent intent = new Intent(Login.this, Navigation.class);


startActivity(intent);
finish();

}
else
{
Toast.makeText(Login.this, "Varify your email first",
Toast.LENGTH_SHORT).show();
}

}
else
{
Toast.makeText(Login.this, "Invalid Credentials",
Toast.LENGTH_SHORT).show();
}

}
});
} // Login Bracket Ended

public void regiseruser(View view) {

Intent intent = new Intent(Login.this, registration.class);


startActivity(intent);

}
}

You might also like