【问题标题】:How do I force logout user via JWT cookie on client side如何通过客户端的 JWT cookie 强制注销用户
【发布时间】:2021-03-25 09:16:43
【问题描述】:

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

我正在通过客户端按钮单击事件处理注销,以使用下面的脚本调用客户端 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.');
    }
};

此函数调用快速服务器端路由'/api/v1/users/logout' 基本上将JWT 设置为'loggedout' 的值以使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() 函数似乎永远不会到达“已成功退出!”控制台日志。好像是缓存问题。

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

【问题讨论】:

  • showAlert 是否被调用?另外,当他们点击logout 时,用户在哪里?当您尝试从 / 重定向到 / 时,浏览器可能表现异常
  • 是的,GET 请求默认被缓存。不要将它们用于操作。将其设为POST 端点。
  • 原来这是我的错,我忘了在按钮点击时防止default(),所以忽略了点击事件!还是谢谢。

标签: javascript node.js jwt


【解决方案1】:

原来这是我的错,我忘记在按钮单击时使用 preventdefault(),所以忽略了单击事件!还是谢谢

【讨论】:

    猜你喜欢
    • 2019-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-04
    • 1970-01-01
    • 2017-05-17
    • 1970-01-01
    相关资源
    最近更新 更多