Module: Commons::Authentication::AuthenticateByJWT

Defined in:
lib/commons/authentication/authenticate_by_jwt.rb

Instance Method Summary collapse

Instance Method Details

#authorize_jwt!Object



4
5
6
7
8
9
10
11
12
13
14
15
# 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