How to access the token registration registration controller?

I use to develop an authentication token to authenticate some parts of my rails application. But when I try to create a new user from the registration path, he gives me the following error {"errors":["Authorized users only."]} .

Here is the rspec code I'm using for the test,

 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 

Additional information: the model name is β€œUser” and the routes look like

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

I understand that the developer expects the user to be authenticated before accessing this path, but this is user registration, it must be out of authentication. Can you suggest a solution for this? Is there any configuration that I am missing here?

+5
source share
1 answer

Try:

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

This will create the following routes:

  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 

Also create a controller in app/controllers/api/v1/api_base_controller.rb

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

Also add to your file app/controllers/application_controller.rb

  before_action :configure_permitted_parameters, if: :devise_controller? 
+4
source

All Articles