Sha256: 04f88061615ab7c1c5df0ffc202dc99e84edefae6f23c047e1b9da75915de4a2

Contents?: true

Size: 1.69 KB

Versions: 2

Compression:

Stored size: 1.69 KB

Contents

# see http://www.emilsoman.com/blog/2013/05/18/building-a-tested/
module DeviseTokenAuth
  class SessionsController < DeviseTokenAuth::ApplicationController
    before_filter :set_user_by_token, :only => [:destroy]
    prepend_before_filter :allow_params_authentication!, only: :create
    prepend_before_filter only: [ :create, :destroy ] { request.env["devise.skip_timeout"] = true }

    def create
      self.resource = warden.authenticate!(auth_options)
      sign_in(resource_name, resource, :store => false)
      @user = resource
      @client_id = SecureRandom.urlsafe_base64(nil, false)
      @token     = SecureRandom.urlsafe_base64(nil, false)

      @user.tokens[@client_id] = {
        token: BCrypt::Password.create(@token),
        expiry: (Time.now + DeviseTokenAuth.token_lifespan).to_i
      }
      @user.save
      yield resource if block_given?
      render json: resource_serializer(resource)
    end

    def auth_options
      { scope: resource_name, recall: "#{controller_path}#new" }
    end

    def destroy
      # remove auth instance variables so that after_filter does not run
      user = remove_instance_variable(:@user) if @user
      client_id = remove_instance_variable(:@client_id) if @client_id
      remove_instance_variable(:@token) if @token

      if user and client_id and user.tokens[client_id]
        user.tokens.delete(client_id)
        user.save!

        render json: success_message, status: 200

      else
        render json: error_messages("User was not found or was not logged in."), status: 404
      end
    end

    def resource_serializer(user)
      serializer = DeviseTokenAuth.session_serializer || ResourceSerializer
      serializer.new(user)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
xing_backend_token_auth-0.1.32 app/controllers/devise_token_auth/sessions_controller.rb
xing_backend_token_auth-0.1.31 app/controllers/devise_token_auth/sessions_controller.rb