Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
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
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