简体   繁体   English

Rails:sign_out current_user 不工作?

[英]Rails: sign_out current_user not working?

I am trying to logout a user session after idle time.我试图在空闲时间后注销用户会话。 I have a controller action in sessions_controller.rb with the below code.我在sessions_controller.rb有一个带有以下代码的控制器操作。

  def inactivity
    flash.now[:alert] = 'Logged out due to inactivity.'
    flash.keep(:alert)
    sign_out current_user
    head :ok
  end

below is the relevant js code which gets triggered after idle time and redirects the user to the sign out page下面是相关的 js 代码,它在空闲时间后触发并将用户重定向到注销页面

Session.inactivity().then(function() {
 localStorage.removeItem(idleTimeCaptureKeyName);
 Turbolinks.visit('/users/sign_in');
}).catch(function(err) {
});

Session.inactivity code is below Session.inactivity 代码如下

  const inactivity = () => {
    return new Promise((resolve, reject) => {
      $.ajax({
        url: '/inactivity',
        method: 'POST',
        beforeSend: function(xhr) {
          xhr.setRequestHeader('X-CSRF-Token',$('meta[name="csrf-token"]').attr('content'));
        },
        success: function(res) {
          resolve(true);
        },
        error: function(err) {
          reject(false);
        }
      })
    });
  }

I am seeing a strange issue where possibly sign_out current_user is not working.我看到一个奇怪的问题,可能sign_out current_user不起作用。 I see sporadically the user is redirected to home page with a message you are already signed in .我偶尔看到用户被重定向到主页,并显示一条you are already signed in的消息。

I am unable to reproduce the issue consistently.我无法始终如一地重现该问题。 below is my question下面是我的问题

  1. what scenario could cause this and how can i reproduce and fix this什么情况会导致这种情况,我该如何重现和解决这个问题

If the Session.inactivity() do request to server you have to make sure that user really logged out and you get proper response.如果Session.inactivity()向服务器发出请求,您必须确保用户真正注销并获得正确响应。 I suppose that some times the then runs earlier then you get the response.我想有时then运行得更早,然后您会得到响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM