Skip to content

Instantly share code, notes, and snippets.

@kolach
Created May 29, 2010 12:02
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kolach/418243 to your computer and use it in GitHub Desktop.
Save kolach/418243 to your computer and use it in GitHub Desktop.
class Users::SessionsController < Devise::SessionsController
# Have to reimplement :recall => "failure"
# for warden to redirect to some action that will return what I want
def create
resource = warden.authenticate!(:scope => resource_name, :recall => "failure")
# set_flash_message :notice, :signed_in
sign_in_and_redirect(resource_name, resource)
end
# Example of JSON response
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
render :json => { :success => true, :redirect => stored_location_for(scope) || after_sign_in_path_for(resource) }
end
# JSON login failure message
def failure
render :json => {:success => false, :errors => {:reason => "Login failed. Try again"}}
end
end
@nnbelous
Copy link

nnbelous commented Aug 9, 2010

Здравствуйте, я пытаюсь решить подобную проблему, подружить devise с json api, что то не получается, у вас получилось?
сейчас существует проблема, когда делаю свой контроллер Users::SessionsController < Devise::SessionsController то на resource = warden.authenticate!(:scope => resource_name, :recall => "failure") всегда идет на failure. Не могу понять в чем дело. Если вы знаете решение данной проблемы буду признателен любой помощи :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment