Reputation: 335
I want after the user sign is complete to get the status of that user and base on that status show him his activity if he is a admin then show him the admin activity if he is a ordinary user the show him the user activity.
private void LoginUser(String email,String password){
fAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
final DocumentReference documentReference = fStore.collection("users").document(fAuth.getCurrentUser().getUid());
documentReference.addSnapshotListener(new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
if(documentSnapshot.getString("Status").equals("user")){
Toast.makeText(LoginActivity.this, "Login Successfully", Toast.LENGTH_SHORT).show();
// startActivity(new Intent(getApplicationContext(),HomeActivity.class));
Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
startActivity(intent);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}else if(documentSnapshot.getString("Status").equals("Admin")){
//Then move to the AdminActivity
Toast.makeText(LoginActivity.this, "he is a Admin", Toast.LENGTH_SHORT).show();
}else{
//throw error
}
}
});
}else{
Toast.makeText(LoginActivity.this, "Error "+ Objects.requireNonNull(task.getException()).getMessage(), Toast.LENGTH_SHORT).show();
}
loadingBar.dismiss();
}
});
}
Upvotes: 0
Views: 85
Reputation: 730
I would recommend creating a Class, which represents the values coming back from your database, I am not sure of your naming convention or structure in your Firebase DB, so here is an example.
Kotlin:
class User {
val name: String? = null
val status: String? = null
}
Java:
public class User {
private String name;
private String status;
public String getName() {
if (name == null) {
return "";
}
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStatus() {
if (status == null) {
return "";
}
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
Then inside onEvent
you should be able to do:
Kotlin:
val documentModel = documentSnapshot.document.toObject(User::class.java))
if(documentModel.status == "user"{
//do your work
}
Java:
User documentModel = documentSnapshot.getDocument().toObject(User.class);
You might not need to do .getDocument
, you might be able to just do documentSnapshot.toObject
but as I am not near my IDE, I am having to guess.
I would also recommend making sure your snapshot is not empty before doing the above, so that you can handle accordingly.
Something like:
if (!documentSnapshots.isEmpty()) {
// blah blah
}
PS; Your question isn't very clear, I am unsure if you're fetching your values ok, but your having issues with handling inside onEvent
or if the whole method isn't working as expected and you're currently not retrieving any values. Regardless, I hope the above points you in the right direction.
Upvotes: 1