Skip to content

Commit

Permalink
Merge 5ef2bf7 into 004d9a7
Browse files Browse the repository at this point in the history
  • Loading branch information
wimski committed Apr 26, 2021
2 parents 004d9a7 + 5ef2bf7 commit 9a76551
Show file tree
Hide file tree
Showing 8 changed files with 638 additions and 1 deletion.
18 changes: 18 additions & 0 deletions README.md
Expand Up @@ -206,6 +206,24 @@ mutation ForgotPassword {
}
```

### Reset Password

Reset the user's password.

```graphql
mutation ResetPassword {
resetPassword(input: {
email: "john.doe@gmail.com",
token: "af269947ed80d4a7bc3f78a6dfd05ec369373f9d"
password: "secret"
password_confirmation: "secret"
}) {
status
message
}
}
```

## Testing

```bash
Expand Down
16 changes: 15 additions & 1 deletion graphql/sanctum.graphql
Expand Up @@ -70,6 +70,18 @@ type ForgotPasswordResponse {
message: String
}

input ResetPasswordInput {
email: String! @rules(apply: ["email"])
token: String!
password: String! @rules(apply: ["confirmed"])
password_confirmation: String!
}

type ResetPasswordResponse {
status: ResetPasswordStatus!
message: String
}

extend type Mutation {
login(input: LoginInput @spread): AccessToken!
@field(resolver: "DanielDeWit\\LighthouseSanctum\\GraphQL\\Mutations\\Login")
Expand All @@ -80,5 +92,7 @@ extend type Mutation {
verifyEmail(input: VerifyEmailInput! @spread): EmailVerificationResponse!
@field(resolver: "DanielDeWit\\LighthouseSanctum\\GraphQL\\Mutations\\VerifyEmail")
forgotPassword(input: ForgotPasswordInput! @spread): ForgotPasswordResponse!
@field(resolver: "DanielDeWit\\LighthouseSanctum\\GraphQL\\Mutations\\ForgotPassword")
@field(resolver: "DanielDeWit\\LighthouseSanctum\\GraphQL\\Mutations\\ForgotPassword")
resetPassword(input: ResetPasswordInput! @spread): ResetPasswordResponse!
@field(resolver: "DanielDeWit\\LighthouseSanctum\\GraphQL\\Mutations\\ResetPassword")
}
15 changes: 15 additions & 0 deletions src/Enums/ResetPasswordStatus.php
@@ -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';
}
39 changes: 39 additions & 0 deletions src/Exceptions/ResetPasswordException.php
@@ -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],
],
];
}
}
80 changes: 80 additions & 0 deletions src/GraphQL/Mutations/ResetPassword.php
@@ -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();
}
}
5 changes: 5 additions & 0 deletions src/Providers/LighthouseSanctumServiceProvider.php
Expand Up @@ -10,6 +10,7 @@
use DanielDeWit\LighthouseSanctum\Enums\ForgotPasswordStatus;
use DanielDeWit\LighthouseSanctum\Enums\LogoutStatus;
use DanielDeWit\LighthouseSanctum\Enums\RegisterStatus;
use DanielDeWit\LighthouseSanctum\Enums\ResetPasswordStatus;
use DanielDeWit\LighthouseSanctum\Services\EmailVerificationService;
use DanielDeWit\LighthouseSanctum\Services\ResetPasswordService;
use Illuminate\Contracts\Events\Dispatcher;
Expand Down Expand Up @@ -78,5 +79,9 @@ protected function registerEnums(): void
$this->typeRegistry->register(
new LaravelEnumType(ForgotPasswordStatus::class),
);

$this->typeRegistry->register(
new LaravelEnumType(ResetPasswordStatus::class),
);
}
}

0 comments on commit 9a76551

Please sign in to comment.