简体   繁体   English

如何从 Rails 控制台注销设计用户?

[英]How can I sign out a devise user from the Rails console?

My devise users are "database_authenticatable" and "token_authenticatable".我的设计用户是“database_authenticable”和“token_authenticable”。 I've tried deleting the "authentication_token" field in the database for that user from the console, but they still seem to be able to use their existing auth token.我已经尝试从控制台为该用户删除数据库中的“authentication_token”字段,但他们似乎仍然能够使用现有的身份验证令牌。 Deleting the user entirely works, but I don't want to go that far.删除用户完全有效,但我不想走那么远。

Edit: for clarity.编辑:为了清楚起见。 I want to use the rails console to sign out a user.我想使用 rails控制台注销用户。 ie run rails console and then some command.即运行rails console ,然后运行一些命令。

Devise provides helper methods to do these things: Devise 提供了辅助方法来做这些事情:

user = User.find(params[:id])
sign_in user
sign_out user

Hope this helps.希望这可以帮助。

If you are using Devise you could use the below in your rails console .如果您使用的是 Devise,您可以在Rails 控制台中使用以下内容。 This works perfect for me as in my app if you are using only 1 session per user account.如果您每个用户帐户仅使用 1 个会话,这对我来说非常适用,就像在我的应用程序中一样。 I am storing my sessions in redisDB.我将我的会话存储在 redisDB 中。

user = User.first
user.update_attributes(unique_session_id: "")

All I had to do was clear my users unique_session_id for that user and rails kicks the user out of the session.我所要做的就是为该用户清除我的用户 unique_session_id 并且 rails 将用户踢出会话。

But for multiple sessions for the same User account this does not work.但是对于同一用户帐户的多个会话,这不起作用。

If you want to clear all user sessions you can do the below from terminal如果要清除所有用户会话,可以从终端执行以下操作

rake db:sessions:clear

To sign_in by Devise check this way in console:要通过设计登录,请在控制台中以这种方式检查:

$ rails console
include Warden::Test::Helpers
def sign_in(resource_or_scope, resource = nil)
  resource ||= resource_or_scope
  scope = Devise::Mapping.find_scope!(resource_or_scope)
  login_as(resource, scope: scope)
end

def sign_out(resource_or_scope)
  scope = Devise::Mapping.find_scope!(resource_or_scope)
  logout(scope)
end

@user = User.find(1)
sign_in @user

Then open http://127.0.0.1:3000/users/sign_in to test, in my case it will bypass this page and go to home page!然后打开http://127.0.0.1:3000/users/sign_in进行测试,在我的情况下它会绕过这个页面并进入主页! Same to sign_out !sign_out相同!

You may be able to use the helpers that others have mentioned after including the necessary module:在包含必要的模块后,您也许可以使用其他人提到的帮助程序:

include Devise::Controllers::SignInOut

source: Module: Devise::Controllers::SignInOut来源: 模块:设计::控制器::SignInOut

There's also another SO question where someone shares a method that doesn't involve using Devise helpers here .还有另一个问题,SO如果有人股不使用设计助手涉及的方法在这里

I'm not a fan of the sign_out @user pattern because, at least for the devise version I'm using, that signs out the current user, regardless of the argument I pass it.我不喜欢sign_out @user模式,因为至少对于我正在使用的设计版本,它会注销当前用户,而不管我传递的参数如何。 If you're storing sessions in your database then you can do this:如果您将会话存储在数据库中,则可以执行以下操作:

@user.update_attributes(current_sign_in_token: "")

TBH I don't think that's the best way to do it, but it's the best way I've seen in my own research. TBH 我不认为这是最好的方法,但这是我在自己的研究中看到的最好的方法。

I believe you can simply update the password_salt and it will invalidate the user session on their next request.我相信您可以简单地更新 password_salt 并且它会在下一次请求时使用户会话无效。

user = User.first
user.update_column(:password_salt, 'reset')    

Reference: http://www.jonathanleighton.com/articles/2013/revocable-sessions-with-devise/参考: http : //www.jonathanleighton.com/articles/2013/revocable-sessions-with-devise/

For old devise versions对于旧设备版本

Seems attribute tokens save sessions:似乎属性令牌保存会话:

user.tokens = nil
user.save

You can create a logout link in views->layouts->application.html.erb as:-您可以在 views->layouts->application.html.erb 中创建一个注销链接:-

<= link_to 'Log Out', destroy_user_session_path, method: :delete %>

It worked for me - hope it does for others as well.它对我有用 - 希望对其他人也有用。

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

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