|
1 | 1 | module GraphqlDevise |
2 | 2 | module Mutations |
3 | | - class Login < GraphQL::Schema::Mutation |
| 3 | + class Login < Base |
4 | 4 | argument :email, String, required: true |
5 | 5 | argument :password, String, required: true |
6 | 6 |
|
7 | 7 | field :authenticable, GraphqlDevise::Types::AuthenticableType, null: true |
| 8 | + field :success, Boolean, null: false |
| 9 | + field :errors, [String], null: false |
8 | 10 |
|
9 | 11 | def resolve(email:, password:) |
10 | | - resource = context[:resource_class].find_by(email: email) |
| 12 | + resource = resource_class.find_by(email: email) |
11 | 13 |
|
12 | | - if resource && (!resource.respond_to?(:active_for_authentication?) || resource.active_for_authentication?) |
13 | | - valid_password = resource.valid_password?(password) |
14 | | - if (resource.respond_to?(:valid_for_authentication?) && !resource.valid_for_authentication? { valid_password }) || !valid_password |
15 | | - return {} |
| 14 | + if resource && active_for_authentication?(resource) |
| 15 | + if invalid_for_authentication?(resource, password) |
| 16 | + return single_error_object(I18n.t('graphql_devise.sessions.bad_credentials')) |
16 | 17 | end |
17 | 18 |
|
18 | | - auth_headers = resource.create_new_auth_token |
19 | | - context[:response].headers.merge!(auth_headers) |
| 19 | + set_auth_headers(resource) |
| 20 | + controller.sign_in(:user, resource, store: false, bypass: false) |
20 | 21 |
|
21 | | - { authenticable: resource } |
22 | | - elsif resource && !(!resource.respond_to?(:active_for_authentication?) || resource.active_for_authentication?) |
23 | | - if resource.respond_to?(:locked_at) && resource.locked_at |
24 | | - return {} |
| 22 | + yield resource if block_given? |
| 23 | + |
| 24 | + { success: true, authenticable: resource, errors: [] } |
| 25 | + elsif resource && !active_for_authentication?(resource) |
| 26 | + if locked?(resource) |
| 27 | + single_error_object(I18n.t('graphql_devise.mailer.unlock_instructions.account_lock_msg')) |
25 | 28 | else |
26 | | - return {} |
| 29 | + single_error_object(I18n.t('devise_token_auth.sessions.not_confirmed', email: resource.email)) |
27 | 30 | end |
28 | 31 | else |
29 | | - return {} |
| 32 | + single_error_object(I18n.t('graphql_devise.sessions.bad_credentials')) |
30 | 33 | end |
31 | 34 | end |
| 35 | + |
| 36 | + private |
| 37 | + |
| 38 | + def set_auth_headers(resource) |
| 39 | + auth_headers = resource.create_new_auth_token |
| 40 | + response.headers.merge!(auth_headers) |
| 41 | + end |
| 42 | + |
| 43 | + def invalid_for_authentication?(resource, password) |
| 44 | + valid_password = resource.valid_password?(password) |
| 45 | + |
| 46 | + (resource.respond_to?(:valid_for_authentication?) && !resource.valid_for_authentication? { valid_password }) || |
| 47 | + !valid_password |
| 48 | + end |
| 49 | + |
| 50 | + def active_for_authentication?(resource) |
| 51 | + !resource.respond_to?(:active_for_authentication?) || resource.active_for_authentication? |
| 52 | + end |
| 53 | + |
| 54 | + def locked?(resource) |
| 55 | + resource.respond_to?(:locked_at) && resource.locked_at |
| 56 | + end |
32 | 57 | end |
33 | 58 | end |
34 | 59 | end |
0 commit comments