devise和devise_token_auth不能一起使用

问题描述 投票:0回答:1

我已成功实现devise_token_auth,并且正在尝试进行常规设计。对于devise_token_auth,我有ApiController,它继承自ApplicationController。这是路线文件:

Rails.application.routes.draw do
    defaults format: :json do
      namespace :api do
        resources :appointments, only: [:index]
      end
    end
    scope module: :web do
        root to: 'pages#home'
    end
    mount_devise_token_auth_for 'User', at: 'api'
end

这是从ApiController成功继承的控制器之一:

class Api::AppointmentsController < ApiController
  before_action :authenticate_user!

  def index
    @appointments = Appointment.all
  end
end

我正确返回了json,通知我登录:

{
    "errors": [
        "You need to sign in or sign up before continuing."
    ]
}

这里是页面控制器:

class Web::PagesController < ApplicationController
  before_action :authenticate_user!

  def home

  end
end

如果我注释掉PagesController中的before_action :authenticate_user!,我将转到正确的页面。但是,如果我添加before_action :authenticate_user!,而不是重定向到http://localhost:3000/web/sign_in,我将被定向到http://localhost:3000/api/sign_in,它是json而不是视图。我尝试将devise_for :users, path: 'web'添加到路由中,但是它不起作用,服务器也无法启动。它给我消息

您可能已经使用:as选项定义了两个具有相同名称的路由,或者您可能会覆盖具有相同名称的资源已定义的路由。对于后者,您可以按照以下说明限制使用resources创建的路由:

有什么想法吗?

ruby-on-rails devise ruby-on-rails-5 rails-routing devise-token-auth
1个回答
0
投票

删除before_action:authenticate_user!从页面控制器,并尝试在要重定向的家庭操作中提供路由

class Web::PagesController < ApplicationController

  def home
    redirect_to new_user_session_path
    end

end
© www.soinside.com 2019 - 2024. All rights reserved.