graphql_devise 0.12.3 → 0.13.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (165) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +17 -0
  3. data/Gemfile +2 -0
  4. data/README.md +38 -0
  5. data/Rakefile +2 -0
  6. data/app/controllers/graphql_devise/application_controller.rb +2 -0
  7. data/app/controllers/graphql_devise/concerns/set_user_by_token.rb +6 -0
  8. data/app/controllers/graphql_devise/graphql_controller.rb +2 -0
  9. data/app/helpers/graphql_devise/application_helper.rb +2 -0
  10. data/app/helpers/graphql_devise/mailer_helper.rb +2 -0
  11. data/app/models/graphql_devise/concerns/model.rb +10 -0
  12. data/app/views/graphql_devise/mailer/confirmation_instructions.html.erb +1 -1
  13. data/app/views/graphql_devise/mailer/reset_password_instructions.html.erb +1 -1
  14. data/config/routes.rb +2 -0
  15. data/lib/generators/graphql_devise/install_generator.rb +2 -0
  16. data/lib/graphql_devise.rb +2 -0
  17. data/lib/graphql_devise/concerns/controller_methods.rb +2 -0
  18. data/lib/graphql_devise/default_operations/mutations.rb +2 -0
  19. data/lib/graphql_devise/default_operations/resolvers.rb +2 -0
  20. data/lib/graphql_devise/engine.rb +2 -0
  21. data/lib/graphql_devise/errors/authentication_error.rb +2 -0
  22. data/lib/graphql_devise/errors/detailed_user_error.rb +2 -0
  23. data/lib/graphql_devise/errors/error_codes.rb +2 -0
  24. data/lib/graphql_devise/errors/execution_error.rb +2 -0
  25. data/lib/graphql_devise/errors/user_error.rb +2 -0
  26. data/lib/graphql_devise/model/with_email_updater.rb +74 -0
  27. data/lib/graphql_devise/mount_method/operation_preparer.rb +2 -0
  28. data/lib/graphql_devise/mount_method/operation_preparers/custom_operation_preparer.rb +2 -0
  29. data/lib/graphql_devise/mount_method/operation_preparers/default_operation_preparer.rb +2 -0
  30. data/lib/graphql_devise/mount_method/operation_preparers/gql_name_setter.rb +2 -0
  31. data/lib/graphql_devise/mount_method/operation_preparers/mutation_field_setter.rb +2 -0
  32. data/lib/graphql_devise/mount_method/operation_preparers/resolver_type_setter.rb +2 -0
  33. data/lib/graphql_devise/mount_method/operation_preparers/resource_name_setter.rb +2 -0
  34. data/lib/graphql_devise/mount_method/operation_sanitizer.rb +2 -0
  35. data/lib/graphql_devise/mount_method/option_sanitizer.rb +2 -0
  36. data/lib/graphql_devise/mount_method/option_sanitizers/array_checker.rb +2 -0
  37. data/lib/graphql_devise/mount_method/option_sanitizers/class_checker.rb +2 -0
  38. data/lib/graphql_devise/mount_method/option_sanitizers/hash_checker.rb +2 -0
  39. data/lib/graphql_devise/mount_method/option_sanitizers/string_checker.rb +2 -0
  40. data/lib/graphql_devise/mount_method/option_validators/provided_operations_validator.rb +2 -0
  41. data/lib/graphql_devise/mount_method/option_validators/skip_only_validator.rb +2 -0
  42. data/lib/graphql_devise/mount_method/option_validators/supported_operations_validator.rb +2 -0
  43. data/lib/graphql_devise/mount_method/options_validator.rb +2 -0
  44. data/lib/graphql_devise/mount_method/supported_options.rb +2 -0
  45. data/lib/graphql_devise/mutations/base.rb +2 -0
  46. data/lib/graphql_devise/mutations/login.rb +2 -0
  47. data/lib/graphql_devise/mutations/logout.rb +2 -0
  48. data/lib/graphql_devise/mutations/resend_confirmation.rb +3 -1
  49. data/lib/graphql_devise/mutations/send_password_reset.rb +3 -1
  50. data/lib/graphql_devise/mutations/sign_up.rb +3 -1
  51. data/lib/graphql_devise/mutations/update_password.rb +2 -0
  52. data/lib/graphql_devise/rails/routes.rb +2 -0
  53. data/lib/graphql_devise/resolvers/base.rb +2 -0
  54. data/lib/graphql_devise/resolvers/check_password_token.rb +2 -0
  55. data/lib/graphql_devise/resolvers/confirm_account.rb +4 -2
  56. data/lib/graphql_devise/resolvers/dummy.rb +2 -0
  57. data/lib/graphql_devise/resource_loader.rb +2 -0
  58. data/lib/graphql_devise/schema.rb +2 -0
  59. data/lib/graphql_devise/schema_plugin.rb +8 -0
  60. data/lib/graphql_devise/types/authenticatable_type.rb +2 -0
  61. data/lib/graphql_devise/types/credential_type.rb +2 -0
  62. data/lib/graphql_devise/types/mutation_type.rb +2 -0
  63. data/lib/graphql_devise/types/query_type.rb +2 -0
  64. data/lib/graphql_devise/version.rb +3 -1
  65. data/spec/dummy/Rakefile +2 -0
  66. data/spec/dummy/app/controllers/api/v1/graphql_controller.rb +3 -1
  67. data/spec/dummy/app/controllers/application_controller.rb +2 -0
  68. data/spec/dummy/app/graphql/dummy_schema.rb +4 -1
  69. data/spec/dummy/app/graphql/interpreter_schema.rb +2 -0
  70. data/spec/dummy/app/graphql/mutations/login.rb +2 -0
  71. data/spec/dummy/app/graphql/mutations/register_confirmed_user.rb +2 -0
  72. data/spec/dummy/app/graphql/mutations/sign_up.rb +2 -0
  73. data/spec/dummy/app/graphql/mutations/update_user.rb +22 -0
  74. data/spec/dummy/app/graphql/resolvers/public_user.rb +2 -0
  75. data/spec/dummy/app/graphql/resolvers/user_show.rb +2 -0
  76. data/spec/dummy/app/graphql/types/base_object.rb +2 -0
  77. data/spec/dummy/app/graphql/types/custom_admin_type.rb +2 -0
  78. data/spec/dummy/app/graphql/types/mutation_type.rb +3 -0
  79. data/spec/dummy/app/graphql/types/query_type.rb +2 -0
  80. data/spec/dummy/app/graphql/types/user_type.rb +2 -0
  81. data/spec/dummy/app/jobs/application_job.rb +2 -0
  82. data/spec/dummy/app/mailers/application_mailer.rb +2 -0
  83. data/spec/dummy/app/models/admin.rb +2 -0
  84. data/spec/dummy/app/models/application_record.rb +2 -0
  85. data/spec/dummy/app/models/guest.rb +2 -0
  86. data/spec/dummy/app/models/schema_user.rb +13 -0
  87. data/spec/dummy/app/models/user.rb +2 -0
  88. data/spec/dummy/app/models/users.rb +2 -0
  89. data/spec/dummy/app/models/users/customer.rb +2 -0
  90. data/spec/dummy/config.ru +2 -0
  91. data/spec/dummy/config/application.rb +2 -0
  92. data/spec/dummy/config/environment.rb +2 -0
  93. data/spec/dummy/config/environments/development.rb +2 -0
  94. data/spec/dummy/config/environments/production.rb +2 -0
  95. data/spec/dummy/config/environments/test.rb +2 -0
  96. data/spec/dummy/config/initializers/application_controller_renderer.rb +2 -0
  97. data/spec/dummy/config/initializers/backtrace_silencers.rb +2 -0
  98. data/spec/dummy/config/initializers/cors.rb +2 -0
  99. data/spec/dummy/config/initializers/devise_token_auth.rb +2 -0
  100. data/spec/dummy/config/initializers/filter_parameter_logging.rb +2 -0
  101. data/spec/dummy/config/initializers/i18n.rb +2 -0
  102. data/spec/dummy/config/initializers/inflections.rb +2 -0
  103. data/spec/dummy/config/initializers/mime_types.rb +2 -0
  104. data/spec/dummy/config/initializers/wrap_parameters.rb +2 -0
  105. data/spec/dummy/config/puma.rb +2 -0
  106. data/spec/dummy/config/routes.rb +2 -0
  107. data/spec/dummy/config/spring.rb +2 -0
  108. data/spec/dummy/db/migrate/20190815114303_create_users.rb +2 -0
  109. data/spec/dummy/db/migrate/20190824215150_add_auth_available_to_users.rb +2 -0
  110. data/spec/dummy/db/migrate/20190916012505_create_admins.rb +2 -0
  111. data/spec/dummy/db/migrate/20191013213045_create_guests.rb +2 -0
  112. data/spec/dummy/db/migrate/20200321121807_create_users_customers.rb +2 -0
  113. data/spec/dummy/db/migrate/20200621182414_remove_uncofirmed_email_from_admins.rb +7 -0
  114. data/spec/dummy/db/migrate/20200623003142_create_schema_users.rb +46 -0
  115. data/spec/dummy/db/schema.rb +30 -2
  116. data/spec/dummy/db/seeds.rb +2 -0
  117. data/spec/factories/admins.rb +2 -0
  118. data/spec/factories/guests.rb +2 -0
  119. data/spec/factories/schema_users.rb +13 -0
  120. data/spec/factories/users.rb +2 -0
  121. data/spec/factories/users_customers.rb +2 -0
  122. data/spec/generators/graphql_devise/install_generator_spec.rb +2 -0
  123. data/spec/graphql_devise/model/with_email_updater_spec.rb +131 -0
  124. data/spec/graphql_devise_spec.rb +2 -0
  125. data/spec/models/user_spec.rb +2 -0
  126. data/spec/rails_helper.rb +3 -0
  127. data/spec/requests/graphql_controller_spec.rb +2 -0
  128. data/spec/requests/mutations/additional_mutations_spec.rb +2 -0
  129. data/spec/requests/mutations/additional_queries_spec.rb +2 -0
  130. data/spec/requests/mutations/login_spec.rb +2 -0
  131. data/spec/requests/mutations/logout_spec.rb +2 -0
  132. data/spec/requests/mutations/resend_confirmation_spec.rb +2 -0
  133. data/spec/requests/mutations/send_password_reset_spec.rb +2 -0
  134. data/spec/requests/mutations/sign_up_spec.rb +2 -0
  135. data/spec/requests/mutations/update_password_spec.rb +2 -0
  136. data/spec/requests/queries/check_password_token_spec.rb +2 -0
  137. data/spec/requests/queries/confirm_account_spec.rb +3 -1
  138. data/spec/requests/user_controller_spec.rb +57 -8
  139. data/spec/services/mount_method/operation_preparer_spec.rb +2 -0
  140. data/spec/services/mount_method/operation_preparers/custom_operation_preparer_spec.rb +2 -0
  141. data/spec/services/mount_method/operation_preparers/default_operation_preparer_spec.rb +2 -0
  142. data/spec/services/mount_method/operation_preparers/gql_name_setter_spec.rb +2 -0
  143. data/spec/services/mount_method/operation_preparers/mutation_field_setter_spec.rb +2 -0
  144. data/spec/services/mount_method/operation_preparers/resolver_type_setter_spec.rb +2 -0
  145. data/spec/services/mount_method/operation_preparers/resource_name_setter_spec.rb +2 -0
  146. data/spec/services/mount_method/operation_sanitizer_spec.rb +2 -0
  147. data/spec/services/mount_method/option_sanitizer_spec.rb +2 -0
  148. data/spec/services/mount_method/option_sanitizers/array_checker_spec.rb +2 -0
  149. data/spec/services/mount_method/option_sanitizers/class_checker_spec.rb +2 -0
  150. data/spec/services/mount_method/option_sanitizers/hash_checker_spec.rb +2 -0
  151. data/spec/services/mount_method/option_sanitizers/string_checker_spec.rb +2 -0
  152. data/spec/services/mount_method/option_validators/provided_operations_validator_spec.rb +2 -0
  153. data/spec/services/mount_method/option_validators/skip_only_validator_spec.rb +2 -0
  154. data/spec/services/mount_method/option_validators/supported_operations_validator_spec.rb +2 -0
  155. data/spec/services/mount_method/options_validator_spec.rb +2 -0
  156. data/spec/services/resource_loader_spec.rb +2 -0
  157. data/spec/services/schema_plugin_spec.rb +2 -0
  158. data/spec/spec_helper.rb +2 -0
  159. data/spec/support/contexts/graphql_request.rb +2 -0
  160. data/spec/support/factory_bot.rb +2 -0
  161. data/spec/support/matchers/auth_headers_matcher.rb +2 -0
  162. data/spec/support/matchers/not_change_matcher.rb +2 -0
  163. data/spec/support/requests/auth_helpers.rb +2 -0
  164. data/spec/support/requests/json_helpers.rb +2 -0
  165. metadata +15 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3ddce4ffb15dd4eac50fd0756808ce5fdef81a2f5fcc3405e8c00e5a7c51b689
4
- data.tar.gz: b47284dc35df29df8a9bd2ef17cb68dde81e4a183cae904220167a37efbd6a05
3
+ metadata.gz: 34880444c63dfec84dd91addc76574355469fdab6b3e0a00100ab4edde7da869
4
+ data.tar.gz: 3e3cbc8dd402f3d2e7450f301b1f0506bd804a1a62ddc7acc5b0c6c07bd892c2
5
5
  SHA512:
6
- metadata.gz: 7dfb908c7284d5aac400dac4424ab7b053cd9c2f8da6b684028d19fa651eb0bd9b76ac911063676bb923c71a882930aefb65c66f2081d441b0461fc226dddf7b
7
- data.tar.gz: 5f49d0b6b687b32de66836f9617eeb344b6c7dd77045b7a3e936f570799f9076c88f6d1abf152ba5ae54d034b5a31a0f106b0593359b742da5b7898578675633
6
+ metadata.gz: 953635cd445812d68d15e20367712b3118abb4bcea774859a80ce7265bc710c9c5917804ddb80f26868e2455591c2a96d14d10d4b5c6ff36315e18680e2d1534
7
+ data.tar.gz: 8b4bbba71c257d2be8fbc73e3c07817bc013a755fde9d13c6226f748e2ceebde975888d6f4e411584c3bb45ae4e6777cd1c8023c20a7f4fb35eb51655f44bb16
@@ -1,5 +1,22 @@
1
1
  # Changelog
2
2
 
3
+ ## [v0.13.0](https://github.com/graphql-devise/graphql_devise/tree/v0.13.0) (2020-06-22)
4
+
5
+ [Full Changelog](https://github.com/graphql-devise/graphql_devise/compare/v0.12.3...v0.13.0)
6
+
7
+ **Breaking changes:**
8
+
9
+ - Fix email reconfirmation feature [\#111](https://github.com/graphql-devise/graphql_devise/pull/111) ([mcelicalderon](https://github.com/mcelicalderon))
10
+
11
+ **Implemented enhancements:**
12
+
13
+ - Add frozen string literal to all relevant files [\#114](https://github.com/graphql-devise/graphql_devise/pull/114) ([mcelicalderon](https://github.com/mcelicalderon))
14
+
15
+ **Fixed bugs:**
16
+
17
+ - CookieOverflow for Own Schema Mount [\#112](https://github.com/graphql-devise/graphql_devise/issues/112)
18
+ - Reconfirmable not setting unconfirmed\_email [\#102](https://github.com/graphql-devise/graphql_devise/issues/102)
19
+
3
20
  ## [v0.12.3](https://github.com/graphql-devise/graphql_devise/tree/v0.12.3) (2020-06-19)
4
21
 
5
22
  [Full Changelog](https://github.com/graphql-devise/graphql_devise/compare/v0.12.2...v0.12.3)
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  gemspec
data/README.md CHANGED
@@ -301,6 +301,44 @@ end
301
301
  The install generator can do this for you if you specify the `user_class` option.
302
302
  See [Installation](#installation) for details.
303
303
 
304
+ ### Email Reconfirmation
305
+ DTA and Devise support email reconfirmation. When the `confirmable` module is added to your
306
+ resource, an email is sent to the provided email address when the `signUp` mutation is used.
307
+ You can also use this gem so every time a user updates the `email` field, a new email gets sent
308
+ for the user to confirm the new email address. Only after clicking on the confirmation link,
309
+ the email will be updated on the database to use the new value.
310
+
311
+ In order to use this feature there are a couple of things to setup first:
312
+ 1. Make user your model includes the `:confirmable` module.
313
+ 1. Add an `unconfirmed_email` String column to your resource's table.
314
+
315
+ After that is done, you simply need to call a different update method on your resource,
316
+ `update_with_email`. This method behaves exactly the same as ActiveRecord's `update` method
317
+ if the previous steps are not performed, or if you are not updating the `email` attribute.
318
+ It is also mandatory to provide two additional attributes when email will change or an error
319
+ will be raised:
320
+
321
+ 1. `schema_url`: The full url where your GQL schema is mounted. You can get this value from the
322
+ controller available in the context of your mutations and queries like this:
323
+ ```ruby
324
+ context[:controller].full_url_without_params
325
+ ```
326
+ 1. `confirmation_success_url`: This the full url where you want users to be redirected after
327
+ the email has changed successfully (usually a front-end url). This value is mandatory
328
+ unless you have set `default_confirm_success_url` in your devise_token_auth initializer.
329
+
330
+ So, it's up to you where you require confirmation of changing emails.
331
+ [Here's an example](https://github.com/graphql-devise/graphql_devise/blob/c4dcb17e98f8d84cc5ac002c66ed98a797d3bc82/spec/dummy/app/graphql/mutations/update_user.rb#L13)
332
+ on how you might do this. And also a demonstration on the method usage:
333
+ ```ruby
334
+ user.update_with_email(
335
+ name: 'New Name',
336
+ email: 'new@domain.com',
337
+ schema_url: 'http://localhost:3000/graphql',
338
+ confirmation_success_url: 'https://google.com'
339
+ )
340
+ ```
341
+
304
342
  ### Customizing Email Templates
305
343
  The approach of this gem is a bit different from DeviseTokenAuth. We have placed our templates in `app/views/graphql_devise/mailer`,
306
344
  so if you want to change them, place yours on the same dir structure on your Rails project. You can customize these two templates:
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  begin
2
4
  require 'bundler/setup'
3
5
  rescue LoadError
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  ApplicationController = if Rails::VERSION::MAJOR >= 5
3
5
  Class.new(ActionController::API)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  module Concerns
3
5
  SetUserByToken = DeviseTokenAuth::Concerns::SetUserByToken
@@ -5,6 +7,10 @@ module GraphqlDevise
5
7
  SetUserByToken.module_eval do
6
8
  attr_accessor :client_id, :token, :resource
7
9
 
10
+ def full_url_without_params
11
+ request.base_url + request.path
12
+ end
13
+
8
14
  def set_resource_by_token(resource)
9
15
  set_user_by_token(resource)
10
16
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_dependency 'graphql_devise/application_controller'
2
4
 
3
5
  module GraphqlDevise
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  module ApplicationHelper
3
5
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  module MailerHelper
3
5
  def confirmation_query(resource_name:, token:, redirect_url:)
@@ -1,5 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'graphql_devise/model/with_email_updater'
4
+
1
5
  module GraphqlDevise
2
6
  module Concerns
3
7
  Model = DeviseTokenAuth::Concerns::User
8
+
9
+ Model.module_eval do
10
+ def update_with_email(attributes = {})
11
+ GraphqlDevise::Model::WithEmailUpdater.new(self, attributes).call
12
+ end
13
+ end
4
14
  end
5
15
  end
@@ -2,4 +2,4 @@
2
2
 
3
3
  <p><%= t('.confirm_link_msg') %></p>
4
4
 
5
- <p><%= link_to t('.confirm_account_link'), url_for(controller: message['controller'], action: message['action'], **confirmation_query(resource_name: @resource.class.to_s, redirect_url: message['redirect-url'], token: @token)) %></p>
5
+ <p><%= link_to t('.confirm_account_link'), "#{message['schema_url']}?#{confirmation_query(resource_name: @resource.class.to_s, redirect_url: message['redirect-url'], token: @token).to_query}" %></p>
@@ -2,7 +2,7 @@
2
2
 
3
3
  <p><%= t('.request_reset_link_msg') %></p>
4
4
 
5
- <p><%= link_to t('.password_change_link'), url_for(controller: message['controller'], action: message['action'], **password_reset_query(token: @token, redirect_url: message['redirect-url'], resource_name: @resource.class.to_s)) %></p>
5
+ <p><%= link_to t('.password_change_link'), "#{message['schema_url']}?#{password_reset_query(token: @token, redirect_url: message['redirect-url'], resource_name: @resource.class.to_s).to_query}" %></p>
6
6
 
7
7
  <p><%= t('.ignore_mail_msg') %></p>
8
8
  <p><%= t('.no_changes_msg') %></p>
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  GraphqlDevise::Engine.routes.draw do
2
4
  # Required as Devise forces routes to reload on eager_load
3
5
  unless GraphqlDevise.schema_loaded?
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  class InstallGenerator < ::Rails::Generators::Base
3
5
  source_root File.expand_path('templates', __dir__)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rails'
2
4
  require 'graphql'
3
5
  require 'devise_token_auth'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  module Concerns
3
5
  module ControllerMethods
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'graphql_devise/mutations/base'
2
4
  require 'graphql_devise/mutations/login'
3
5
  require 'graphql_devise/mutations/logout'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'graphql_devise/resolvers/base'
2
4
  require 'graphql_devise/resolvers/check_password_token'
3
5
  require 'graphql_devise/resolvers/confirm_account'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'graphql_devise/rails/routes'
2
4
 
3
5
  module GraphqlDevise
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  class AuthenticationError < ExecutionError
3
5
  def to_h
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  class DetailedUserError < ExecutionError
3
5
  def initialize(message, errors:)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  ERROR_CODES = {
3
5
  user_error: 'USER_ERROR',
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  class ExecutionError < GraphQL::ExecutionError
3
5
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  class UserError < ExecutionError
3
5
  def to_h
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphqlDevise
4
+ module Model
5
+ class WithEmailUpdater
6
+ def initialize(resource, attributes)
7
+ @attributes = attributes
8
+ @resource = resource
9
+ end
10
+
11
+ def call
12
+ resource_attributes = @attributes.except(:schema_url, :confirmation_success_url)
13
+ return @resource.update(resource_attributes) unless requires_reconfirmation?(resource_attributes)
14
+
15
+ @resource.assign_attributes(resource_attributes)
16
+
17
+ if @resource.email == email_in_database
18
+ return @resource.save
19
+ elsif required_reconfirm_attributes?
20
+ return false unless @resource.valid?
21
+
22
+ store_unconfirmed_email
23
+ saved = @resource.save
24
+ send_confirmation_instructions(saved)
25
+
26
+ saved
27
+ else
28
+ raise(
29
+ GraphqlDevise::Error,
30
+ 'Method `update_with_email` requires attributes `confirmation_success_url` and `schema_url` for email reconfirmation to work'
31
+ )
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def required_reconfirm_attributes?
38
+ @attributes[:schema_url].present? &&
39
+ (@attributes[:confirmation_success_url].present? || DeviseTokenAuth.default_confirm_success_url.present?)
40
+ end
41
+
42
+ def requires_reconfirmation?(resource_attributes)
43
+ resource_attributes.key?(:email) &&
44
+ @resource.devise_modules.include?(:confirmable) &&
45
+ @resource.respond_to?(:unconfirmed_email=)
46
+ end
47
+
48
+ def store_unconfirmed_email
49
+ @resource.unconfirmed_email = @resource.email
50
+ @resource.confirmation_token = nil
51
+ @resource.email = email_in_database
52
+ @resource.send(:generate_confirmation_token)
53
+ end
54
+
55
+ def email_in_database
56
+ if Devise.activerecord51?
57
+ @resource.email_in_database
58
+ else
59
+ @resource.email_was
60
+ end
61
+ end
62
+
63
+ def send_confirmation_instructions(saved)
64
+ return unless saved
65
+
66
+ @resource.send_confirmation_instructions(
67
+ redirect_url: @attributes[:confirmation_success_url] || DeviseTokenAuth.default_confirm_success_url,
68
+ template_path: ['graphql_devise/mailer'],
69
+ schema_url: @attributes[:schema_url]
70
+ )
71
+ end
72
+ end
73
+ end
74
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'operation_preparers/gql_name_setter'
2
4
  require_relative 'operation_preparers/mutation_field_setter'
3
5
  require_relative 'operation_preparers/resolver_type_setter'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  module MountMethod
3
5
  module OperationPreparers
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  module MountMethod
3
5
  module OperationPreparers
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  module MountMethod
3
5
  module OperationPreparers
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  module MountMethod
3
5
  module OperationPreparers
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  module MountMethod
3
5
  module OperationPreparers
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  module MountMethod
3
5
  module OperationPreparers
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  module MountMethod
3
5
  class OperationSanitizer
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'supported_options'
2
4
 
3
5
  module GraphqlDevise
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  module MountMethod
3
5
  module OptionSanitizers
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  module MountMethod
3
5
  module OptionSanitizers
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  module MountMethod
3
5
  module OptionSanitizers
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  module MountMethod
3
5
  module OptionSanitizers
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'supported_operations_validator'
2
4
 
3
5
  module GraphqlDevise
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  module MountMethod
3
5
  module OptionValidators
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  module MountMethod
3
5
  module OptionValidators
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'option_validators/skip_only_validator'
2
4
  require_relative 'option_validators/provided_operations_validator'
3
5