Class: Devise::Api::Responses::TokenResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/devise/api/responses/token_response.rb

Constant Summary collapse

ACTIONS =
%i[
  sign_in
  sign_up
  refresh
  revoke
  info
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request, token:, action:) ⇒ TokenResponse

Returns a new instance of TokenResponse.



23
24
25
26
27
28
# File 'lib/devise/api/responses/token_response.rb', line 23

def initialize(request, token:, action:)
  @request = request
  @token = token
  @action = action
  @resource_owner = token&.resource_owner
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



7
8
9
# File 'lib/devise/api/responses/token_response.rb', line 7

def action
  @action
end

#requestObject (readonly)

Returns the value of attribute request.



7
8
9
# File 'lib/devise/api/responses/token_response.rb', line 7

def request
  @request
end

#resource_ownerObject (readonly)

Returns the value of attribute resource_owner.



7
8
9
# File 'lib/devise/api/responses/token_response.rb', line 7

def resource_owner
  @resource_owner
end

#tokenObject (readonly)

Returns the value of attribute token.



7
8
9
# File 'lib/devise/api/responses/token_response.rb', line 7

def token
  @token
end

Instance Method Details

#bodyObject



30
31
32
33
34
35
36
# File 'lib/devise/api/responses/token_response.rb', line 30

def body
  return {} if revoke_action?
  return signed_up_body if 
  return info_body if info_action?

  default_body
end

#default_bodyObject



38
39
40
41
42
43
44
45
46
# File 'lib/devise/api/responses/token_response.rb', line 38

def default_body
  {
    token: token.access_token,
    refresh_token: Devise.api.config.refresh_token.enabled ? token.refresh_token : nil,
    expires_in: token.expires_in,
    token_type: ::Devise.api.config.authorization.scheme,
    resource_owner: default_resource_owner
  }.compact
end

#default_resource_ownerObject



48
49
50
51
52
53
54
55
# File 'lib/devise/api/responses/token_response.rb', line 48

def default_resource_owner
  keys_to_extract = %i[id email created_at updated_at]
  if Devise.api.config..extra_fields.present?
    keys_to_extract |= Devise.api.config..extra_fields.map(&:to_sym)
  end

  resource_owner.slice(*keys_to_extract)
end

#statusObject



57
58
59
60
61
62
# File 'lib/devise/api/responses/token_response.rb', line 57

def status
  return :created if 
  return :no_content if revoke_action?

  :ok
end