Ruby-on-rails 如何从 Rails 控制台使用 Devise 登录用户?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4929078/
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:21:42  来源:igfitidea点击:

How to sign in a user using Devise from a Rails console?

ruby-on-railsconsoledevise

提问by Christian

After loading the Rails console, how should I sign in a user?

加载 Rails 控制台后,我应该如何登录用户?

Devise provides a test helper which can be used in tests and I've tried to use in console:

Devise 提供了一个可以在测试中使用的测试助手,我已经尝试在控制台中使用:

>> include Devise::TestHelpers
>> helper.sign_in(User.first)

But I get:

但我得到:

NoMethodError: undefined method `env' for nil:NilClass

Anyway, I would like to use the real devise helper and not this test helper. Is there any way to achieve this?

无论如何,我想使用真正的设计助手而不是这个测试助手。有没有办法实现这一目标?

回答by Brian Deterling

Here's one way I was able to do it:

这是我能够做到的一种方法:

>> ApplicationController.allow_forgery_protection = false
>> app.post('/sign_in', {"user"=>{"login"=>"login", "password"=>"password"}})

Then you can do:

然后你可以这样做:

 >> app.get '/some_other_path_that_only_works_if_logged_in'
 >> pp app.response.body

回答by Eric London

Here is another example which uses the csrf token, authenticates the user, and makes a POST/GET request.

这是另一个使用 csrf 令牌、验证用户并发出 POST/GET 请求的示例。

# get csrf token
app.get  '/users/sign_in'
csrf_token = app.session[:_csrf_token]

# log in
app.post('/users/sign_in', {"authenticity_token"=>csrf_token, "user"=>{"email"=>"foo", "password"=>"bar"}})

# get new csrf token, as auth user
app.get ''
csrf_token = app.session[:_csrf_token]

# make a POST request
app.post '/some_request.json', {"some_value"=>"wee", "authenticity_token"=>csrf_token}

# make a GET request
app.get '/some_other_request.json'

回答by Sylvain Kieffer

You can add an action inside one of your controller, and use the technique explained here.

您可以在其中一个控制器中添加一个动作,并使用此处解释的技术。

class MyController < ApplicationController
  # POST /my_controller/become {'email': '[email protected]'}
  def become
    raise 'not in development environment' unless Rails.env == 'development'
    sign_in User.find_by_email(params[:email])
  end
end

回答by justin phelps

Important to note that since Rails 5, the params parsing has been changed to take only one argument and params (instead of params being a second argument.

重要的是要注意,从Rails 5 开始,params 解析已更改为仅采用一个参数和 params(而不是 params 作为第二个参数。

ActionDispatch::ParamsParser is deprecated and was removed from the middleware stack. To configure the parameter parsers use ActionDispatch::Request.parameter_parsers=

ActionDispatch::ParamsParser 已弃用并已从中间件堆栈中删除。要配置参数解析器,请使用 ActionDispatch::Request.parameter_parsers=

So these earlier examples will no longer work in modern Rails without patching your middleware. To make them work, simply include the arguments in the POST params like:

因此,如果不修补中间件,这些早期的示例将不再适用于现代 Rails。要使它们工作,只需在 POST 参数中包含参数,例如:

app.post('/users/sign_in', {"user"=>{"email"=>"[email protected]", "password"=>"mycoolpassword"}})<-- No longer valid

app.post('/users/sign_in', {"user"=>{"email"=>"[email protected]", "password"=>"mycoolpassword"}})<-- 不再有效

app.post('/users/sign_in', params: {"user"=>{"email"=>"[email protected]", "password"=>"mycoolpassword"}})<-- valid

app.post('/users/sign_in', params: {"user"=>{"email"=>"[email protected]", "password"=>"mycoolpassword"}})<--有效