Module: GrapeOnRails::Authenticator

Includes:
Support
Defined in:
lib/grape_on_rails/authenticator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Support

#class_name, #gor_translate

Instance Attribute Details

#current_userObject

Returns the value of attribute current_user.



5
6
7
# File 'lib/grape_on_rails/authenticator.rb', line 5

def current_user
  @current_user
end

Instance Method Details

#authenticate!Object

Raises:

  • (APIError::Unauthenticated)


7
8
9
10
11
# File 'lib/grape_on_rails/authenticator.rb', line 7

def authenticate!
  @current_user = GoR.models.user_token.constantize
    .find_token!(token_on_header)&.public_send(GoR.models.user.downcase)
  raise APIError::Unauthenticated unless current_user
end

#refresh_token!Object



13
14
15
16
17
# File 'lib/grape_on_rails/authenticator.rb', line 13

def refresh_token!
  token = GoR.models.user_token.constantize.find_by refresh_token: token_on_header
  token ? token.renew! : raise(APIError::Unauthenticated)
  token
end

#token_on_headerObject



19
20
21
22
# File 'lib/grape_on_rails/authenticator.rb', line 19

def token_on_header
  auth_header = request.headers[GoR.access_token_header]
  auth_header&.scan(/^#{GoR.access_token_value_prefix} (.+)$/i)&.flatten&.[]0
end