Forked from nickhoffman/app-controllers-sessions_controller.rb
Created
September 26, 2012 09:52
-
-
Save simonoff/3787090 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SessionsController < Devise::SessionsController | |
def create | |
if request.xhr? | |
resource = warden.authenticate! :scope => resource_name | |
if resource.nil? | |
render :json => { | |
:status => 'fail', | |
:data => { | |
:cause => 'invalid', | |
:message => I18n.t('devise.failure.invalid'), | |
}, | |
} | |
else | |
sign_in resource_name, resource | |
render :json => { | |
:status => 'success', | |
:data => {:message => I18n.t('devise.sessions.signed_in')}, | |
} | |
end | |
else | |
super | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SessionFailure < Devise::FailureApp | |
def respond | |
message = I18n.t 'devise.failure.invalid' | |
cause = 'invalid' | |
if warden_options[:message] == :locked | |
message = I18n.t 'devise.failure.locked' | |
cause = 'account_locked' | |
end | |
if request.xhr? | |
self.status = 200 | |
self.content_type = 'json' | |
self.response_body = { | |
:status => 'fail', | |
:data => { :message => message, :cause => cause }, | |
}.to_json | |
else | |
super | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Devise.setup do |config| | |
config.http_authenticatable_on_xhr = false | |
config.navigational_formats = [:html] | |
config.warden do |manager| | |
manager.failure_app = SessionFailure | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment