【问题标题】:How to initiate a devise logout with AJAX如何使用 AJAX 启动设备注销
【发布时间】:2019-10-29 01:55:58
【问题描述】:

我想使用 Sweetalert2 启动设计注销/注销,但下面的代码在 rails 中生成了一个无效的 CSRF 令牌错误。

以下代码向设计退出 URL 发起 DELETE HTTP 请求,但此时 CSRF 错误发生且未退出。请注意 destroy_user_session_path 的 ERB 标记。如果我硬编码到注销页面的链接,它会产生同样的错误。

任何有关如何解决此问题的帮助将不胜感激。

function logoutSwal() {
    Swal.fire({
    title: 'Ready to Leave?',
    text: "Select 'Logout' below if you are ready to end your current session.",
    type: 'question',
    showCancelButton: true,
    confirmButtonColor: '#3085d6',
    confirmButtonText: 'Logout'
    }).then((result) => {
        if (result.value) {
            fetch('<%= destroy_user_session_path %>', {
                method: 'DELETE'
            }
        }
    })
}

更新

以下功能现在可以部分工作,它可以正确注销(删除会话),但不会重定向到登录页面。如果我刷新页面 (CTRL-R),我会被带到登录页面。我怀疑这个问题现在与设计控制器和响应 AJAX / JSON 有关?

我是否必须更新控制器以响应(即重定向)此请求?

功能:

window.ajaxLogout = function(logoutUrl) {
    Swal.fire({
        title: 'Ready to Leave?',
        text: "Select 'Logout' below if you are ready to end your current session.",
        type: 'question',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        confirmButtonText: 'Logout'
    }).then((result) => {
        if (result.value) {
            $.ajax({
                url: logoutUrl,
                headers: {
                    'X-CSRF-Token': $('meta[name=csrf-token]').attr('content')
                },
                type: "DELETE"
            })
        }
    })
}

函数运行时的日志入口:

Started DELETE "/logout" for IPXXX at 2019-10-29 19:14:47 -0400

Processing by Users::SessionsController#destroy as */*
  User Load (0.7ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Account Load (0.3ms)  SELECT  "public"."accounts".* FROM "public"."accounts" WHERE "public"."accounts"."subdomain_name" = $1 LIMIT $2  [["subdomain_name", "demo"], ["LIMIT", 1]]
   (0.2ms)  BEGIN
   (0.6ms)  SELECT COUNT(*) FROM "users"
   (0.3ms)  COMMIT
Completed 204 No Content in 19ms (ActiveRecord: 3.8ms)

【问题讨论】:

  • 阅读 fetch 文档 ... 默认情况下,fetch 不会从服务器发送或接收任何 cookie ...所以我假设它缺少 cookie问题...您需要在init 参数(第二个)中使用适当的credentials 选项 - 请参阅documentation
  • 谢谢,这让我走上了正确的道路。我现在可以将一个有效的 CSRF 令牌传递给控制器​​并删除会话,但它不会重定向(因为它通常用于 HTML 响应)带有成功消息的登录页面。请参阅上面的更新。
  • its not redirecting 什么不是?当前页面? $.ajax 加载数据,不改变当前页面
  • 哇,所以你放弃了fetch,转而选择jQuery.ajax?我认为你已经使用了 jQuery,并且没有为此加载 jQuery!
  • 是的 jQuery 已经加载,只是在尝试 ajax。 Fetch 和 ajax 都存在同样的重定向问题。更具体地说,设计会话控制器操作未运行。有关控制器操作,请参见此处(我的操作是指 flash 通知和 respond_to_on_destroy 方法:github.com/plataformatec/devise/blob/…

标签: javascript ruby-on-rails devise sweetalert2


【解决方案1】:

最后我只是使用了ajax成功函数重定向到登录页面。

window.ajaxLogout = function(logoutUrl, loginUrl) {
  Swal.fire({
    title: "Ready to Leave?",
    text: "Select 'Logout' below if you are ready to end your current session.",
    icon: "question",
    showCancelButton: true,
    confirmButtonColor: "#3085d6",
    confirmButtonText: "Logout"
  }).then(result => {
    if (result.value) {
      $.ajax({
        url: logoutUrl,
        type: "DELETE",
        success: function() {
          window.location.href = loginUrl;
        }
      });
    }
  });
};

【讨论】:

    猜你喜欢
    • 2022-08-15
    • 1970-01-01
    • 2023-04-07
    • 2012-12-20
    • 2019-02-14
    • 1970-01-01
    • 2020-12-07
    • 2022-07-20
    • 1970-01-01
    相关资源
    最近更新 更多