2

GET on /auth/sign_up is not functioning as expected. Getting the following 404 error

{
    "status": 404,
    "error": "Not Found",
    "exception": "#<AbstractController::ActionNotFound: The action 'new' could not be found for DeviseTokenAuth::RegistrationsController>",
    "traces": #too long to post; 46 traces; none includes user created files
}

Here's everything I did

  • Created a new Rails API project

    rails new untitled --javascript=jquery --api
    
  • Added this to Gemfile

    #authentication
    gem 'devise'
    gem 'omniauth'
    gem 'devise_token_auth'
    
  • Ran the following

    bundle install
    rails generate devise_token_auth:install User auth 
    rails db:migrate
    rails s
    
  • Tested the url localhost:3000/auth/sign_up from browser and from Postman

My routes.rb

Rails.application.routes.draw do
  mount_devise_token_auth_for 'User', at: 'auth'
end

My rails routes output

              Prefix Verb     URI Pattern                            Controller#Action
        new_user_session GET      /auth/sign_in(.:format)                devise_token_auth/sessions#new
            user_session POST     /auth/sign_in(.:format)                devise_token_auth/sessions#create
    destroy_user_session DELETE   /auth/sign_out(.:format)               devise_token_auth/sessions#destroy
           user_password POST     /auth/password(.:format)               devise_token_auth/passwords#create
       new_user_password GET      /auth/password/new(.:format)           devise_token_auth/passwords#new
      edit_user_password GET      /auth/password/edit(.:format)          devise_token_auth/passwords#edit
                         PATCH    /auth/password(.:format)               devise_token_auth/passwords#update
                         PUT      /auth/password(.:format)               devise_token_auth/passwords#update
cancel_user_registration GET      /auth/cancel(.:format)                 devise_token_auth/registrations#cancel
       user_registration POST     /auth(.:format)                        devise_token_auth/registrations#create
   new_user_registration GET      /auth/sign_up(.:format)                devise_token_auth/registrations#new
  edit_user_registration GET      /auth/edit(.:format)                   devise_token_auth/registrations#edit
                         PATCH    /auth(.:format)                        devise_token_auth/registrations#update
                         PUT      /auth(.:format)                        devise_token_auth/registrations#update
                         DELETE   /auth(.:format)                        devise_token_auth/registrations#destroy
       user_confirmation POST     /auth/confirmation(.:format)           devise_token_auth/confirmations#create
   new_user_confirmation GET      /auth/confirmation/new(.:format)       devise_token_auth/confirmations#new
                         GET      /auth/confirmation(.:format)           devise_token_auth/confirmations#show
     auth_validate_token GET      /auth/validate_token(.:format)         devise_token_auth/token_validations#validate_token
            auth_failure GET      /auth/failure(.:format)                devise_token_auth/omniauth_callbacks#omniauth_failure
                         GET      /auth/:provider/callback(.:format)     devise_token_auth/omniauth_callbacks#omniauth_success
                         GET|POST /omniauth/:provider/callback(.:format) devise_token_auth/omniauth_callbacks#redirect_callbacks
        omniauth_failure GET|POST /omniauth/failure(.:format)            devise_token_auth/omniauth_callbacks#omniauth_failure
                         GET      /auth/:provider(.:format)              redirect(301)

Other things I've noticed

  • Other routes, for eg. /auth/sign_in work flawlessly and I'm able to sign in successfully for the users I've created using Rails Console
  • Looks like a similar error caused this post, but not caused due to subdomain constraints in routes.rb Some have mentioned in the comments even after doing like what's given in the answer did not get rid of 404

Here's what I tried then

  • Tried rails generate devise:controller users
  • Uuncommmented new section in RegistrationsController
  • Added some random render :jsons which always throws up an empty JSON with status 200 with weird bin/rails: No such file or directory error on terminal
  • Tried to get rid of it using rails app:update:bin, nothing changes :(

What am I doing wrong?

Community
  • 1
  • 1
Saravanabalagi Ramachandran
  • 8,551
  • 11
  • 53
  • 102
  • Possible [bug](https://github.com/lynndylanhurley/devise_token_auth/issues/100) on token-auth gem, but it has already been raised and its status is now closed _they seem to have tried to remove this route but then let it stay for some reasons stated [here](http://stackoverflow.com/a/7042419)_! – Saravanabalagi Ramachandran Sep 03 '16 at 15:42
  • There is no new action for [`DeviseTokenAuth::RegistrationsController`](https://github.com/lynndylanhurley/devise_token_auth/blob/master/app/controllers/devise_token_auth/registrations_controller.rb) which makes a lot of sense when you consider that token based authentication is mostly used for API's or single page applications where rendering the new route is done in javascript and not in rails. – max Sep 03 '16 at 20:26
  • In general you don't have `new` or `edit` actions in API applications as you are not rendering forms to be used in a web interface. For an API client `GET /some_resource/new` makes no sense at all. – max Sep 03 '16 at 20:28
  • what about public APIs? how would they tell what's required to register a new user via API rather than via docs – Saravanabalagi Ramachandran Sep 04 '16 at 05:09

1 Answers1

3

As stated in the README documentation of devise_token_auth

Why are the new routes included if this gem doesn't use them?

Removing the new routes will require significant modifications to devise. If the inclusion of the new routes is causing your app any problems, post an issue in the issue tracker and it will be addressed ASAP.

new and edit functionality is not provided by devise_token_auth, but the routes would still display them as the routes are coming from devise which devise_token_auth is built upon and hard to undo those routes.

Typically you don't need that functionality anyway for API based application, as that logic is built into the API client (for ex: angular.js or react.js). If for some reason, you need to let the API client know of the data required for create or update, you could document it or when the API client provides wrong data, you could provide the required fields information as part of the errors.

Dharam Gollapudi
  • 6,328
  • 2
  • 34
  • 42