In the below example, I call a method signinUser(username, password)
This then runs through Firebase to determine if the user has successfully or unsuccessfully been able to sign in. However, it takes Firebase a short amount of time to do this, which by that time the method has already returned with the original value, before being updated by the successful-ness / unsuccessful-ness of the sign in process.
How would I go about returning the method once the Firebase authentication has done it's thing. I am aware I could put a timer on when the return statement is called, however, I'm not sure how that'd work as slow internet connects could cause it to take longer than the given amount set by a timer.
My code is as follows:
public AuthSuccess signinUser(String username, String password) {
mAuth.signInWithEmailAndPassword(username + "@debugrestaurant.com", password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Toast.makeText(context, "Successful!", Toast.LENGTH_SHORT).show();
authSuccess = new AuthSuccess(true, null);
} else {
Toast.makeText(context, "Unsuccessful!", Toast.LENGTH_SHORT).show();
authSuccess = new AuthSuccess(false, task.getException());
Toast.makeText(context, "Exception: " + authSuccess.getException().getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
}
});
return authSuccess;
}
Please note that the class AuthSuccess
is simply an object I've created to collect whether or not the sign in was successful, and if not, to collect the exception.