【问题标题】:devise_token_auth with devise for non-devise routesdevise_token_auth 与非设计路线的设计
【发布时间】:2020-06-17 03:53:59
【问题描述】:

我正在尝试使用“enable_standard_devise_support = true”让 devise + devise_token_auth 一起工作,而无需复制每个控制器,一个用于 Web,一个用于 API。

我在初始化程序中打开标准设计支持

        : config.enable_standard_devise_support = true

并将我的路线设置为嵌套

        :   namespace :api, defaults: {format: 'json'} do # namespace devise token routes to stop duplication
        :     namespace :v1 do
        :       mount_devise_token_auth_for 'User', at: 'auth'
        :     end
        :   end

home 路由仍然在 API 之外

        : resources :home, only: [:index]
        : root to: "home#index"

我有一个 API application_controller.rb;

        : module Api
        :   module V1
        :     class ApplicationController < ActionController::API
        :       include DeviseTokenAuth::Concerns::SetUserByToken
        :       before_action :authenticate_user!
        :       before_action :configure_permitted_parameters, if: :devise_controller?
        :       
        :       protected
        : 
        :       def configure_permitted_parameters
        :         devise_parameter_sanitizer.permit(:sign_in, keys: [:email, :password])
        :       end
        :     end
        :   end
        : end

还有一个 application_controller.rb ;

        : class ApplicationController < ActionController::Base
        :   protect_from_forgery unless: -> { request.format.json? }
        : end

为什么我的其他路线(例如 home#index)不起作用?

我从 devise_token_auth 访问 /home.json 区域得到的错误

        : Successfully synced application org.nativescript.NativeScriptTemplate2 on device emulator-5586.
        : JS: Angular is running in the development mode. Call enableProdMode() to enable the production mode.
        : JS: Login called
        : JS: Logged in: 06rD-pZ-kstw_YZO7cWTCQ
        : JS: ERROR {
        : JS:   "headers": {
        : JS:     "normalizedNames": {},
        : JS:     "lazyUpdate": null
        : JS:   },
        : JS:   "status": 401,
        : JS:   "statusText": "Unauthorized",
        : JS:   "url": "http://192.168.200.4:3000/home.json?uid=user@example.com&client=vRardfTGtk10YTXwz8cSRg&access-token=06rD-pZ-kstw_YZO7cWTCQ",
        : JS:   "ok": false,
        : JS:   "name": "HttpErrorResponse",
        : JS:   "message": "Http failure response for http://192.168.200.4:3000/home.json?uid=user@example.com&client=vRardfTGtk10YTXwz8cSRg&access-token=06rD-pZ-kstw_YZO7cWTCQ: 401 Unauthorized",
        : JS:   "error": {
        : JS:     "error": "You need to sign in or sign up before continuing."
        : JS:   }
        : JS: }

这是家庭控制器

class HomeController < ApplicationController
  before_action :authenticate_user!

  respond_to :html, :json

  def index
    respond_to do |format|
      format.html
      format.json { render json: {message: "Welcome to the Ruby on Rails backend"} }
    end
  end
end

更新:使用 curl 进行测试

这是我的模板代码:https://github.com/map7/backend_rails6_template

如果我使用 curl 命令测试 API,我可以复制问题;

在提交“102bb24”时,主路由的 API 已损坏,但 Web 视图正常工作。

登录

curl -i -X POST http://localhost:3000/api/v1/auth/sign_in.json -F email=user@example.com -F password=password

获取带有凭据的下一页(请注意,您必须粘贴从登录标头返回的客户端和访问令牌)

curl -i -X GET http://localhost:3000/home.json\?uid\=user@example.com\&client\=UQPxKFYEQ1GcaA_PpIjx2Q\&access-token\=PTR-jCn8xgg6GfZ2Fu5QeA

我得到的错误

HTTP/1.1 401 Unauthorized
Content-Type: application/json; charset=utf-8
Cache-Control: no-cache
X-Request-Id: a746a91c-bffa-405a-8b17-a5dd0ce37df1
X-Runtime: 0.012248
Vary: Origin
Transfer-Encoding: chunked

{"error":"You need to sign in or sign up before continuing."}% 

注意:要查看工作示例,请先尝试提交“e475478”。此提交适用于 API,但不适用于 Web 视图。

【问题讨论】:

    标签: ruby-on-rails devise devise-token-auth


    【解决方案1】:

    尝试创建App::BaseController

    并将认证方式放在这里:

    class App::BaseController < ApplicationController
      before_action :authenticate_user!
    end
    

    现在,为您想要获得授权的所有控制器继承 App::BaseController

    【讨论】:

      【解决方案2】:

      我必须为 API 创建一个单独的控制器,并将该单独的控制器放入路由中以使其工作。

      API 主控制器

                : module Api
                :   module V1
                :     class HomeController < ApplicationController
                :       before_action :authenticate_user!
                :       
                :       def index
                :         render json: {message: "Welcome to the Ruby on Rails backend"}
                :       end
                :     end
                :   end
                : end
      

      HTML 主页控制器

                : class HomeController < ApplicationController
                :   before_action :authenticate_user!
                : end
      

      路线

                : Rails.application.routes.draw do
                :   devise_for :users             # Standard devise routes must come first!
                :   resources :home, only: [:index]
                :   root to: "home#index"
                : 
                :   namespace :api, defaults: {format: 'json'} do
                :     namespace :v1 do
                :       mount_devise_token_auth_for 'User', at: 'auth'
                :       resources :home, only: [:index]
                :     end
                :   end
                :   
                : end
      

      【讨论】:

        猜你喜欢
        • 2013-10-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-10
        相关资源
        最近更新 更多