为什么我的认证不允许我登录?



试图找出为什么身份验证不能正常工作为我的代码,即使与gem设计。我的登录页面显示正确,但当它涉及到登录与现有用户的信息位于种子。如果是Rb文件,它会输出else语句,而不是指向主页。

def sign_in
page = User.find_by(email: params[:email])
if page.present? && page.authenticate(params[:password])
session[:user_id] = page.id
redirect_to root_path, notice: "You have successfully logged in"
else
flash[:alert] = "Invalid info"
redirect_to login_path
end
end

我将user. authenticate (params[:password])更改为page. authenticate (params[:password]),但同样的问题仍然显示

Started POST "/login"For::1 at 2022-11-15 19:30:14 -0600由pagescontroller# sign_in作为TURBO_STREAM处理参数:{"authenticity_token"=>"[FILTERED]";session"=>{"email"=>"hi@email.com";password"=>"[FILTERED]"};User Load (0.4ms) SELECT "users"* FROM "users"在"users"!"email"$1 [["LIMIT", 1]]↳app/controllers/pages_controller。rb: 14:"sign_in"重定向到http://localhost:3000/login完成302发现在188ms (ActiveRecord: 2.1ms |分配:8011)

Started GET "/login"For::1 at 2022-11-15 19:30:14 -0600由pagescontroller# login作为TURBO_STREAM处理渲染布局/login.html渲染页面。在布局/应用程序中的动词/login.html呈现页面。在布局/应用程序中的erb(持续时间:1.2ms |分配:817)共享/_flash.html呈现。erb(持续时间:0.1ms |分配:31)渲染布局布局/application.htmlerb(持续时间:80.3ms |分配:6806)在102ms内完成200个OK (Views: 82.3ms | ActiveRecord: 0.0ms | Allocations: 7152)

这是你的日志中的第一条线索…

SELECT "users".* FROM "users" WHERE "users"."email" IS NULL

params[:email]为空,因为email参数的形式是session,您可以在日志中看到…

"session"=>{"email"=>"hi@email.com", "password"=>"[FILTERED]"}, "commit"=>"Login"}

所以你的代码需要使用表单名来访问emailpassword参数…

def sign_in
user = User.find_by(email: params[:session][:email])
if user.present? && user.authenticate(params[:session][:password])
session[:user_id] = user.id
redirect_to root_path, notice: "You have successfully logged in"
else
flash[:alert] = "Invalid info"
redirect_to login_path
end
end

最新更新