Class: Devise::Rownd::User

Inherits:
Object
  • Object
show all
Defined in:
lib/devise/rownd/user.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(profile, access_token) ⇒ User

Returns a new instance of User.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/devise/rownd/user.rb', line 13

def initialize(profile, access_token)
  Devise::Rownd::Log.debug("initialize user: #{profile} - #{access_token}")
  @user_id = profile['data']['user_id']
  @data = profile
  Devise::Rownd.app_schema.each do |key, _value|
    self.class.send :attr_accessor, key
    instance_variable_value = profile['data'].is_a?(Hash) && profile['data'].key?(key) ? profile['data'][key] : nil
    instance_variable_set("@#{key}", instance_variable_value)
  end

  @access_token = access_token
  @auth_level = profile['auth_level']
  @is_verified_user = profile['auth_level'] == 'verified'

  Devise::Rownd::Log.debug('successfully initialized user')
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



11
12
13
# File 'lib/devise/rownd/user.rb', line 11

def access_token
  @access_token
end

#auth_levelObject (readonly)

Returns the value of attribute auth_level.



11
12
13
# File 'lib/devise/rownd/user.rb', line 11

def auth_level
  @auth_level
end

#dataObject (readonly)

Returns the value of attribute data.



11
12
13
# File 'lib/devise/rownd/user.rb', line 11

def data
  @data
end

#is_verified_userObject (readonly)

Returns the value of attribute is_verified_user.



11
12
13
# File 'lib/devise/rownd/user.rb', line 11

def is_verified_user
  @is_verified_user
end

#user_idObject (readonly)

Returns the value of attribute user_id.



11
12
13
# File 'lib/devise/rownd/user.rb', line 11

def user_id
  @user_id
end

Class Method Details

.fetch_user(access_token, bypass_cache = false) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/devise/rownd/user.rb', line 34

def self.fetch_user(access_token, bypass_cache = false)
  Devise::Rownd::Log.debug("fetch_user: #{self}")
  begin
    decoded_jwt = ::Devise::Rownd::Token.verify_token(access_token)
    app_id = decoded_jwt['aud'].find(/^app:.+/).first.split(':').last

    cache_key = "rownd_user_#{decoded_jwt['https://auth.rownd.io/app_user_id']}"
    if bypass_cache == true
      Devise::Rownd::Log.debug('fetch_user bypassing cache')
      profile = fetch_user_from_api(access_token, app_id)
      return nil unless profile

      Rails.cache.write(cache_key, profile, expires_in: 1.minute)
      return profile
    end

    Devise::Rownd::Log.debug('fetch_user from cache if possible')
    Devise::Rownd::Caching.fetch(cache_key, 1.minute) { fetch_user_from_api(access_token, app_id) }
  rescue StandardError => e
    Devise::Rownd::Log.error("fetch_user failed: #{e.message}")
    nil
  end
end

.fetch_user_from_api(access_token, app_id) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/devise/rownd/user.rb', line 58

def self.fetch_user_from_api(access_token, app_id)
  response = ::Devise::Rownd::API.make_api_call(
    "/me/applications/#{app_id}/data",
    {
      method: 'GET',
      headers: { 'Authorization' => "Bearer #{access_token}" }
    }
  )
  return response.body if response.success?

  Devise::Rownd::Log.error("Failed to fetch user: #{response.body}")
  nil
end

Instance Method Details

#verified?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/devise/rownd/user.rb', line 30

def verified?
  @is_verified_user
end