[英]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.