Skip to content

Commit 81e2802

Browse files
committed
Add user to login response
fix #7
1 parent d01bd89 commit 81e2802

File tree

5 files changed

+30
-2
lines changed

5 files changed

+30
-2
lines changed

graphql/auth.graphql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,18 @@ input RefreshTokenInput {
77
refresh_token: String
88
}
99

10+
type User {
11+
id: ID!
12+
name: String!
13+
email: String!
14+
}
15+
1016
type AuthPayload {
1117
access_token: String!
1218
refresh_token: String!
1319
expires_in: Int!
1420
token_type: String!
21+
user: User!
1522
}
1623

1724
type LogoutResponse {

readme.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,18 @@ input RefreshTokenInput {
4545
refresh_token: String
4646
}
4747

48+
type User {
49+
id: ID!
50+
name: String!
51+
email: String!
52+
}
53+
4854
type AuthPayload {
4955
access_token: String!
5056
refresh_token: String!
5157
expires_in: Int!
5258
token_type: String!
59+
user: User!
5360
}
5461

5562
type LogoutResponse {

src/GraphQL/Mutations/Login.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Joselfonseca\LighthouseGraphQLPassport\GraphQL\Mutations;
44

55
use GraphQL\Type\Definition\ResolveInfo;
6+
use Illuminate\Support\Facades\Auth;
67
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
78

89
class Login extends BaseAuthResolver
@@ -18,7 +19,11 @@ class Login extends BaseAuthResolver
1819
public function resolve($rootValue, array $args, GraphQLContext $context = null, ResolveInfo $resolveInfo)
1920
{
2021
$credentials = $this->buildCredentials($args);
21-
return $this->makeRequest($credentials);
22+
$response = $this->makeRequest($credentials);
23+
Auth::once(['email' => $args['data']['username'], 'password' => $args['data']['password']]);
24+
$user = Auth::user();
25+
$response['user'] = $user;
26+
return $response;
2227
}
2328

2429
}

tests/Integration/GraphQL/Mutations/LoginTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,22 @@ function test_it_gets_access_token()
2424
}) {
2525
access_token
2626
refresh_token
27+
user {
28+
id
29+
name
30+
email
31+
}
2732
}
2833
}'
2934
]);
3035
$responseBody = json_decode($response->getContent(), true);
3136
$this->assertArrayHasKey('login', $responseBody['data']);
3237
$this->assertArrayHasKey('access_token', $responseBody['data']['login']);
3338
$this->assertArrayHasKey('refresh_token', $responseBody['data']['login']);
39+
$this->assertArrayHasKey('user', $responseBody['data']['login']);
40+
$this->assertArrayHasKey('id', $responseBody['data']['login']['user']);
41+
$this->assertArrayHasKey('name', $responseBody['data']['login']['user']);
42+
$this->assertArrayHasKey('email', $responseBody['data']['login']['user']);
3443
}
3544

3645
}

tests/Integration/GraphQL/Mutations/RefreshToken.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ function test_it_refresh_a_token()
3838
}'
3939
]);
4040
$responseBodyRefreshed = json_decode($responseRefreshed->getContent(), true);
41-
$this->assertNotEquals($responseBody['data']['login']['access_token'], $responseBodyRefreshed['data']['login']['access_token']);
41+
$this->assertNotEquals($responseBody['data']['login']['access_token'], $responseBodyRefreshed['data']['refreshToken']['access_token']);
4242
}
4343
}

0 commit comments

Comments
 (0)