I have implemented both old oneTapClient and credentialManager for users to log in, and it works like it should, but in few cases it returns null and users cant log in. I'm getting NullPointerException on credentialManager when calling .getCredentialAsync -->
java.lang.NullPointerException
Attempt to invoke interface method 'void h0.j.a(android.content.Context, h0.d0, android.os.CancellationSignal, java.util.concurrent.Executor, h0.k)' on a null object reference
This line credentialManager = CredentialManager.create(context); obviously doesn't initiate credentialManager, so it remains null.
I need help how to solve it, this is my code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auth);
context = this;
mAuth = FirebaseAuth.getInstance();
currentUser = mAuth.getCurrentUser();
if (currentUser != null) {
startNextActivity...
}
else {
credentialManager = CredentialManager.create(context);
GetGoogleIdOption googleIdOption = new GetGoogleIdOption.Builder()
// Only show accounts previously used to sign in.
.setFilterByAuthorizedAccounts(false)
.setServerClientId(DEFAULT_WEB_CLIENT_ID)
// Automatically sign in when exactly one credential is retrieved.
.setAutoSelectEnabled(true)
//.setNonce("")
.build();
request = new GetCredentialRequest.Builder()
.addCredentialOption(googleIdOption)
.build();
}
}
SignInButton signInBtn = findViewById(R.id.sign_in_button);
signInBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
signIn();
}
});
}
private void signIn(){
// Launch sign in flow and do getCredential Request to retrieve the credentials
Executor executor = Executors.newSingleThreadExecutor();
credentialManager.getCredentialAsync(
context,
request,
null, //CancellationSignal cancellationSignal = new CancellationSignal(); cancellationSignal.cancel();
executor,
new CredentialManagerCallback<GetCredentialResponse, GetCredentialException>() {
@Override
public void onResult(GetCredentialResponse result) {
// Handle the successfully returned credential.
Credential credential = result.getCredential();
try {
GoogleIdTokenCredential googleIdTokenCredential = GoogleIdTokenCredential.createFrom(((CustomCredential) credential).getData());
String idToken = googleIdTokenCredential.getIdToken();
// Got an ID token from Google. Use it to authenticate with Firebase.
AuthCredential firebaseCredential = GoogleAuthProvider.getCredential(idToken, null);
mAuth.signInWithCredential(firebaseCredential)
.addOnCompleteListener(AuthActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
currentUser = mAuth.getCurrentUser();
startNextActivity...
} else {
// If sign in fails, display a message to the user.
startNextActivity...
}
}
});
}
catch (Exception e) {
e.printStackTrace();
startNextActivity...
}
}
@Override
public void onError(GetCredentialException e) {
e.printStackTrace();
startNextActivity...
}
});
}