Last active
October 18, 2017 01:40
-
-
Save dnlserrano/9722769 to your computer and use it in GitHub Desktop.
Custom Token Authentication SessionController with DRY Authenticatable Logout
This file contains 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
json.success true | |
json.data do | |
json.auth_token @user.authentication_token | |
json.message "login successful" | |
end |
This file contains 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
json.success true | |
json.data do | |
json.message "logout successful" | |
end |
This file contains 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
json.sucess false | |
json.data do | |
json.message "login failed" | |
end |
This file contains 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 | |
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