Skip to content

Instantly share code, notes, and snippets.

@mcfiredrill
Created March 30, 2013 20:52
Show Gist options
  • Save mcfiredrill/5278283 to your computer and use it in GitHub Desktop.
Save mcfiredrill/5278283 to your computer and use it in GitHub Desktop.
json login
█▓▒░tony@kiwi░▒▓█ ~$ curl -i -H 'Content-Type: application/json' -H 'Accept: application/json' -X POST -d '{"user":{"login":"tony", "password":"f0ibles1"}}' http://localhost:3000/login
HTTP/1.1 406 Not Acceptable
Content-Type: application/json; charset=utf-8
X-Ua-Compatible: IE=Edge
Cache-Control: no-cache
X-Request-Id: e0d06fea7951ed28ecd1c788612f55e9
X-Runtime: 0.151305
Server: WEBrick/1.3.1 (Ruby/1.9.3/2013-02-13)
Date: Sat, 30 Mar 2013 20:51:24 GMT
Content-Length: 1
Connection: Keep-Alive
Set-Cookie: _datafruits-rails_session=BAh7BkkiD3Nlc3Npb25faWQGOgZFRkkiJTBiZTViOGY1ZjA5YzQyZmQyZDg5NjA5NTk0YjI4YjlkBjsAVA%3D%3D--72bf9e2f5ceb0a02c626f96de653589d51e78021; path=/; HttpOnly
class SessionsController < Devise::SessionsController
def create
resource = warden.authenticate!(:scope => resource_name, :recall => :failure)
return sign_in_and_redirect(resource_name, resource)
end
def sign_in_and_redirect(resource_or_scope, resource=nil)
scope = Devise::Mapping.find_scope!(resource_or_scope)
resource ||= resource_or_scope
sign_in(scope, resource) unless warden.user(scope) == resource
respond_to do |format|
format.html {
set_flash_message(:notice, :signed_in) if is_navigational_format?
respond_with resource, :location => after_sign_in_path_for(resource)
}
format.json {
render :json => {:success => true, :redirect => stored_location_for(scope) || after_sign_in_path_for(resource)}
}
end
end
def failure
respond_to do |format|
format.html {
respond_with resource, :location => after_sign_in_path_for(resource)
}
format.json {
render :json => {:success => false, :errors => ["Login failed."]}
}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment