Skip to content

Instantly share code, notes, and snippets.

@dnlserrano
Last active October 18, 2017 01:40
Show Gist options
  • Save dnlserrano/9722769 to your computer and use it in GitHub Desktop.
Save dnlserrano/9722769 to your computer and use it in GitHub Desktop.
Custom Token Authentication SessionController with DRY Authenticatable Logout
json.success true
json.data do
json.auth_token @user.authentication_token
json.message "login successful"
end
json.success true
json.data do
json.message "logout successful"
end
json.sucess false
json.data do
json.message "login failed"
end
class SessionsController < Devise::SessionsController
skip_before_filter :verify_authenticity_token, if: :json_request?
acts_as_token_authentication_handler_for User
skip_before_filter :authenticate_entity_from_token!
skip_before_filter :authenticate_entity!
before_filter :authenticate_entity_from_token!, :only => [:destroy]
before_filter :authenticate_entity!, :only => [:destroy]
def create
warden.authenticate!(:scope => resource_name, :recall => "sessions#failure")
@user = current_user
# renders destroy.json.jbuilder
end
def destroy
@user = current_user
@user.authentication_token = nil
@user.save
# renders destroy.json.jbuilder
end
def failure
# renders failure.json.jbuilder
end
protected
def json_request?
request.format.json?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment