Skip to content

Commit

Permalink
Merge pull request #55 from graphql-devise/add-credentials-field-logi…
Browse files Browse the repository at this point in the history
…n-mutation

Return credentials field in login mutation
  • Loading branch information
mcelicalderon committed Dec 26, 2019
2 parents e033121 + 1633d78 commit 020b125
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ Here is a list of the available mutations and queries assuming your mounted mode

#### Mutations
1. `userLogin(email: String!, password: String!): UserLoginPayload`

This mutation has a second field by default. `credentials` can be fetched directly on the mutation return type.
Credentials are still returned in the headers of the response.

1. `userLogout: UserLogoutPayload`
1. `userSignUp(email: String!, password: String!, passwordConfirmation: String!, confirmSuccessUrl: String): UserSignUpPayload`

Expand Down
6 changes: 4 additions & 2 deletions app/graphql/graphql_devise/mutations/login.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class Login < Base
argument :email, String, required: true
argument :password, String, required: true

field :credentials, Types::CredentialType, null: false

def resolve(email:, password:)
resource = resource_class.find_by(email: email)

Expand All @@ -12,12 +14,12 @@ def resolve(email:, password:)
raise_user_error(I18n.t('graphql_devise.sessions.bad_credentials'))
end

set_auth_headers(resource)
new_headers = set_auth_headers(resource)
controller.sign_in(:user, resource, store: false, bypass: false)

yield resource if block_given?

{ authenticatable: resource}
{ authenticatable: resource, credentials: new_headers }
elsif resource && !active_for_authentication?(resource)
if locked?(resource)
raise_user_error(I18n.t('graphql_devise.mailer.unlock_instructions.account_lock_msg'))
Expand Down
31 changes: 31 additions & 0 deletions app/graphql/graphql_devise/types/credential_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module GraphqlDevise
module Types
class CredentialType < GraphQL::Schema::Object
field :access_token, String, null: false
field :uid, String, null: false
field :token_type, String, null: false
field :client, String, null: false
field :expiry, Int, null: false

def access_token
object[DeviseTokenAuth.headers_names[:"access-token"]]
end

def uid
object[DeviseTokenAuth.headers_names[:uid]]
end

def token_type
object[DeviseTokenAuth.headers_names[:"token-type"]]
end

def client
object[DeviseTokenAuth.headers_names[:client]]
end

def expiry
object[DeviseTokenAuth.headers_names[:expiry]]
end
end
end
end
2 changes: 2 additions & 0 deletions lib/graphql_devise/concerns/controller_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ def client
def set_auth_headers(resource)
auth_headers = resource.create_new_auth_token
response.headers.merge!(auth_headers)

auth_headers
end

def client_and_token(token)
Expand Down
12 changes: 10 additions & 2 deletions spec/requests/mutations/login_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
password: "#{password}"
) {
user { email name signInCount }
credentials { accessToken uid tokenType client expiry }
}
}
GRAPHQL
Expand All @@ -24,11 +25,18 @@

context 'when user is able to login' do
context 'when credentials are valid' do
it 'return credentials in headers and user information' do
it 'return credentials in headers/field and user information' do
expect(response).to include_auth_headers
expect(user.reload.tokens.keys).to include(response.headers['client'])
expect(json_response[:data][:userLogin]).to match(
user: { email: user.email, name: user.name, signInCount: 1 }
user: { email: user.email, name: user.name, signInCount: 1 },
credentials: {
accessToken: response.headers['access-token'],
uid: response.headers['uid'],
tokenType: response.headers['token-type'],
client: response.headers['client'],
expiry: response.headers['expiry'].to_i
}
)
expect(json_response[:errors]).to be_nil
end
Expand Down

0 comments on commit 020b125

Please sign in to comment.