如何访问设计令牌身份验证注册控制器?

Dha*_*ana 6 ruby rspec ruby-on-rails token devise

我正在使用Devise auth token gem来验证我的rails应用程序的某些部分.但是当我尝试使用注册路径创建一个新用户时,它会给我以下错误{"errors":["Authorized users only."]}.

这是我用于测试的rspec代码,

it 'creates a user using email/password combo' do
    post api_user_registration_path, { email: 'xxx', password: 'yyy',password_confirmation: 'yyy'}
    puts last_response.body
    expect(last_response.body).not_to have_content('error')
end
Run Code Online (Sandbox Code Playgroud)

附加信息:型号名称为"用户",路线如下,

namespace :api do
  scope :v1 do
    mount_devise_token_auth_for 'User', at: 'auth'
  end
end
Run Code Online (Sandbox Code Playgroud)

我知道该设计期望在访问此路径之前对用户进行身份验证,但这是用户注册,它需要在身份验证之外.你能为此建议一个解决方案吗?我在这里缺少任何配置吗?

Pau*_*lgo 4

尝试使用:

  namespace :api do
    namespace :v1 do
      mount_devise_token_auth_for 'User', at: '/auth'
    end
  end
Run Code Online (Sandbox Code Playgroud)

这将创建以下路由:

        new_api_v1_user_session GET    /api/v1/auth/sign_in(.:format)        devise_token_auth/sessions#new                                                                                                                                  
            api_v1_user_session POST   /api/v1/auth/sign_in(.:format)        devise_token_auth/sessions#create                                                                                                                               
    destroy_api_v1_user_session DELETE /api/v1/auth/sign_out(.:format)       devise_token_auth/sessions#destroy                                                                                                                              
           api_v1_user_password POST   /api/v1/auth/password(.:format)       devise_token_auth/passwords#create                                                                                                                              
       new_api_v1_user_password GET    /api/v1/auth/password/new(.:format)   devise_token_auth/passwords#new                                                                                                                                 
      edit_api_v1_user_password GET    /api/v1/auth/password/edit(.:format)  devise_token_auth/passwords#edit                                                                                                                                
                                PATCH  /api/v1/auth/password(.:format)       devise_token_auth/passwords#update                                                                                                                              
                                PUT    /api/v1/auth/password(.:format)       devise_token_auth/passwords#update                                                                                                                              
cancel_api_v1_user_registration GET    /api/v1/auth/cancel(.:format)         devise_token_auth/registrations#cancel                                                                                                                          
       api_v1_user_registration POST   /api/v1/auth(.:format)                devise_token_auth/registrations#create                                                                                                                          
   new_api_v1_user_registration GET    /api/v1/auth/sign_up(.:format)        devise_token_auth/registrations#new                                                                                                                             
  edit_api_v1_user_registration GET    /api/v1/auth/edit(.:format)           devise_token_auth/registrations#edit                                                                                                                            
                                PATCH  /api/v1/auth(.:format)                devise_token_auth/registrations#update                                                                                                                          
                                PUT    /api/v1/auth(.:format)                devise_token_auth/registrations#update                                                                                                                          
                                DELETE /api/v1/auth(.:format)                devise_token_auth/registrations#destroy                                                                                                                         
     api_v1_auth_validate_token GET    /api/v1/auth/validate_token(.:format) devise_token_auth/token_validations#validate_token  
Run Code Online (Sandbox Code Playgroud)

还创建一个控制器app/controllers/api/v1/api_base_controller.rb

class Api::V1::BaseApiController < ActionController::Base

  include DeviseTokenAuth::Concerns::SetUserByToken

end
Run Code Online (Sandbox Code Playgroud)

还要添加到您的文件中app/controllers/application_controller.rb

  before_action :configure_permitted_parameters, if: :devise_controller?
Run Code Online (Sandbox Code Playgroud)