-
Notifications
You must be signed in to change notification settings - Fork 7
/
ResetPassword.php
80 lines (69 loc) · 2.48 KB
/
ResetPassword.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
declare(strict_types=1);
namespace DanielDeWit\LighthouseSanctum\GraphQL\Mutations;
use DanielDeWit\LighthouseSanctum\Enums\ResetPasswordStatus;
use DanielDeWit\LighthouseSanctum\Exceptions\ResetPasswordException;
use Exception;
use GraphQL\Type\Definition\ResolveInfo;
use Illuminate\Auth\Events\PasswordReset;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\PasswordBroker;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Hashing\Hasher;
use Illuminate\Contracts\Translation\Translator;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
class ResetPassword
{
protected PasswordBroker $passwordBroker;
protected Hasher $hash;
protected Dispatcher $dispatcher;
protected Translator $translator;
public function __construct(
PasswordBroker $passwordBroker,
Hasher $hash,
Dispatcher $dispatcher,
Translator $translator
) {
$this->passwordBroker = $passwordBroker;
$this->hash = $hash;
$this->dispatcher = $dispatcher;
$this->translator = $translator;
}
/**
* @param mixed $_
* @param array<string, mixed> $args
* @param GraphQLContext $context
* @param ResolveInfo $resolveInfo
* @return array<string, ResetPasswordStatus|string|null>
* @throws Exception
*/
public function __invoke($_, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): array
{
$credentials = Arr::except($args, [
'directive',
'password_confirmation',
]);
$response = $this->passwordBroker->reset($credentials, function (Authenticatable $user, string $password) {
$this->resetPassword($user, $password);
$this->dispatcher->dispatch(new PasswordReset($user));
});
if ($response === PasswordBroker::PASSWORD_RESET) {
return [
'status' => ResetPasswordStatus::PASSWORD_RESET(),
'message' => $this->translator->get($response),
];
}
throw new ResetPasswordException(
$this->translator->get($response),
implode('.', $resolveInfo->path),
);
}
protected function resetPassword(Authenticatable $user, string $password): void
{
/** @var Model $user */
$user->setAttribute('password', $this->hash->make($password));
$user->save();
}
}