Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create User in Devise from JSON

I'm working on integrating my Rails 3.1 app running Devise for user authentication with my iOS app. I'd like the user to be able to register from the application, and then I can store those credentials to login later.

Using RestKit, I do this:

-(IBAction)registerUser:(id)sender {
    NSDictionary *params = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:
                                                                self.email.text,
                                                                self.password.text,
                                                                self.confirmPassword.text,
                                                                nil]
                                                       forKeys:[NSArray arrayWithObjects:
                                                                @"email",
                                                                @"password",
                                                                @"password_confirmation",
                                                                nil]];

    [[RKClient sharedClient] post:@"/users.json" params:params delegate:self];
}

The /users.json url goes to: user_registration POST /users(.:format) {:action=>"create", :controller=>"devise/registrations"} (according to rake routes). It appears that the post call accepts different formats, so I assume it will accept JSON. My Post request is serialized as JSON, and sent off. The server gets it, and this is the log:

Started POST "/users.json" for 129.21.84.10 at 2012-01-12 15:33:57 -0500
  Processing by Devise::RegistrationsController#create as JSON
  Parameters: {"password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "email"=>"[email protected]"}
WARNING: Can't verify CSRF token authenticity
  User Load (0.9ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
   (0.3ms)  BEGIN
   (0.2ms)  COMMIT
   (0.2ms)  BEGIN
   (0.4ms)  ROLLBACK
Completed 422 Unprocessable Entity in 93ms (Views: 3.8ms | ActiveRecord: 10.2ms)

I get a 422 error, and my user is not created. The response my iOS app gets is: Response: {"email":["can't be blank"],"password":["can't be blank"]}. But the password and email isn't blank, the server got them successfully. So, something isn't working right, and I'm not sure where to go. How can I create the user using JSON?

Thanks for helping!

like image 575
Ethan Mick Avatar asked Jan 12 '12 20:01

Ethan Mick


2 Answers

I'm just working on the same thing myself - creating users in Devise from an iPhone app. I've got it working a treat by POSTing json that looks like this:

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

and the URL:

http://localhost:3000/users.json

and the request header Content-Type of my post is 'application/json'.

All standard Devise config.

like image 166
ED-209 Avatar answered Oct 05 '22 16:10

ED-209


Your rails app is expecting JSON in this form :

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

But by default RestKit is sending this :

{"email":"[email protected]", "password":"Test123", "password_confirmation":"Test123"}

To add "user" you just need to set the rootKeyPath like this:

RKObjectMapping *userSerializationMapping = [userMapping inverseMapping];
userSerializationMapping.rootKeyPath = @"user";
[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:userSerializationMapping forClass:[User class]];
like image 45
mrabin Avatar answered Oct 05 '22 17:10

mrabin