简体   繁体   English

设计自定义会话控制器奇怪的事情

[英]Devise Custom Sessions Controller weird thing

I'm trying to customise SessionsControllers, I've got One for the API part of my projet, the other one for websessions. 我正在尝试自定义SessionsControllers,我为projet的API部分提供了一个,为websessions提供了另一个。

Here my routes.rb file 这是我的routes.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

It seems to work fine. 似乎工作正常。

Now got a problem for my websessions_controller. 现在我的websessions_controller遇到了问题。 I need to check a database flag (boolean) to check if my user is activated or not (FlgEnbl). 我需要检查数据库标志(布尔值)以检查我的用户是否已激活(FlgEnbl)。 Here my code 这是我的代码

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

So, if I try to log with a valid user and a wrong password, the failure message appear on my login page, valid user and password and bool set to true redirect to my dashboard page, but valid user and password with my bool set to false redirect to my dashboard page with a message 'You are already signed in.' 因此,如果我尝试使用有效的用户名和错误的密码登录,则失败消息会显示在我的登录页面上,有效的用户名和密码以及bool设置为true会重定向到我的仪表板页面,而有效的用户名和密码将bool设置为错误地重定向到我的仪表板页面,并显示消息“您已经登录”。 instead of displaying the error message. 而不是显示错误消息。

Same code with a render json on my api controller work. 我的api控制器上具有render json的相同代码。

Did I miss something or is it an issue ? 我错过了什么吗?还是有问题?

Thanks 谢谢

So, I found another solution adding this to my model : 因此,我找到了另一个解决方案,将其添加到模型中:

 def active_for_authentication? 
    super && FlgEnbl? 
  end 

  def inactive_message 
    if !FlgEnbl? 
      :not_approved 
    else 
      super # Use whatever other message 
    end 
  end

Just need to add the message for :not_approved in i118n devise file in failure section. 只需在故障部分的i118n devise文件中添加:not_approved的消息即可。

Hope this help. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM