module Commons::Authentication::AuthenticateByJWT

Public Instance Methods

authorize_jwt!() click to toggle source
# File lib/commons/authentication/authenticate_by_jwt.rb, line 4
def authorize_jwt!
  raise Commons::Errors::Unauthorized if jwt.blank?

  begin
    decoded = JSONWebToken.decode(jwt)
    @current_user = UserRepository.instance.find_by(id: decoded[:user_id])
  rescue ActiveRecord::RecordNotFound => e
    raise Commons::Errors::Unauthorized
  rescue JWT::DecodeError => e
    raise Commons::Errors::Unauthorized
  end
end

Private Instance Methods

jwt() click to toggle source
# File lib/commons/authentication/authenticate_by_jwt.rb, line 19
def jwt
  jwt ||= request.headers['Authorization']
  return nil unless jwt.instance_of? String

  jwt = jwt.split(' ').last if jwt
end