Richlewis
Richlewis

Reputation: 15374

Create Devise user via JSON call

I am learning how to make and handle api calls with rails. I am using Devise to handle my user authentication/sign_up etc, but seem to be having issues making a post request.

To allow a user to sign_up should i be creating my own registrations controller and inherit from devises? or is there a way to deal with json requests through devises controller?

One example I have seen which would could make the process simpler is

class RegistrationsController < Devise::RegistrationsController
  respond_to :json
    def create
      super
    end
end

Routes

constraints(subdomain: 'api') do
  devise_for :users, path: 'lnf', controllers: { registrations: "registrations" }
end

So my base url will look like api.local.dev:3000.

But with this approach, does this mean I could make a curl request to the following url?

 http://api.local.dev:3000/lnf/users/sign_up

Upvotes: 3

Views: 2988

Answers (1)

Maxim
Maxim

Reputation: 9961

You should disable CSRF protection and enable JSON response type:

class RegistrationsController < Devise::RegistrationsController

  skip_before_action :verify_authenticity_token

  respond_to :json

end  

then just call POST request with parameters and format suffix (.json)

curl -X POST \
  --form "user[email][email protected]" \
  --form "user[password]=password" \
  --form "user[password_confirmation]=password" \
  'http://localhost:3000/users.json'

Upvotes: 1

Related Questions