Skip to content

Instantly share code, notes, and snippets.

@dnlserrano
Last active October 18, 2017 01:40

Revisions

  1. dnlserrano revised this gist Mar 24, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions sessions_controller.rb
    Original file line number Diff line number Diff line change
    @@ -16,6 +16,7 @@ def create
    def destroy
    @user = current_user
    @user.authentication_token = nil
    @user.save
    # renders destroy.json.jbuilder
    end

  2. dnlserrano revised this gist Mar 23, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions sessions_controller.rb
    Original file line number Diff line number Diff line change
    @@ -20,6 +20,7 @@ def destroy
    end

    def failure
    # renders failure.json.jbuilder
    end

    protected
  3. dnlserrano created this gist Mar 23, 2014.
    5 changes: 5 additions & 0 deletions create.json.jbuilder
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    json.success true
    json.data do
    json.auth_token @user.authentication_token
    json.message "login successful"
    end
    4 changes: 4 additions & 0 deletions destroy.json.jbuilder
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    json.success true
    json.data do
    json.message "logout successful"
    end
    4 changes: 4 additions & 0 deletions failure.json.jbuilder
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    json.sucess false
    json.data do
    json.message "login failed"
    end
    30 changes: 30 additions & 0 deletions sessions_controller.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    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
    # renders destroy.json.jbuilder
    end

    def failure
    end

    protected

    def json_request?
    request.format.json?
    end
    end