Guest User

Untitled

a guest
Dec 26th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
  2.  
  3. Button buttonSignIn,buttonVentanaRegistrar;
  4. EditText editTextEmail, editTextPass;
  5.  
  6. public static String userEmail;
  7.  
  8. FirebaseAuth.AuthStateListener mAuthListener;
  9.  
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_login);
  14.  
  15. buttonSignIn = (Button) findViewById(R.id.btn_login);
  16. buttonSignIn.setOnClickListener(this);
  17.  
  18. buttonVentanaRegistrar = (Button) findViewById(R.id.btn_ventana_registrar);
  19. buttonVentanaRegistrar.setOnClickListener(this);
  20.  
  21. editTextEmail = (EditText) findViewById(R.id.correo_login);
  22. editTextPass = (EditText) findViewById(R.id.contraseña_login);
  23.  
  24.  
  25. //CODIGO QUE IMPLEMENTE PARA QUE DE ERROR CUANDO NO SE INGRESA NINGUN DATO
  26. /*buttonSignIn.setOnClickListener(new View.OnClickListener() {
  27. @Override
  28. public void onClick(View view) {
  29. if(editTextEmail.getText().toString().isEmpty()){
  30. editTextEmail.setError("Ingrese usuario");
  31. }
  32. if(editTextPass.getText().toString().isEmpty()){
  33. editTextPass.setError("Ingrese contraseña");
  34. }
  35.  
  36. if (editTextEmail.getText().toString().isEmpty() && editTextPass.getText().toString().isEmpty()){
  37. editTextEmail.setError("Ingrese usuario");
  38. editTextPass.setError("Ingrese contraseña");
  39. }
  40. else{
  41.  
  42. }
  43. }
  44. });*/
  45.  
  46.  
  47.  
  48.  
  49. mAuthListener = new FirebaseAuth.AuthStateListener() {
  50. @Override
  51. public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
  52. FirebaseUser user = firebaseAuth.getCurrentUser();
  53.  
  54. if(user != null){
  55.  
  56. // startActivity(new Intent(LoginActivity.this, CatalogoActivity.class));
  57. // finish();
  58. }else{
  59.  
  60. //Log.i("SESION","Sesion cerrada");
  61. }
  62.  
  63. }
  64. };
  65.  
  66. }
  67.  
  68.  
  69. @Override
  70. public void onClick(View view) {
  71.  
  72. switch (view.getId()){
  73. case R.id.btn_login:
  74. String email_login = editTextEmail.getText().toString();
  75. String pass_Login = editTextPass.getText().toString();
  76. iniciarSesion(email_login,pass_Login);
  77. break;
  78.  
  79.  
  80. case R.id.btn_ventana_registrar:
  81. Intent siguienteVentana = new Intent(getApplicationContext(), RegistroActivity.class);
  82. startActivity(siguienteVentana);
  83. break;
  84. }
  85.  
  86.  
  87. }
  88.  
  89. private void iniciarSesion(String email,String pass){
  90. userEmail = email;
  91. FirebaseAuth.getInstance().signInWithEmailAndPassword(email,pass).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
  92. @Override
  93. public void onComplete(@NonNull Task<AuthResult> task) {
  94.  
  95. if(task.isSuccessful()){
  96. //mensaje("Inicio de sesion correctamente");
  97. Intent siguienteVentana = new Intent(getApplicationContext(), CatalogoActivity.class);
  98. startActivity(siguienteVentana);
  99. }else{
  100. mensaje(task.getException().getMessage()+"");
  101. }
  102.  
  103. if(editTextEmail.getText().toString().isEmpty() && editTextPass.getText().toString().isEmpty()){
  104. editTextEmail.setError("Ingrese usuario");
  105. editTextPass.setError("Ingrese contraseña");
  106. }
  107. }
  108.  
  109.  
  110. });
  111.  
  112. }
  113.  
  114.  
  115.  
  116. @Override
  117. protected void onStart() {
  118. super.onStart();
  119. FirebaseAuth.getInstance().addAuthStateListener(mAuthListener);
  120. }
  121.  
  122. @Override
  123. protected void onStop() {
  124. super.onStop();
  125. if(mAuthListener != null){
  126. FirebaseAuth.getInstance().removeAuthStateListener(mAuthListener);
  127. }
  128.  
  129. }
  130.  
  131. public void mensaje(String mensaje){
  132.  
  133. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  134. builder.setMessage(mensaje)
  135. .setTitle("Mensaje")
  136. .setCancelable(false)
  137. .setNeutralButton("Aceptar",
  138. new DialogInterface.OnClickListener() {
  139. public void onClick(DialogInterface dialog, int id) {
  140. dialog.cancel();
  141. }
  142. });
  143. AlertDialog alert = builder.create();
  144. alert.show();
  145. }
Add Comment
Please, Sign In to add comment