1

I'm building a Rails 5 JSON API with the jsonapi-resources gem. I've found it simple to use, except since I tried to pair it with Devise. I keep getting a 404 error.

I've tried to pair a Users::RegistrationResource with a Devise Users::RegistrationsController. Here's the code I have.

app/controllers/application_controller.rb

class ApplicationController < ActionController::API
  include JSONAPI::ActsAsResourceController
end

app/controllers/users/registrations_controller.rb

class Users::RegistrationsController < Devise::RegistrationsController
  include JSONAPI::ActsAsResourceController
end

app/resources/users/registration_resource.rb

class Users::RegistrationResource < JSONAPI::Resource
  model_name 'User'
  attributes :email, :password, :password_confirmation
end

config/routes.rb

             Prefix Verb   URI Pattern                        Controller#Action
          auth_user POST   /auth_user(.:format)               authentication#authenticate_user
users_registrations GET    /users/registrations(.:format)     users/registrations#index
                    POST   /users/registrations(.:format)     users/registrations#create
 users_registration GET    /users/registrations/:id(.:format) users/registrations#show
                    PATCH  /users/registrations/:id(.:format) users/registrations#update
                    PUT    /users/registrations/:id(.:format) users/registrations#update
                    DELETE /users/registrations/:id(.:format) users/registrations#destroy

rake routes

users_registrations POST   /users/registrations(.:format)     users/registrations#create
          auth_user POST   /auth_user(.:format)               authentication#authenticate_user
                    GET    /users/registrations(.:format)     users/registrations#index
                    POST   /users/registrations(.:format)     users/registrations#create
 users_registration GET    /users/registrations/:id(.:format) users/registrations#show
                    PATCH  /users/registrations/:id(.:format) users/registrations#update
                    PUT    /users/registrations/:id(.:format) users/registrations#update
                    DELETE /users/registrations/:id(.:format) users/registrations#destroy
               root GET    /                                  pages#index

rails server

Started POST "/users/registrations" for ::1 at 2016-10-14 18:08:21 +0100
Processing by Users::RegistrationsController#create as */*
  Parameters: {"data"=>{"type"=>"users", "attributes"=>{"email"=>"neil@examples.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}}}
[Devise] Could not find devise mapping for path "/users/registrations".
This may happen for two reasons:

1) You forgot to wrap your route inside the scope block. For example:

  devise_scope :user do
    get "/some/route" => "some_devise_controller"
  end

2) You are testing a Devise controller bypassing the router.
   If so, you can explicitly tell Devise which mapping to use:

   @request.env["devise.mapping"] = Devise.mappings[:user]


Completed 404 Not Found in 0ms (ActiveRecord: 0.0ms)



AbstractController::ActionNotFound (Could not find devise mapping for path "/users/registrations".
This may happen for two reasons:

1) You forgot to wrap your route inside the scope block. For example:

  devise_scope :user do
    get "/some/route" => "some_devise_controller"
  end

2) You are testing a Devise controller bypassing the router.
   If so, you can explicitly tell Devise which mapping to use:

   @request.env["devise.mapping"] = Devise.mappings[:user]

):

This is the data I'm posting with Postman:

POST http://localhost:3000/users/registrations
Content-Type application/vnd.api+json
{
 "data": {
    "type": "users",
    "attributes": {
        "email": "neil@examples.com",
        "password": "Whatevs",
        "password_confirmation": "Whatevs"
    }
  } 
}

I guess in the first instance it's a problem with routes. Can anyone help?

Neil Wheatley
  • 71
  • 1
  • 10
  • Devise is really built for classical web apps - 3/4 of the stuff it does only really makes sense in that context. Beating the complicated beast that is devise into a JSON API is going to be PITA. – max Oct 14 '16 at 17:43
  • You may want to look at alternatives like [Knock](https://github.com/nsarno/knock) that are built from the ground up as lightweight token based authentication for API's. – max Oct 14 '16 at 17:45
  • Thanks @max, Knock does look interesting. I guess it would mean building my own user model, registrations controller, passwords controller and so on though, which is what I wanted from Devise. JWT I already have a solution for. – Neil Wheatley Oct 14 '16 at 21:45
  • @NeilWheatley did you manage to run Devise with JSONAPI? – Stéphane Bruckert Jun 15 '17 at 12:55
  • @StéphaneBruckert No, I ended up building my own user system. – Neil Wheatley Jun 18 '17 at 21:07

0 Answers0