如何让 Devise 和 JWT 与我的 Versioned API Rails 控制器一起工作?

How do I get Devise and JWT to work with my Versioned API Rails Controller?

我有一个 rails API 使用 Devise。当您点击 POST /login 端点时,它会让用户登录并提供 JWT。这一切都很好,直到我最近将我的控制器命名为一个版本。这就是我的会话#create 方法的样子:

def create
    self.resource = warden.authenticate!(auth_options)

    set_flash_message!(:notice, :signed_in)
    sign_in(resource_name, resource)
    yield resource if block_given?

    render json: current_user
  end

从前端发出登录请求时,我的参数如下所示:

{
    user: {
        email: 'john@gmail.com',
        password: 'password'
    }
}

这之前是有效的,但现在我已经为我的会话控制器迁移到 Api::V1 命名空间,它要求前端在 :api_v1_user 而不是 :user 下提交凭据。

有什么方法可以更改我的会话#create 函数以查看 :user 属性而不是 :api_v1_user?

更新

  namespace :api do
namespace :v1 do
    devise_for :users,
       path: '',
       path_names: {
         sign_in: 'login',
         sign_out: 'logout',
         registration: 'signup'
       },
       controllers: {
         sessions: 'api/v1/sessions',
         registrations: 'api/v1/registrations'
       }

好的,我明白了。感觉很老套,但确实有效。这是我将 Api::V1::SessionsController 创建方法更改为:

  def create
    # Changing scope from :api_v1_user to :user
    Devise.mappings[:user] = Devise.mappings[:api_v1_user]
    warden.config[:default_strategies][:user] =  warden.config[:default_strategies].delete(:api_v1_user)
    auth_opts = auth_options
    auth_opts[:scope] = :user

    self.resource = warden.authenticate!(auth_opts)

    set_flash_message!(:notice, :signed_in)
    sign_in(resource_name, resource)
    yield resource if block_given?

    render json: current_api_v1_user
  end