ruby on rails-如何访问设计令牌身份验证注册控制器



我正在使用Devise auth令牌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

附加信息:型号名称为"用户",路线看起来像

namespace :api do
  scope :v1 do
    mount_devise_token_auth_for 'User', at: 'auth'
  end
end

我知道设计人员希望用户在访问此路径之前进行身份验证,但这是用户注册,需要在身份验证之外。你能为此提出一个解决方案吗?我这里缺少什么配置吗?

尝试使用:

  namespace :api do
    namespace :v1 do
      mount_devise_token_auth_for 'User', at: '/auth'
    end
  end

这将创建以下路线:

        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  

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

class Api::V1::BaseApiController < ActionController::Base
  include DeviseTokenAuth::Concerns::SetUserByToken
end

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

  before_action :configure_permitted_parameters, if: :devise_controller?

最新更新