Class: Devise::Api::TokensController

Inherits:
Object
  • Object
show all
Defined in:
app/controllers/devise/api/tokens_controller.rb

Instance Method Summary collapse

Instance Method Details

#infoObject

rubocop:enable Metrics/AbcSize



75
76
77
78
79
80
# File 'app/controllers/devise/api/tokens_controller.rb', line 75

def info
  token_response = Devise::Api::Responses::TokenResponse.new(request, token: current_devise_api_token,
                                                                      action: __method__)

  render json: token_response.body, status: token_response.status
end

#refreshObject

rubocop:disable Metrics/AbcSize



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'app/controllers/devise/api/tokens_controller.rb', line 106

def refresh
  unless Devise.api.config.refresh_token.enabled
    error_response = Devise::Api::Responses::ErrorResponse.new(request,
                                                               resource_class: resource_class,
                                                               error: :refresh_token_disabled)

    return render json: error_response.body, status: error_response.status
  end

  if current_devise_api_refresh_token.blank?
    error_response = Devise::Api::Responses::ErrorResponse.new(request, error: :invalid_token,
                                                                        resource_class: resource_class)

    return render json: error_response.body, status: error_response.status
  end

  if current_devise_api_refresh_token.revoked?
    error_response = Devise::Api::Responses::ErrorResponse.new(request, error: :revoked_token,
                                                                        resource_class: resource_class)

    return render json: error_response.body, status: error_response.status
  end

  Devise.api.config.before_refresh.call(current_devise_api_refresh_token, request)

  service = Devise::Api::TokensService::Refresh.new(devise_api_token: current_devise_api_refresh_token).call

  if service.success?
    token_response = Devise::Api::Responses::TokenResponse.new(request, token: service.success,
                                                                        action: __method__)

    Devise.api.config.after_successful_refresh.call(service.success.resource_owner, service.success, request)

    return render json: token_response.body, status: token_response.status
  end

  error_response = Devise::Api::Responses::ErrorResponse.new(request,
                                                             resource_class: resource_class,
                                                             **service.failure)

  render json: error_response.body, status: error_response.status
end

#revokeObject

rubocop:disable Metrics/AbcSize



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/controllers/devise/api/tokens_controller.rb', line 83

def revoke
  Devise.api.config.before_revoke.call(current_devise_api_token, request)

  service = Devise::Api::TokensService::Revoke.new(devise_api_token: current_devise_api_token).call

  if service.success?
    token_response = Devise::Api::Responses::TokenResponse.new(request, token: service.success,
                                                                        action: __method__)

    Devise.api.config.after_successful_revoke.call(service.success&.resource_owner, service.success, request)

    return render json: token_response.body, status: token_response.status
  end

  error_response = Devise::Api::Responses::ErrorResponse.new(request,
                                                             resource_class: resource_class,
                                                             **service.failure)

  render json: error_response.body, status: error_response.status
end

#sign_inObject

rubocop:disable Metrics/AbcSize



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/controllers/devise/api/tokens_controller.rb', line 48

def 
  Devise.api.config..call(, request, resource_class)

  service = Devise::Api::ResourceOwnerService::SignIn.new(params: ,
                                                          resource_class: resource_class).call

  if service.success?
    token = service.success

    call_devise_trackable!(token.resource_owner)

    token_response = Devise::Api::Responses::TokenResponse.new(request, token: service.success,
                                                                        action: __method__)

    Devise.api.config..call(token.resource_owner, token, request)

    return render json: token_response.body, status: token_response.status
  end

  error_response = Devise::Api::Responses::ErrorResponse.new(request,
                                                             resource_class: resource_class,
                                                             **service.failure)

  render json: error_response.body, status: error_response.status
end

#sign_upObject

rubocop:disable Metrics/AbcSize



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/controllers/devise/api/tokens_controller.rb', line 14

def 
  unless Devise.api.config..enabled
    error_response = Devise::Api::Responses::ErrorResponse.new(request, error: :sign_up_disabled,
                                                                        resource_class: resource_class)

    return render json: error_response.body, status: error_response.status
  end

  Devise.api.config..call(, request, resource_class)

  service = Devise::Api::ResourceOwnerService::SignUp.new(params: ,
                                                          resource_class: resource_class).call

  if service.success?
    token = service.success

    call_devise_trackable!(token.resource_owner)

    token_response = Devise::Api::Responses::TokenResponse.new(request, token: token, action: __method__)

    Devise.api.config..call(token.resource_owner, token, request)

    return render json: token_response.body, status: token_response.status
  end

  error_response = Devise::Api::Responses::ErrorResponse.new(request,
                                                             resource_class: resource_class,
                                                             **service.failure)

  render json: error_response.body, status: error_response.status
end