Ronel101

Login

Jul 24th, 2018
450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.58 KB | None | 0 0
  1. package com.example.ronel.finalapp;
  2.  
  3. import android.content.Intent;
  4. import android.support.annotation.NonNull;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.text.TextUtils;
  8. import android.util.Log;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.EditText;
  12. import android.widget.Toast;
  13.  
  14. import com.google.android.gms.tasks.OnCompleteListener;
  15. import com.google.android.gms.tasks.Task;
  16. import com.google.firebase.auth.AuthResult;
  17. import com.google.firebase.auth.FirebaseAuth;
  18. import com.google.firebase.auth.FirebaseUser;
  19. import com.google.firebase.database.DataSnapshot;
  20. import com.google.firebase.database.DatabaseError;
  21. import com.google.firebase.database.DatabaseReference;
  22. import com.google.firebase.database.FirebaseDatabase;
  23. import com.google.firebase.database.ValueEventListener;
  24.  
  25. public class Login extends AppCompatActivity {
  26.  
  27.     EditText etMail, etPass;
  28.     Button btn;
  29.     String mail,pass;
  30.     Intent go;
  31.     private FirebaseAuth mAuth;
  32.     DatabaseReference ref;
  33.     @Override
  34.     protected void onCreate(Bundle savedInstanceState) {
  35.         super.onCreate(savedInstanceState);
  36.         setContentView(R.layout.activity_login);
  37.         etMail = findViewById(R.id.etMail);
  38.         etPass = findViewById(R.id.etPass);
  39.         btn = findViewById(R.id.btn);
  40.  
  41.         mAuth = FirebaseAuth.getInstance();
  42.     }
  43.  
  44.     public void func(View view) {
  45.         mail = etMail.getText().toString();
  46.         pass = etPass.getText().toString();
  47.         if (TextUtils.isEmpty(mail)) {
  48.             Toast.makeText(this, "Enter mail", Toast.LENGTH_SHORT).show();
  49.             return;
  50.         }
  51.         if (TextUtils.isEmpty(pass)) {
  52.             Toast.makeText(this, "Enter password", Toast.LENGTH_SHORT).show();
  53.             return;
  54.         }
  55.         //If it's regular user:
  56.         if (!mail.equals("[email protected]")) {
  57.  
  58.             //Sign-in
  59.             mAuth.signInWithEmailAndPassword(mail, pass)
  60.                     .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  61.                         @Override
  62.                         public void onComplete(@NonNull Task<AuthResult> task) {
  63.                             if (task.isSuccessful()) {
  64.                                 // Sign in success, update UI with the signed-in user's information
  65.                                 Log.d("good", "signInWithEmail:success");
  66.                                 FirebaseUser user = mAuth.getCurrentUser();
  67.                                 Toast.makeText(Login.this, "Sign in success",
  68.                                         Toast.LENGTH_SHORT).show();
  69.                             } else {
  70.                                 // If sign in fails, display a message to the user.
  71.                                 Log.w("bad", "signInWithEmail:failure", task.getException());
  72.                                 Toast.makeText(Login.this, "Authentication failed.",
  73.                                         Toast.LENGTH_SHORT).show();
  74.                             }
  75.  
  76.                             // ...
  77.                         }
  78.                     });
  79.         }
  80.         //If manager
  81.         else {
  82.             ref = FirebaseDatabase.getInstance().getReference().child("manager");
  83.             final String[] s = new String[1];
  84.             ref.addListenerForSingleValueEvent(
  85.                     new ValueEventListener() {
  86.                         @Override
  87.                         public void onDataChange(DataSnapshot dataSnapshot) {
  88.  
  89.                             //Get map of users in datasnapshot
  90.  
  91.                             s[0] = dataSnapshot.getValue(String.class);
  92.                             Log.d("Admin password", s[0]);
  93.                             //Check if password is the same as typed:
  94.                             if (pass.equals(s[0])) {
  95.                                 continueList();
  96.                             }
  97.                             else {
  98.                                 Toast.makeText(Login.this,"Change your email",Toast.LENGTH_SHORT);
  99.                                 return;
  100.                             }
  101.                         }
  102.  
  103.                         @Override
  104.                         public void onCancelled(DatabaseError databaseError) {
  105.                             //handle databaseError
  106.                         }
  107.                     });
  108.         }
  109.     }
  110.     //Send manager to "manager view page"
  111.     private void continueList() {
  112.         Toast.makeText(Login.this,"Admin registered",Toast.LENGTH_SHORT);
  113.         go = new Intent(this, AdminPageView.class);
  114.         startActivity(go);
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment