Ruby-on-rails 如何从 Rails 控制台注销设计用户?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28935054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:06:33  来源:igfitidea点击:

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

ruby-on-railsdeviselogout

提问by joseph.hainline

My devise users are "database_authenticatable" and "token_authenticatable". 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. Deleting the user entirely works, but I don't want to go that far.

我的设计用户是“database_authenticable”和“token_authenticable”。我已经尝试从控制台为该用户删除数据库中的“authentication_token”字段,但他们似乎仍然能够使用现有的身份验证令牌。删除用户完全有效,但我不想走那么远。

Edit: for clarity. I want to use the rails consoleto sign out a user. i.e. run rails consoleand then some command.

编辑:为了清楚起见。我想使用 rails控制台注销用户。即运行rails console,然后一些命令。

回答by Ankit Samarthya

Devise provide helper methods to do these things.

设计提供帮助方法来做这些事情。

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

Hope this helps.

希望这可以帮助。

回答by Anto Dominic

If you are using Devise you could use the below in your rails console. This works perfect for me as in my app if you are using only 1 session per user account. I am storing my sessions in redisDB.

如果您使用的是 Devise,您可以在Rails 控制台中使用以下内容。如果您每个用户帐户仅使用 1 个会话,这对我来说非常适用,就像在我的应用程序中一样。我将我的会话存储在 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

回答by octopushugs

I'm not a fan of the sign_out @userpattern because, at least for the devise version I'm using, that signs out the current user, regardless of the argument I pass it. If you're storing sessions in your database then you can do this:

我不喜欢这种sign_out @user模式,因为至少对于我正在使用的设计版本,它会注销当前用户,而不管我传递的参数如何。如果您将会话存储在数据库中,则可以执行以下操作:

@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 我不认为这是最好的方法,但这是我在自己的研究中看到的最好的方法。

回答by mpz

Seems attribute tokens save sessions:

似乎属性令牌保存会话:

user.tokens = nil
user.save

回答by sixty4bit

You may be able to use the helpers that others have mentioned after including the necessary module:

在包含必要的模块后,您也许可以使用其他人提到的帮助程序:

include Devise::Controllers::SignInOut

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如果有人股不使用设计助手涉及的方法在这里

回答by jkelley

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/

回答by Archita Sundaray

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.

它对我有用 - 希望对其他人也有用。