使用Devise
我想从注册页面复制登录表单的行为。
因此,如果用户存在于数据库中,并且用户输入的密码与数据库中用户的密码匹配,则用户应自动登录到应用程序。
以下代码会覆盖默认的注册控制器,但就我所做的而言。我已经尝试过各种方法从这里调用sign_in方法,但它常常不尽如人意。
class RegistrationsController < Devise::RegistrationsController
def new
super
end
def create
build_resource(sign_up_params)
if User.exists?
Devise::SessionsController.new.create
# sign_in @user
# # self.resource = warden.authenticate!(auth_options)
# set_flash_message(:notice, :signed_in) if is_flashing_format?
# sign_in(resource_name, resource)
# yield resource if block_given?
# respond_with resource, :location => after_sign_in_path_for(resource)
# # set_flash_message :notice, :"user already exists"
# # sign_in(resource_name, resource)
# # respond_with resource, :location => after_sign_in_path_for(resource)
end
if resource.save
yield resource if block_given?
if resource.active_for_authentication?
set_flash_message :notice, :signed_up if is_flashing_format?
sign_up(resource_name, resource)
respond_with resource, :location => after_sign_up_path_for(resource)
else
set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_flashing_format?
expire_data_after_sign_in!
respond_with resource, :location => after_inactive_sign_up_path_for(resource)
end
else
set_flash_message :notice, :"failed to login"
clean_up_passwords resource
respond_with resource
end
end
def update
super
end
end
答案 0 :(得分:0)
在考虑安全性和用户体验时,我不确定这是否是一个好主意。但是......如果你真的想要,你可以确保凭证(电子邮件和密码)是正确的,然后从那一点开始创建会话。代码看起来就像Devise已经在sign_in方法中做到的那样。