Sha256: 13c42bbb8d4873c25d7d100e704aba82c50ae77b043ce01808064dab319d59b4

Contents?: true

Size: 789 Bytes

Versions: 6

Compression:

Stored size: 789 Bytes

Contents

# frozen_string_literal: true

#
# From https://gist.github.com/josevalim/fb706b1e933ef01e4fb6
#
module Renalware
  module API
    class TokenAuthenticatedApiController < ApplicationController
      before_action :authenticate_user_from_token!
      before_action :authenticate_user! # fallback

      private

      def authenticate_user_from_token!
        username = params[:username].presence
        user = username && User.find_by(username: username)

        # Notice how we use Devise.secure_compare to compare the token
        # in the database with the token given in the params, mitigating
        # timing attacks.
        if user && Devise.secure_compare(user.authentication_token, params[:token])
          sign_in user, store: false
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
renalware-core-2.0.23 app/controllers/renalware/api/token_authenticated_api_controller.rb
renalware-core-2.0.22 app/controllers/renalware/api/token_authenticated_api_controller.rb
renalware-core-2.0.21 app/controllers/renalware/api/token_authenticated_api_controller.rb
renalware-core-2.0.20 app/controllers/renalware/api/token_authenticated_api_controller.rb
renalware-core-2.0.18 app/controllers/renalware/api/token_authenticated_api_controller.rb
renalware-core-2.0.17 app/controllers/renalware/api/token_authenticated_api_controller.rb