Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
638 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DanielDeWit\LighthouseSanctum\Enums; | ||
|
||
use BenSampo\Enum\Enum; | ||
|
||
/** | ||
* @method static static PASSWORD_RESET() | ||
*/ | ||
class ResetPasswordStatus extends Enum | ||
{ | ||
public const PASSWORD_RESET = 'PASSWORD_RESET'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DanielDeWit\LighthouseSanctum\Exceptions; | ||
|
||
use Exception; | ||
use Nuwave\Lighthouse\Exceptions\RendersErrorsExtensions; | ||
|
||
class ResetPasswordException extends Exception implements RendersErrorsExtensions | ||
{ | ||
protected string $validationMessage; | ||
|
||
public function __construct(string $message, string $path) | ||
{ | ||
$this->validationMessage = $message; | ||
|
||
parent::__construct("Validation failed for the field [{$path}]."); | ||
} | ||
|
||
public function isClientSafe(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function getCategory(): string | ||
{ | ||
return 'validation'; | ||
} | ||
|
||
public function extensionsContent(): array | ||
{ | ||
return [ | ||
'validation' => [ | ||
'input' => [$this->validationMessage], | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.