如何通过客户端的JWT cookie强制注销用户



我在前端(公共端(的节点应用程序上注销时遇到问题。

我通过客户端按钮点击事件处理注销,使用下面的脚本调用客户端signout((,但在pug模板中应该重定向到的页面仍然显示为已登录,直到再次刷新页面!事实上,重定向似乎从未发生过!!

export const signout = async () => {
console.log('Client Side Logout');
try {
const res = await axios({
method: 'GET',
url: '/api/v1/users/logout',
});
console.log(res);
if (res.data.status === 'success') {
showAlert('success', 'Signed out successfully !');
window.setTimeout(() => {
location.assign('/');
}, 1000);
}
} catch (err) {
console.log(err.response);
showAlert('error', 'Error logging out! Try again.');
}
};

此函数调用express服务器端路由"/api/v1/users/logout",基本上将JWT设置为"loggeout"值,以使JWT无效并有效注销用户。

exports.logout = (req, res) => {
res.cookie('jwt', 'loggedout', {
expires: new Date(Date.now() + 10 * 1000),
httpOnly: true,
secure: req.secure || req.headers['x-forwarded-proto'] === 'https',
});
res.status(200).json({ status: 'success' });
};

http状态似乎总是204,而客户端的signout((函数似乎从未达到"Signed out successfully!"控制台日志。似乎是缓存问题。

扯掉我的头发!!!非常感谢任何帮助或想法。

原来是我的错,我忘记了在按钮点击时阻止default((,所以忽略了点击事件!无论如何,谢谢

最新更新