我有一个简单的云功能,应该可以处理被禁止访问我们网站的用户:
exports.beforeSignIn = functions.auth.user().beforeSignIn((user, context) => {
const {isBanned} = user.customClaims || {};
if (isBanned) {
throw new functions.https.HttpsError('permission-denied', 'This account has been permanently banned due to violating Terms and Conditions');
}
});
我使用此云函数触发customClaim:
exports.setBanStatus = functions.https.onRequest(async (req, res) => {
const {uid, isBanned} = req.body;
try {
await admin.auth().setCustomUserClaims(uid, {isBanned});
return res.json({message: `Ban status updated for user ${uid}`});
} catch (error) {
throw new functions.https.HttpsError('internal', error.message);
}
});
现在,当用户未通过 Firebase 控制台内的身份验证手动禁用时,此功能将起作用,并且显示以下错误:
Firebase: HTTP Cloud Function returned an error: {"error":{"message":"This account has been permanently banned due to violating Terms and Conditions","status":"PERMISSION_DENIED"}} (auth/internal-error).
但是,使用我登录时使用的ajax表单:
$('#signinbtn').on('click', async () => {
const email = $('#email').val();
const password = $('#password').val();
try {
await signInWithEmailAndPassword(auth, email, password);
} catch (error) {
if (error.code === 'auth/user-disabled') {
showMessage('User account has been disabled. Please contact us to re-enable your account.');
} else if (error.code === 'auth/wrong-password') {
showMessage('Wrong password.');
} else {
showMessage(error.message);
}
}
});
当我们禁用用户时,它会触发
auth/user-disabled
并向用户显示 User account has been disabled. Please contact us to re-enable your account.
,因此 beforeSignIn
被接管。
有什么解决办法吗?如果没有,您建议我们采取什么其他方式来处理这种情况?我们需要两种类型的用户禁用,一种是当用户从他们的个人资料中禁用他们的帐户时,另一种是当我们禁用用户帐户作为禁止他们的手段时。
此外,如果用户决定重新启用其帐户,我们希望让他们登录,即基本登录将重新启用他们的帐户,而不是向我们发送电子邮件并要求我们重新启用它.
这听起来像是按设计工作的:在 Firebase 控制台中禁用用户会将其个人资料上的
disabled
声明设置为 true
,这会阻止他们登录。您的自定义实现(使用 isBanned
声明)不会执行任何操作来改变这一点,并且无法改变 Firebase 处理 disabled
声明的行为。
如果您想始终使用自己的逻辑来禁用用户,则应确保您只有一项声明。我建议为您的管理员创建一个自定义用户管理页面,并在其中设置
isBanned
属性 - 或您的云功能可以检查的其他属性。