Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User Registration in Devise through API

I have recently started developing REST APIs with rails 5. I am trying to do User crude actions through API calls. I have set up a basic devise for users.

In my routes I have this:

Rails.application.routes.draw do
  devise_for :users, :controllers => {sessions: 'sessions', registrations: 'registrations'}
end

I created two separate controllers sessions_controller and registrations_controller to override devise methods and make it accept JSON format.

class RegistrationsController < Devise::RegistrationsController
    respond_to :json
end

and

class SessionsController < Devise::SessionsController
    respond_to :json
end

I am trying to register a user passing in the following data:

{  
    user: {
        email: [email protected],
        password: password,
        password_confirmation: password
    }
}

I get a status 200. but in my rails server I'm getting the following error:

Started POST "/users" for 23.233.9.94 at 2016-12-22 08:22:11 +0000
Processing by RegistrationsController#create as */*
  Parameters: {"Payload: {  \r\n    user: {\r\n        email: [email protected],\r\n        password: password,\r\n        password_confirmation: password\r\n    }\r\n}"=>"[FILTERED]"}
   (0.2ms)  begin transaction
   (0.1ms)  rollback transaction
Completed 200 OK in 5ms (Views: 0.2ms | ActiveRecord: 0.3ms)

So I basically have 2 questions. How do I format the JSON so that my devise controllers recognize it and save it to the database. And my second question is how do I pass in a CSRF token. A little example would really help me out here. Thanks in advance.

like image 741
Shadid Avatar asked Dec 22 '16 08:12

Shadid


1 Answers

Your JSON is not valid.You need to enclose the string with double quotes

{  
    "user": {
        "email": "[email protected]",
        "password": "password",
        "password_confirmation": "password"
    }
}
like image 90
Aniket Tiwari Avatar answered Sep 27 '22 16:09

Aniket Tiwari