Skip to content

Commit f163526

Browse files
authored
Merge branch 'master' into create-sign-up-mutation
2 parents f5677b4 + 9c65e4f commit f163526

20 files changed

Lines changed: 255 additions & 63 deletions

File tree

Appraisals

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ end
77
appraise 'rails5.0-graphql1.8' do
88
gem 'rails', github: 'rails/rails', branch: '5-0-stable'
99
gem 'graphql', '~> 1.8.0'
10-
gem 'devise_token_auth', '0.1.37'
10+
gem 'devise_token_auth', '0.1.43'
1111
gem 'devise', '>= 4.0'
1212
end
1313

@@ -19,7 +19,7 @@ end
1919
appraise 'rails5.1-graphql1.8' do
2020
gem 'rails', github: 'rails/rails', branch: '5-1-stable'
2121
gem 'graphql', '~> 1.8.0'
22-
gem 'devise_token_auth', '0.1.42'
22+
gem 'devise_token_auth', '0.1.43'
2323
gem 'devise', '>= 4.3'
2424
end
2525

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# GraphqlDevise
2-
[![Build Status](https://travis-ci.org/graphql-device/graphql_devise.svg?branch=master)](https://travis-ci.org/graphql-device/graphql_devise)
2+
[![Build Status](https://travis-ci.org/graphql-devise/graphql_devise.svg?branch=master)](https://travis-ci.org/graphql-devise/graphql_devise)
33

44
## Installation
55

@@ -27,7 +27,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
2727

2828
## Contributing
2929

30-
Bug reports and pull requests are welcome on GitHub at https://github.com/graphql-device/graphql_devise.
30+
Bug reports and pull requests are welcome on GitHub at https://github.com/graphql-devise/graphql_devise.
3131

3232
## License
3333

app/graphql/graphql_devise/mutations/base.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,20 @@ module Mutations
55
class Base < GraphQL::Schema::Mutation
66
private
77

8+
def raise_user_error(message)
9+
raise GraphqlDevise::UserError, message
10+
end
11+
12+
def raise_user_error_list(message, errors:)
13+
raise GraphqlDevise::DetailedUserError.new(message, errors: errors)
14+
end
15+
816
def remove_resource
917
controller.resource = nil
1018
controller.client_id = nil
1119
controller.token = nil
1220
end
1321

14-
def single_error_object(error)
15-
{ success: false, errors: [error] }
16-
end
17-
1822
def request
1923
controller.request
2024
end
@@ -31,6 +35,10 @@ def resource_class
3135
context[:resource_class]
3236
end
3337

38+
def recoverable_enabled?
39+
resource_class.devise_modules.include?(:recoverable)
40+
end
41+
3442
def current_resource
3543
context[:current_resource]
3644
end

app/graphql/graphql_devise/mutations/login.rb

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,28 @@ class Login < Base
44
argument :email, String, required: true
55
argument :password, String, required: true
66

7-
field :success, Boolean, null: false
8-
field :errors, [String], null: false
9-
107
def resolve(email:, password:)
118
resource = resource_class.find_by(email: email)
129

1310
if resource && active_for_authentication?(resource)
1411
if invalid_for_authentication?(resource, password)
15-
return single_error_object(I18n.t('graphql_devise.sessions.bad_credentials'))
12+
raise_user_error(I18n.t('graphql_devise.sessions.bad_credentials'))
1613
end
1714

1815
set_auth_headers(resource)
1916
controller.sign_in(:user, resource, store: false, bypass: false)
2017

2118
yield resource if block_given?
2219

23-
{ success: true, authenticable: resource, errors: [] }
20+
{ authenticable: resource}
2421
elsif resource && !active_for_authentication?(resource)
2522
if locked?(resource)
26-
single_error_object(I18n.t('graphql_devise.mailer.unlock_instructions.account_lock_msg'))
23+
raise_user_error(I18n.t('graphql_devise.mailer.unlock_instructions.account_lock_msg'))
2724
else
28-
single_error_object(I18n.t('devise_token_auth.sessions.not_confirmed', email: resource.email))
25+
raise_user_error(I18n.t('devise_token_auth.sessions.not_confirmed', email: resource.email))
2926
end
3027
else
31-
single_error_object(I18n.t('graphql_devise.sessions.bad_credentials'))
28+
raise_user_error(I18n.t('graphql_devise.sessions.bad_credentials'))
3229
end
3330
end
3431

app/graphql/graphql_devise/mutations/logout.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
module GraphqlDevise
22
module Mutations
33
class Logout < Base
4-
field :success, Boolean, null: false
5-
field :errors, [String], null: false
6-
74
def resolve
85
if current_resource && client && current_resource.tokens[client]
96
current_resource.tokens.delete(client)
@@ -13,9 +10,9 @@ def resolve
1310

1411
yield current_resource if block_given?
1512

16-
{ success: true, errors: [], authenticable: current_resource }
13+
{ authenticable: current_resource }
1714
else
18-
{ success: false, errors: [I18n.t('graphql_devise.user_not_found')] }
15+
raise_user_error(I18n.t('graphql_devise.user_not_found'))
1916
end
2017
end
2118
end
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
module GraphqlDevise
2+
module Mutations
3+
class UpdatePassword < Base
4+
argument :password, String, required: true
5+
argument :password_confirmation, String, required: true
6+
argument :current_password, String, required: false
7+
8+
def resolve(current_password: nil, **attrs)
9+
if current_resource.blank?
10+
raise_user_error(I18n.t('graphql_devise.not_authenticated'))
11+
elsif current_resource.provider != 'email'
12+
raise_user_error(
13+
I18n.t('graphql_devise.passwords.password_not_required', provider: current_resource.provider.humanize)
14+
)
15+
end
16+
17+
if update_resource_password(current_password, attrs)
18+
current_resource.allow_password_change = false if recoverable_enabled?
19+
current_resource.save!
20+
21+
yield current_resource if block_given?
22+
23+
{ authenticable: current_resource }
24+
else
25+
raise_user_error_list(
26+
I18n.t('graphql_devise.passwords.update_password_error'),
27+
errors: current_resource.errors.full_messages
28+
)
29+
end
30+
end
31+
32+
private
33+
34+
def update_resource_password(current_password, attrs)
35+
allow_password_change = recoverable_enabled? && current_resource.allow_password_change == true
36+
if DeviseTokenAuth.check_current_password_before_update == false || allow_password_change
37+
current_resource.public_send(:update, attrs)
38+
else
39+
current_resource.public_send(:update_with_password, attrs.merge(current_password: current_password))
40+
end
41+
end
42+
end
43+
end
44+
end

config/locales/en.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
en:
22
graphql_devise:
3+
not_authenticated: "User is not logged in."
34
user_not_found: "User was not found or was not logged in."
5+
passwords:
6+
update_password_error: "Unable to update user password"
7+
missing_passwords: "You must fill out the fields labeled 'Password' and 'Password confirmation'."
8+
password_not_required: "This account does not require a password. Sign in using your '%{provider}' account instead."
49
sessions:
510
bad_credentials: "Invalid login credentials. Please try again."
611
not_confirmed: "A confirmation email was sent to your account at '%{email}'. You must follow the instructions in the email before your account can be activated"

gemfiles/rails5.0_graphql1.8.gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ source "https://rubygems.org"
44

55
gem "rails", github: "rails/rails", branch: "5-0-stable"
66
gem "graphql", "~> 1.8.0"
7-
gem "devise_token_auth", "0.1.37"
7+
gem "devise_token_auth", "0.1.43"
88
gem "devise", ">= 4.0"
99

1010
gemspec path: "../"

gemfiles/rails5.1_graphql1.8.gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ source "https://rubygems.org"
44

55
gem "rails", github: "rails/rails", branch: "5-1-stable"
66
gem "graphql", "~> 1.8.0"
7-
gem "devise_token_auth", "0.1.42"
7+
gem "devise_token_auth", "0.1.43"
88
gem "devise", ">= 4.3"
99

1010
gemspec path: "../"

graphql_devise.gemspec

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ Gem::Specification.new do |spec|
1010

1111
spec.summary = 'GraphQL queries and mutations on top of devise_token_auth'
1212
spec.description = 'GraphQL queries and mutations on top of devise_token_auth'
13-
spec.homepage = 'https://github.com/graphql-device/graphql_devise'
13+
spec.homepage = 'https://github.com/graphql-devise/graphql_devise'
1414
spec.license = 'MIT'
1515

1616
spec.metadata['homepage_uri'] = spec.homepage
17-
spec.metadata['source_code_uri'] = 'https://github.com/graphql-device/graphql_devise'
17+
spec.metadata['source_code_uri'] = 'https://github.com/graphql-devise/graphql_devise'
1818
spec.files = Dir.chdir(File.expand_path(__dir__)) do
1919
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
2020
end
@@ -25,7 +25,7 @@ Gem::Specification.new do |spec|
2525

2626
spec.required_ruby_version = '>= 2.2.0'
2727

28-
spec.add_dependency 'devise_token_auth', '>= 0.1.37'
28+
spec.add_dependency 'devise_token_auth', '>= 0.1.43'
2929
spec.add_dependency 'graphql', '>= 1.8'
3030
spec.add_dependency 'rails', '>= 4.2'
3131

0 commit comments

Comments
 (0)