Search

Admin and User Login in Firebase Database in Android


Register Page :-




package com.example.personlitydevementapp;

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

import android.content.Intent;
import android.os.Bundle;
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.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
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.FirebaseDatabase;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.FirebaseFirestore;

import java.util.HashMap;
import java.util.Map;

public class Registerpage extends AppCompatActivity {
EditText userid,name,mobilenumber,email,pass;
Button registerbtn;
    boolean valid = true;
    FirebaseAuth fAuth;
    FirebaseDatabase database;
    FirebaseFirestore fstore;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registerpage);
        userid=findViewById(R.id.userid);
        name=findViewById(R.id.username);
        mobilenumber=findViewById(R.id.usermobile);
        email=findViewById(R.id.useremail);
        registerbtn=findViewById(R.id.registerbtn);
        pass=findViewById(R.id.userpassword);
        fAuth=FirebaseAuth.getInstance();
        database=FirebaseDatabase.getInstance();
        fstore=FirebaseFirestore.getInstance();
        registerbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                fAuth.createUserWithEmailAndPassword(email.getText().toString(),pass.getText().toString()).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()){
                            String uid = task.getResult().getUser().getUid();
                            Users users = new Users(userid.getText().toString(),name.getText().toString(),mobilenumber.getText().toString(),email.getText().toString(),pass.getText().toString(),uid,0);

                            database.getReference().child("Usersregister").child(uid).setValue(users);
                            Toast.makeText(Registerpage.this, "User Create", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(Registerpage.this, UsersPage.class);
                            startActivity(intent);
                            finish();
                        }
                    }
                });

            }
        });

    }
}
    
            








Login Page :-


package com.example.personlitydevementapp;

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

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.personlitydevementapp.ui.home.HomeFragment;
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.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class UsersPage extends AppCompatActivity {
    TextView register;
    EditText email,password;
    Button loginbtn;
    FirebaseAuth firebaseAuth;
    FirebaseDatabase firebaseDatabase;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_users_page);
firebaseAuth=FirebaseAuth.getInstance();
firebaseDatabase=FirebaseDatabase.getInstance();
email=findViewById(R.id.loginemail);
password=findViewById(R.id.loginpassword);
        register=findViewById(R.id.gotoregisterpage);
        loginbtn=findViewById(R.id.logingbtn);
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(UsersPage.this,Registerpage.class);
                startActivity(intent);
            }
        });
        loginbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                firebaseAuth.signInWithEmailAndPassword(email.getText().toString(),password.getText().toString()).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()){
                            String uid = task.getResult().getUser().getUid();
                            firebaseDatabase.getReference().child("Usersregister").child(uid).child("usertype").addListenerForSingleValueEvent(new ValueEventListener() {
                                @Override
                                public void onDataChange(@NonNull DataSnapshot snapshot) {
                                    int usertype = snapshot.getValue(Integer.class);
                                    if (usertype == 0){
                                        Intent intent = new Intent(UsersPage.this, MainActivity.class);
                                        startActivity(intent);
                                        finish();

                                    }
                                    if (usertype == 1){
                                        Intent intent = new Intent(UsersPage.this, Adminpage.class);
                                        startActivity(intent);
                                        finish();

                                    }


                                }

                                @Override
                                public void onCancelled(@NonNull DatabaseError error) {

                                }
                            });



                        }

                    }
                });
            }
        });
    }
}






Model Class :- 




package com.example.personlitydevementapp;

public class Users {
    String Userid,Name,Mobilenumber,Email,Password,uid;
int usertype;
    public Users() {
    }

    public Users(String userid, String name, String mobilenumber, String email, String password, String uid, int usertype) {
        Userid = userid;
        Name = name;
        Mobilenumber = mobilenumber;
        Email = email;
        Password = password;
        this.uid = uid;
        this.usertype = usertype;
    }

    public String getUserid() {
        return Userid;
    }

    public void setUserid(String userid) {
        Userid = userid;
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public String getMobilenumber() {
        return Mobilenumber;
    }

    public void setMobilenumber(String mobilenumber) {
        Mobilenumber = mobilenumber;
    }

    public String getEmail() {
        return Email;
    }

    public void setEmail(String email) {
        Email = email;
    }

    public String getPassword() {
        return Password;
    }

    public void setPassword(String password) {
        Password = password;
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public int getUsertype() {
        return usertype;
    }

    public void setUsertype(int usertype) {
        this.usertype = usertype;
    }
}


0 comments

Recent Posts

See All

namefood ingredients app flutter projectFood Donation App College ProjectCollege Android and Website ProjectCollege Leave Management SystemServey for Indian fingerprint unlocking system for

Order any project or you have any idea. Contact - 9306637630. Online Course and Examination Management System Advanced Car & Scooty Training Driving School Management System Advanced Employee Manageme