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);
}
}