im trying to get a table in firestore to store additional details about a user authenticated from firebase auth using one time authentication. the authentication bit works really fine,problem is when i try to check if a user exists in firestore, i get a null value instead.
check out my code below`
public FirebaseFirestore db=FirebaseFirestore.getInstance();
FirebaseUser mUser;
public String userId;
public final String[] useRole=new String[1];
private Boolean fstoreUser;
private Boolean found;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
checkSignIn();
checkFirestoreuser();
getUseRole();
TextView mText=(TextView) findViewById(R.id.tv_testuser);
switch (useRole[0]){
case "LC":
mText.setText("im an LC");
break;
case "Ordinary":
mText.setText("im an ordionary user");
}
}
private void checkFirestoreuser() {
found = findUserInFirestore(userId);
if(!found)
createUserInFirestore();
}
private Boolean findUserInFirestore(String userId) {
final Boolean[] foundOrNot = new Boolean[1];
DocumentReference docRef=db.collection("users").document(userId);
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot doc=task.getResult();
foundOrNot[0] =doc.exists();
}else{
Log.d("failed",task.getException().getMessage());
Toast.makeText(MainActivity.this, task.getException().getMessage(),
Toast.LENGTH_LONG).show();
}
}
}
);
return foundOrNot[0];
}
private void checkSignIn(){
FirebaseAuth auth=FirebaseAuth.getInstance();
if(auth.getCurrentUser()!=null) {
mUser=auth.getCurrentUser();
userId=mUser.getUid();
}
}
private void getUseRole() {
if (fstoreUser||found) {
db.document("users/"+userId).get()
.addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()){
DocumentSnapshot doc=task.getResult();
useRole[0]=doc.getString("role");
}else{
Toast.makeText(MainActivity.this, task.getException().getMessage(),
Toast.LENGTH_SHORT).show();
}
}
});
} else {
createUserInFirestore();
}
}
private void createUserInFirestore() {
Map<String,Object> newUser= new HashMap<>();
newUser.put("role","Ordinary");
db.collection("users").document(userId).set(newUser)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (!task.isSuccessful()){
Toast.makeText(MainActivity.this,"Please Try Again: "+
task.getException().getMessage(), Toast.LENGTH_LONG).show();
}
}
});
checkSignIn();
}
inside the findUserInFirestore() method, it just doest check so foundOrNot returns with a null value, and this throws anull pointer exception. please help me double check what went wrong, this has been beating me up and over for two nights now.