Reputation: 197
FATAL EXCEPTION: "Attempt to invoke virtual method 'boolean java.lang.Boolean.booleanValue()' on a null object reference"
below is the code
authStateListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull final FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
user.getIdToken(false).addOnSuccessListener(new OnSuccessListener<GetTokenResult>() {
@Override
public void onSuccess(GetTokenResult result) {
boolean isDriver = (boolean) result.getClaims().get("driver");
boolean isStudent = (boolean) result.getClaims().get("verified");
if (isDriver) {
// Show driver UI.
showDriverUI();
}else if(isStudent) {
// Show student UI.
showStudentUI();
}else {
// Show regular user UI.
firebaseAuth.signOut();
Snackbar.make(login, "Please confirm your identity, contact KIU Transport Office", Snackbar.LENGTH_LONG).show();
}
}
});
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
}
}
};
I had also tried to set them to false by cloud-function onCreate trigger, here is cloud-function code
exports.defaultClaims = functions.auth.user().onCreate((user) => {
const email = user.email; // The email of the user.
return admin.setCustomUserClaims(user.uid, {
admin: false,
clerk: false,
driver: false,
verified: false
}).then(function () {
return {
message: `Successfully default role has been added for ${email}`
}
}).catch(function (err) {
return err;
});
});
Upvotes: 0
Views: 1475
Reputation: 317392
The error message is telling you that one of the values in the custom claims doesn't exist. Your code should check for null and check the type instead of blindly casting to a boolean:
Object driver = result.getClaims().get("driver");
if (driver != null && driver instanceof Boolean) {
boolean isDriver = ((Boolean) driver).booleanValue();
}
Also, if you want to force refresh the ID token that contains any custom claims, you should pass true to getIdToken:
user.getIdToken(true)
Upvotes: 1