Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.ronel.finalapp;
- import android.content.Intent;
- import android.support.annotation.NonNull;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.text.TextUtils;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- 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;
- import com.google.firebase.database.DataSnapshot;
- import com.google.firebase.database.DatabaseError;
- import com.google.firebase.database.DatabaseReference;
- import com.google.firebase.database.FirebaseDatabase;
- import com.google.firebase.database.ValueEventListener;
- public class Login extends AppCompatActivity {
- EditText etMail, etPass;
- Button btn;
- String mail,pass;
- Intent go;
- private FirebaseAuth mAuth;
- DatabaseReference ref;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- etMail = findViewById(R.id.etMail);
- etPass = findViewById(R.id.etPass);
- btn = findViewById(R.id.btn);
- mAuth = FirebaseAuth.getInstance();
- }
- public void func(View view) {
- mail = etMail.getText().toString();
- pass = etPass.getText().toString();
- if (TextUtils.isEmpty(mail)) {
- Toast.makeText(this, "Enter mail", Toast.LENGTH_SHORT).show();
- return;
- }
- if (TextUtils.isEmpty(pass)) {
- Toast.makeText(this, "Enter password", Toast.LENGTH_SHORT).show();
- return;
- }
- //If it's regular user:
- //Sign-in
- mAuth.signInWithEmailAndPassword(mail, pass)
- .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
- @Override
- public void onComplete(@NonNull Task<AuthResult> task) {
- if (task.isSuccessful()) {
- // Sign in success, update UI with the signed-in user's information
- Log.d("good", "signInWithEmail:success");
- FirebaseUser user = mAuth.getCurrentUser();
- Toast.makeText(Login.this, "Sign in success",
- Toast.LENGTH_SHORT).show();
- } else {
- // If sign in fails, display a message to the user.
- Log.w("bad", "signInWithEmail:failure", task.getException());
- Toast.makeText(Login.this, "Authentication failed.",
- Toast.LENGTH_SHORT).show();
- }
- // ...
- }
- });
- }
- //If manager
- else {
- ref = FirebaseDatabase.getInstance().getReference().child("manager");
- final String[] s = new String[1];
- ref.addListenerForSingleValueEvent(
- new ValueEventListener() {
- @Override
- public void onDataChange(DataSnapshot dataSnapshot) {
- //Get map of users in datasnapshot
- s[0] = dataSnapshot.getValue(String.class);
- Log.d("Admin password", s[0]);
- //Check if password is the same as typed:
- if (pass.equals(s[0])) {
- continueList();
- }
- else {
- Toast.makeText(Login.this,"Change your email",Toast.LENGTH_SHORT);
- return;
- }
- }
- @Override
- public void onCancelled(DatabaseError databaseError) {
- //handle databaseError
- }
- });
- }
- }
- //Send manager to "manager view page"
- private void continueList() {
- Toast.makeText(Login.this,"Admin registered",Toast.LENGTH_SHORT);
- go = new Intent(this, AdminPageView.class);
- startActivity(go);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment