gpt4 book ai didi

ruby-on-rails - 设计完成 401 Unauthorized in 1ms after apply strong parameter

转载 作者:行者123 更新时间:2023-12-03 16:21:10 25 4
gpt4 key购买 nike

我刚刚在我的应用程序中配置了强参数,一切似乎都很好。但我的设备登录失败。我仍然可以注册用户并登录用户。我正在设计 3.1.1

我添加到 application_controller:

   before_filter :configure_permitted_parameters, if: :devise_controller? 

protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:email, :password, :remember_me) }
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password) }
end

在我的模型中
   devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :token_authenticatable, :invitable, :invite_for => 2.weeks, :authentication_keys => [:username]

和我的日志,有一个弃用警告:设计:token_authenticable is deprecated.,不确定这是否是问题?
       Started POST "/users/login" for 127.0.0.1 at 2013-10-09 21:54:13 +1300
DEPRECATION WARNING: devise :token_authenticatable is deprecated. Please check Devise 3.1 release notes for more information on how to upgrade. (called from <class:User> at /home/jcui/Desktop/workspace/iv/app/models/user.rb:6)
Configuration Load (0.4ms) SELECT "configurations".* FROM "configurations"
Processing by Devise::SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"G1aVfzHcwHAI7ao6sBLF9WtgJAWlQ8c5KlKzEHpZzTo=", "user"=>{"email"=>"xxx@gmail.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign in"}
Completed 401 Unauthorized in 1ms
Processing by Devise::SessionsController#new as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"G1aVfzHcwHAI7ao6sBLF9WtgJAWlQ8c5KlKzEHpZzTo=", "user"=>{"email"=>"xxx@gmail.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign in"}
Rendered devise/shared/_links.erb (0.6ms)
Rendered devise/sessions/new.html.erb within layouts/application (6.8ms)
Completed 200 OK in 169ms (Views: 83.0ms | ActiveRecord: 0.0ms | Solr: 0.0ms)

我尝试在session new view中输出资源错误对象,但是没有错误!!
#<ActiveModel::Errors:0xe911424 @base=#<User id: nil, email: "myemail@gmail.com", encrypted_password: "$2a$10$j6lAQhBNgsO01HuBjxbgCOdvd0biRehWhQct50ee8cAo...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil,
sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: nil, updated_at: nil, api_key: nil, avatar: nil, deleted_at: nil, roles_mask: nil, subdomain_id: nil,
invitation_token: nil, invitation_sent_at: nil, invitation_accepted_at: nil, invitation_limit: nil, invited_by_id: nil, invited_by_type: nil, username: nil, avatar_processing: nil, lang: nil>, @messages={}>

最佳答案

您在设计模型中使用用户名作为身份验证模式

:authentication_keys => [:username]

在您的登录中,您有用于身份验证的电子邮件。尝试更改您允许的参数
 devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:email, :password, :remember_me) } 


devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:username, :password, :remember_me) }

还要修改 config/initializers/devise.rb 以具有:
config.case_insensitive_keys = [ :email, :username ]
config.strip_whitespace_keys = [ :email, :username ]

如果您想使用用户名或密码登录,请查看此链接:
https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-sign-in-using-their-username-or-email-address

关于ruby-on-rails - 设计完成 401 Unauthorized in 1ms after apply strong parameter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19267370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com