【问题标题】:Devise Custom Sessions Controller weird thing设计自定义会话控制器奇怪的事情
【发布时间】:2015-06-26 22:12:38
【问题描述】:

我正在尝试自定义 SessionsControllers,我有一个用于我的项目的 API 部分,另一个用于 websessions。

这是我的 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

它似乎工作正常。

现在我的 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 相同的代码。

我错过了什么还是有问题?

谢谢

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 devise


    【解决方案1】:

    所以,我找到了另一种解决方案,将其添加到我的模型中:

     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 的消息。

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-26
      • 1970-01-01
      • 1970-01-01
      • 2012-10-12
      • 1970-01-01
      相关资源
      最近更新 更多