致命异常:"尝试在null对象引用上调用虚拟方法'boolean java.lang.boolean.booleanValue(('">
下面是代码
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");
}
}
};
我还试图通过云函数onCreate触发器将它们设置为false,这是云功能代码
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;
});
});
错误消息告诉您自定义声明中的一个值不存在。您的代码应该检查null并检查类型,而不是盲目地转换为布尔值:
Object driver = result.getClaims().get("driver");
if (driver != null && driver instanceof Boolean) {
boolean isDriver = ((Boolean) driver).booleanValue();
}
此外,如果您想强制刷新包含任何自定义声明的ID令牌,则应将true传递给getIdToken:
user.getIdToken(true)