1

I would like to be able to authenticate through a curl style !

So I would like to get a Token when I signed in :

curl -X POST 'http://localhost:3000/users/sign_in.json' -d 'user[email]=em...@provider.us&user[password]=pass' 

But if I perform this action I only get :

{"email":"em...@provider.us","name":"Name of the user"} 

How I could add some specific fields like authentication_token ?

Is what I want to do is right ? Is there any other better way to do this ?

Thanks !

Arkan
  • 6,196
  • 3
  • 38
  • 54
  • In the case of logging out, I think you'd like to read this: http://stackoverflow.com/questions/9362910/rails-warning-cant-verify-csrf-token-authenticity-for-json-devise-requests – Diego Sep 12 '12 at 03:24

1 Answers1

3

I created a Controller named SessionController

class Users::SessionsController < Devise::SessionsController
  append_view_path 'app/views/devise'
    def create
      respond_to do |format|
        format.html { super }
        format.json {
          warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new")
          current_user.ensure_authentication_token!
          render :json => {:user => current_user.api_attributes, :auth_token => current_user.authentication_token}.to_json, :status => :ok
        }
      end
    end

    def destroy
      respond_to do |format|
        format.html { super }
        format.json {
          warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new")
          current_user.empty_authentication_token!
          render :json => {}.to_json, :status => :ok
        }
      end
    end
end

And added this to the root file :

devise_for :users, :controllers => { :sessions => "users/sessions" }

And it works :-)

Arkan
  • 6,196
  • 3
  • 38
  • 54
  • I tried this but I still am unable to log out using Curl. It just redirects me to sign_in.json. What was the actual Curl command you used to sign out. This didnt work: http://stackoverflow.com/questions/7997009/rails-3-calling-destroy-method-with-curl @Arkan – JohnMerlino Nov 04 '11 at 12:39