module HasJwtToken::Authenticatable::ClassMethods

Public Instance Methods

find_with_jwt(jwt_token) click to toggle source
# File lib/has_jwt_token/authenticatable.rb, line 8
def find_with_jwt(jwt_token)
  payload = decode!(jwt_token)
  find_by(authenticate_by => payload[authenticate_by])
    &.tap { |model| model.token = model.encode }
rescue JWT::DecodeError
  raise HasJwtToken::InvalidToken, 'Invalid token has been provided.'
end

Private Instance Methods

authenticate_by() click to toggle source
# File lib/has_jwt_token/authenticatable.rb, line 18
def authenticate_by
  @authenticate_by ||= begin
    auth_by_attr = has_jwt_token.authenticate_by.to_s
    return auth_by_attr if column_names.include?(auth_by_attr)

    raise HasJwtToken::BadConfiguration, "#{auth_by_attr.inspect} must be one of model attributes."
  end
end
decode!(token) click to toggle source
# File lib/has_jwt_token/authenticatable.rb, line 27
def decode!(token)
  HasJwtToken::JwtProxy.decode!(
    token: token,
    algorithm: has_jwt_token.algorithm,
    secret: has_jwt_token.secret
  )
end