Skip to content

Commit 2085be9

Browse files
committed
Old password should be sent to update it.
1 parent e941a90 commit 2085be9

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

graphql/auth.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ input VerifyEmailInput {
8181
}
8282

8383
input UpdatePassword {
84+
old_password: String!
8485
password: String! @rules(apply: ["required", "confirmed", "min:8"])
8586
password_confirmation: String!
8687
}

src/GraphQL/Mutations/UpdatePassword.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use GraphQL\Type\Definition\ResolveInfo;
66
use Illuminate\Support\Facades\Hash;
77
use Joselfonseca\LighthouseGraphQLPassport\Events\PasswordUpdated;
8+
use Joselfonseca\LighthouseGraphQLPassport\Exceptions\ValidationException;
89
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
910

1011
/**
@@ -23,6 +24,11 @@ class UpdatePassword
2324
public function resolve($rootValue, array $args, GraphQLContext $context = null, ResolveInfo $resolveInfo)
2425
{
2526
$user = $context->user();
27+
if (!Hash::check($args['old_password'], $user->password)) {
28+
throw new ValidationException([
29+
'password' => _('Current password is incorrect')
30+
], 'Validation Exception');
31+
}
2632
$user->password = Hash::make($args['password']);
2733
$user->save();
2834
event(new PasswordUpdated($user));

tests/Integration/GraphQL/Mutations/UpdatePasswordTest.php

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

55
use Illuminate\Support\Facades\Event;
6+
use Illuminate\Support\Facades\Hash;
67
use Joselfonseca\LighthouseGraphQLPassport\Events\PasswordUpdated;
78
use Joselfonseca\LighthouseGraphQLPassport\Tests\TestCase;
89
use Joselfonseca\LighthouseGraphQLPassport\Tests\User;
@@ -17,12 +18,13 @@ public function test_it_updates_logged_in_user_password()
1718
$user = User::create([
1819
'name' => 'Jose Fonseca',
1920
'email' => 'jose@example.com',
20-
'password' => bcrypt('123456789qq'),
21+
'password' => Hash::make('123456789qq'),
2122
]);
2223
Passport::actingAs($user);
2324
$response = $this->postGraphQL([
2425
'query' => 'mutation {
2526
updatePassword(input: {
27+
old_password: "123456789qq",
2628
password: "newPassword123",
2729
password_confirmation: "newPassword123"
2830
}) {
@@ -52,6 +54,7 @@ public function test_it_validates_rules_for_password()
5254
$response = $this->postGraphQL([
5355
'query' => 'mutation {
5456
updatePassword(input: {
57+
old_password: "123456789qq",
5558
password: "newPassword123"
5659
}) {
5760
status
@@ -76,6 +79,7 @@ public function test_it_validates_logged_in_user()
7679
$response = $this->postGraphQL([
7780
'query' => 'mutation {
7881
updatePassword(input: {
82+
old_password: "123456789qq",
7983
password: "newPassword123",
8084
password_confirmation: "newPassword123"
8185
}) {
@@ -88,4 +92,32 @@ public function test_it_validates_logged_in_user()
8892
$this->assertArrayHasKey('errors', $responseBody);
8993
$this->assertEquals('Unauthenticated.', $responseBody['errors'][0]['message']);
9094
}
95+
96+
public function test_it_validates_old_password()
97+
{
98+
Event::fake([PasswordUpdated::class]);
99+
$this->createClient();
100+
$user = User::create([
101+
'name' => 'Jose Fonseca',
102+
'email' => 'jose@example.com',
103+
'password' => Hash::make('123456789qq'),
104+
]);
105+
Passport::actingAs($user);
106+
$response = $this->postGraphQL([
107+
'query' => 'mutation {
108+
updatePassword(input: {
109+
old_password: "123456789erreqq",
110+
password: "newPassword123",
111+
password_confirmation: "newPassword123"
112+
}) {
113+
status
114+
message
115+
}
116+
}',
117+
]);
118+
$responseBody = json_decode($response->getContent(), true);
119+
$this->assertArrayHasKey('errors', $responseBody);
120+
$this->assertEquals('Validation Exception', $responseBody['errors'][0]['message']);
121+
$this->assertEquals('Current password is incorrect', $responseBody['errors'][0]['extensions']['errors']['password']);
122+
}
91123
}

0 commit comments

Comments
 (0)