简体   繁体   中英

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: me@gmail.com,
        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: me@gmail.com,\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.

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

{  
    "user": {
        "email": "me@gmail.com",
        "password": "password",
        "password_confirmation": "password"
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM