我正在尝试使用“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
主路由仍在 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 视图。