我有一个rails应用程序,我添加了一项功能,使用发送给他们的确认令牌自动登录用户,作为电子邮件链接中URL的一部分。
我在application_controller.rb
添加了以下内容:
class ApplicationController < ActionController::Base
before_action :bypass_login
private
def bypass_login
if params[:some_params]
some_params = params[:some_params]
if some_params[:token] && some_params[:client_email]
user = User.by_bypass_token(some_params[:token])
sign_in(user, :bypass => true) if user
puts "We should have a current user over here #{current_user}"
end
end
end
end
在上面,puts确实在终端显示current_user
,但它似乎仍然会抛出错误而不是真正登录用户。终端响应是:
Processing by ErrorsController#routing as */*
Parameters: {"some_params"=>
{"client_email"=>"some@example.com", "token"=>"iasSDagLadgdagB"}, "a"=>"api/v1/player/game_play"}
User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."login_bypass_token" = $1 LIMIT 1 [["login_bypass_token", "2464l6nbjb416vi4v64iv64iva25a44326363yhvyivg234624646"]]
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 5]]
We should have a current user over here #<User:0x007fc215c58b48>
Completed 401 Unauthorized in 322ms (Views: 0.2ms | ActiveRecord: 1.6ms)
即使用户应该在到达任何其他控制器之前登录,它也会显示errorController#routing和401 Unauthorized。我需要在这里添加更多内容吗?
答案 0 :(得分:0)
您是否考虑过在确认后重定向中使用设计sign_in
?例如:
class ConfirmationsController < Devise::ConfirmationsController
private
def after_confirmation_path_for(resource_name, resource)
sign_in(resource)
your_new_after_confirmation_path
end
end