Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access devise token auth registration controller?

I am using Devise auth token gem for authenticating some parts of my rails app. But when I try to create a new user with the registration path, it is giving me the following error{"errors":["Authorized users only."]}.

Here is the rspec code that I am 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 info: the model name is 'User' and the routes looks like,

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

I understand that the devise is expecting the user to be authenticated before accessing this path, but this being the user registration, it needs to be outside the authentication. Can you suggest a solution for this? Is there any configuration that I am missing here?

like image 736
Dhanesh Neela Mana Avatar asked Jun 30 '15 13:06

Dhanesh Neela Mana


People also ask

What is devise token?

Simple, multi-client and secure token-based authentication for Rails. If you're building SPA or a mobile app, and you want authentication, you need tokens, not cookies. This gem refreshes the tokens on each request, and expires them in a short time, so the app is secure.

What is device gem in rails?

Devise is the cornerstone gem for Ruby on Rails authentication. With Devise, creating a User that can log in and out of your application is so simple because Devise takes care of all the controllers necessary for user creation ( users_controller ) and for user sessions ( users_sessions_controller ).


1 Answers

Try with:

  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 an 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?
like image 85
Paulo Fidalgo Avatar answered Oct 03 '22 11:10

Paulo Fidalgo