简体   繁体   English

Firebase Login Register App with DeviceID

[英]Firebase Login Register App with DeviceID

I have created a login register App.我创建了一个登录注册应用程序。 I used email and password for registration.我使用 email 和密码进行注册。 I also add that if user registration is successful the device id will save to the database.我还补充说,如果用户注册成功,设备 ID 将保存到数据库中。 I want that id the device id is registered with a account the user can't create a new account with that device And i also want that if database device id is matched with the current Device id let user to login or else don't.我想要那个 id 设备 id 是用一个帐户注册的,用户不能用该设备创建一个新帐户我还希望如果数据库设备 id 与当前设备 id 匹配,则让用户登录,否则不登录。 How can i solve this?我该如何解决这个问题? By using Query?通过使用查询?

Create a Child Named USerId .创建一个名为 UserId 的孩子。 And try this:试试这个:

// save data to firebase
     DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
                DatabaseReference ezzeearnRef = rootRef.child("UserId");
                ValueEventListener eventListener = new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                            //here android_id is IMEI number
                        if (dataSnapshot.child(android_id).exists()) {
                            //show what u want
                        } else {
                            //creating a new user
                            firebaseAuth.createUserWithEmailAndPassword(email, password)
                                    .addOnCompleteListener(MainActivity.this, new OnCompleteListener<AuthResult>() {
                                        @Override
                                        public void onComplete(@NonNull Task<AuthResult> task) {
                                            //checking if success
                                            if (task.isSuccessful()) {
                                                DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();

                                                rootRef.child("UserId").child(android_id).setValue(0);

                                                finish();
                                                startActivity(new Intent(MainActivity.this, LoginActivity.class));
                                            } else {
                                                //display some message here
                                                Toast.makeText(MainActivity.this, "Registration Error", Toast.LENGTH_LONG).show();
                                            }
                                            progressDialog.dismiss();
                                        }
                                    });
                        }
public class LoginActivity extends AppCompatActivity {
// vew banding
private ActivityLoginBinding binding;

//fibase atuh
private FirebaseAuth firebaseAuth;

//progress dialog
private ProgressDialog progressDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = ActivityLoginBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());

    //inti firbase atuh
    firebaseAuth = FirebaseAuth.getInstance();

    //setup progress dialog
    progressDialog = new ProgressDialog(this);
    progressDialog.setTitle("please wait...");
    progressDialog.setCanceledOnTouchOutside(false);


    // handel click go to regster
    binding.noAccountTv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(LoginActivity.this, 
   RegisterActivity.class));
        }
    });

    // handel click bifen login
    binding.loginBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            validataData();

        }
    });

}
private String email ="" , password="";
private void validataData() {

    //getdata
    email = binding.emailEt.getText().toString().trim();
    password = binding.passwordEt.getText().toString().trim();

    //validata data

if (.Patterns.EMAIL_ADDRESS.matcher(email).matches()){ if (.Patterns.EMAIL_ADDRESS.matcher(email).matches()){

        Toast.makeText(this, "Invalid email pattern...!", Toast.LENGTH_SHORT).show();
    }
    else if (TextUtils.isEmpty(password)){
        Toast.makeText(this, "Enter password...!", 
 Toast.LENGTH_SHORT).show();
    }
    else {
        //data is vaid data login
         loginUser();
}

 }

 private void loginUser() {
    //show progress
    progressDialog.setMessage("Logging In...");
    progressDialog.show();

    //login user
    firebaseAuth.signInWithEmailAndPassword(email,password)
            .addOnSuccessListener(new OnSuccessListener<AuthResult>() {
                @Override
                public void onSuccess(AuthResult authResult) {
                    //login success , chek user if user or admin
                    chekUser();

                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure( Exception e) {
                    //login faild
                    progressDialog.dismiss();
                    Toast.makeText(LoginActivity.this, ""+e.getMessage(), 
 Toast.LENGTH_SHORT).show();

                }
            });
}

private void chekUser() {
    progressDialog.setMessage("Checking User...");

    //chek user if user or admin for realtime database
    //get crunt user
    FirebaseUser firebaseUser=firebaseAuth.getCurrentUser();

    //cheak in db
    DatabaseReference ref = 
   FirebaseDatabase.getInstance().getReference("Users");
    ref.child(firebaseUser.getUid())
            .addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange( DataSnapshot snapshot) {
                    progressDialog.dismiss();
                    //get user typy
                    String userType = 
    ""+snapshot.child("userType").getValue();
                    //chek userType
                    if (userType.equals("0")){
                        // thise is user soimpel open user dshbord
                        startActivity(new Intent(LoginActivity.this, 
     DashboardOneNozom.class));
                        finish();
                    }
                    else if (userType.equals("1")){
                        // thise is admin soimpel open admin dshbord
                        startActivity(new Intent(LoginActivity.this, 
   DashboardTowNozom.class));
                        finish();
                    }
                }

                @Override
                public void onCancelled( DatabaseError error) {

                }
            });
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android Studio 模拟器 Firebase(登录/注册)不工作,但在设备上工作 - Android Studio emulator Firebase (login/register) not working but works on device Firebase:如何在应用检查中注册我的 Flutter 应用? - Firebase: How do I register my Flutter app in app check? 如何只允许我的应用程序在没有登录的情况下访问 firebase? - How to allow only my app to access firebase without a login? 登录 Firebase/Flutter - Login Firebase/Flutter Firebase 手机登录失败 - Firebase login failed on mobile Facebook 和 Google 登录不适用于 Android AAB 构建上传到 Firebase 应用程序分发 - Facebook and Google login doesn't work for Android AAB build uploaded to Firebase App Distribution Android Android用firebase注册登录 - Android Android registration and login with firebase 如何为我的 iOS 应用程序创建仅允许授权用户(由我确定)并且不包含“注册帐户”功能的登录功能 - How do I create a login feature for my iOS app that only allows authorized users (determined by me) and does not include a "register account" feature Firebase 登录失败 - Firebase login failed Flutter:注册成功后如何让用户自动登录? - Flutter: How to make user login automatically after register successfully?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM