我正在尝试自定义SessionsControllers,我有一个用于我的项目的API部分,另一个用于Websessions。
这是我的路线.rb文件
devise_for :users, :controllers => {:sessions => 'websessions'}
resource :dashboard
root :to => "dashboard#index"
namespace :api, defaults: {format: 'json'} do
devise_for :users, :controllers => {:sessions => "api/apisessions"}
end
它似乎工作正常。
现在我的websessions_controller遇到了问题。我需要检查数据库标志(布尔值)以检查我的用户是否已激活(FlgEnbl)。这是我的代码
def create
resource = User.find_for_database_authentication(:email => params[:user][:email])
return failure unless resource
if resource.valid_password?(params[:user][:password])
logger.info 'Flag value' => resource.FlgEnbl
if resource.FlgEnbl == true
sign_in('user', resource)
redirect_to root_url
else
return failure
end
else
return failure
end
end
protected
def failure
warden.custom_failure!
return redirect_to new_user_session_path, alert: 'Invalid email/password, or account disabled !'
end
因此,如果我尝试使用有效用户和错误密码登录,则失败消息会出现在我的登录页面上,有效用户和密码和 bool 设置为 true 重定向到我的仪表板页面,但将我的 bool 设置为 false 的有效用户和密码重定向到我的仪表板页面,并显示消息"您已登录",而不是显示错误消息。
与我的 api 控制器上的渲染 json 相同的代码工作。
我错过了什么还是问题?
谢谢
因此,我找到了另一种解决方案,将其添加到我的模型中:
def active_for_authentication?
super && FlgEnbl?
end
def inactive_message
if !FlgEnbl?
:not_approved
else
super # Use whatever other message
end
end
只需要在失败部分的 i118n 设计文件中添加 :not_approved 的消息。
希望这有帮助。