【问题标题】:Rails: Authenticate two user types in one controller with Devise?Rails:使用 Devise 在一个控制器中验证两种用户类型?
【发布时间】:2021-04-06 14:34:12
【问题描述】:

我有两个使用 Devise 的独立用户模型:客户和供应商

我想在我的应用程序中使用一个控制器来执行一组任务,但对两个用户来说都是唯一的,所以我尝试像这样进行身份验证

class ProjectsController < ApplicationController
    before_action :authenticate_customer!...
    before_action :authenticate_vendor!...
...
end

我想从某些操作/视图中排除其中一个。

上述在我的应用程序中的作用是,例如,如果我以供应商身份登录并尝试执行不同的操作 - 它会尝试对客户进行身份验证,反之亦然。

有解决办法吗?我正在考虑设置一个新的控制器,在那里我为同一模型分别验证其中一个,但它必须有自己的视图和所有重复。

有没有办法避免走那条路?

【问题讨论】:

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


    【解决方案1】:

    试试这个-

    class ProjectsController < ApplicationController
        before_action :authenticate_customer!, if: :customer?
        before_action :authenticate_vendor!, if: :vendor?
    ...
    end
    

    customer?vendor? 方法可以基于存储在 DB 中的某个参数值或某个值(例如角色)来定义。

    【讨论】:

      猜你喜欢
      • 2017-03-29
      • 1970-01-01
      • 1970-01-01
      • 2016-07-14
      • 1970-01-01
      • 2021-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多