|
@@ -0,0 +1,123 @@
|
|
|
|
|
+<?php
|
|
|
|
|
+
|
|
|
|
|
+namespace Webkul\BagistoApi\State;
|
|
|
|
|
+
|
|
|
|
|
+use ApiPlatform\Metadata\Operation;
|
|
|
|
|
+use ApiPlatform\Metadata\Post;
|
|
|
|
|
+use ApiPlatform\State\ProcessorInterface;
|
|
|
|
|
+use Illuminate\Auth\Events\PasswordReset;
|
|
|
|
|
+use Illuminate\Support\Facades\Event;
|
|
|
|
|
+use Illuminate\Support\Facades\Hash;
|
|
|
|
|
+use Illuminate\Support\Facades\Password;
|
|
|
|
|
+use Illuminate\Support\Str;
|
|
|
|
|
+use Webkul\BagistoApi\Dto\ResetPasswordInput;
|
|
|
|
|
+use Webkul\Customer\Repositories\CustomerRepository;
|
|
|
|
|
+
|
|
|
|
|
+class ResetPasswordProcessor implements ProcessorInterface
|
|
|
|
|
+{
|
|
|
|
|
+ public function __construct(protected CustomerRepository $customerRepository) {}
|
|
|
|
|
+
|
|
|
|
|
+ public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = [])
|
|
|
|
|
+ {
|
|
|
|
|
+ $defaultResponse = [
|
|
|
|
|
+ 'success' => false,
|
|
|
|
|
+ 'message' => '',
|
|
|
|
|
+ ];
|
|
|
|
|
+
|
|
|
|
|
+ $isRestPost = $operation instanceof Post;
|
|
|
|
|
+ $isGraphQlCreate = $operation->getName() === 'create';
|
|
|
|
|
+
|
|
|
|
|
+ if (! $isRestPost && ! $isGraphQlCreate) {
|
|
|
|
|
+ $defaultResponse['message'] = __('bagistoapi::app.graphql.reset-password.invalid-operation');
|
|
|
|
|
+
|
|
|
|
|
+ return (object) $defaultResponse;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if ($isRestPost && ! $data instanceof ResetPasswordInput) {
|
|
|
|
|
+ $input = new ResetPasswordInput;
|
|
|
|
|
+ $input->token = (string) (request()->input('token') ?? '');
|
|
|
|
|
+ $input->email = (string) (request()->input('email') ?? '');
|
|
|
|
|
+ $input->password = (string) (request()->input('password') ?? '');
|
|
|
|
|
+ $input->passwordConfirmation = (string) (request()->input('passwordConfirmation') ?? request()->input('password_confirmation') ?? '');
|
|
|
|
|
+ $data = $input;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (! $data instanceof ResetPasswordInput) {
|
|
|
|
|
+ $defaultResponse['message'] = __('bagistoapi::app.graphql.reset-password.invalid-input-data');
|
|
|
|
|
+
|
|
|
|
|
+ return (object) $defaultResponse;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (empty($data->token)) {
|
|
|
|
|
+ $defaultResponse['message'] = __('bagistoapi::app.graphql.reset-password.token-required');
|
|
|
|
|
+
|
|
|
|
|
+ return (object) $defaultResponse;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (empty($data->email)) {
|
|
|
|
|
+ $defaultResponse['message'] = __('bagistoapi::app.graphql.reset-password.email-required');
|
|
|
|
|
+
|
|
|
|
|
+ return (object) $defaultResponse;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (empty($data->password)) {
|
|
|
|
|
+ $defaultResponse['message'] = __('bagistoapi::app.graphql.reset-password.password-required');
|
|
|
|
|
+
|
|
|
|
|
+ return (object) $defaultResponse;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ $response = $this->broker()->reset([
|
|
|
|
|
+ 'email' => $data->email,
|
|
|
|
|
+ 'password' => $data->password,
|
|
|
|
|
+ 'password_confirmation' => $data->passwordConfirmation ?? $data->password,
|
|
|
|
|
+ 'token' => $data->token,
|
|
|
|
|
+ ], function ($customer, $password) {
|
|
|
|
|
+ $this->resetPassword($customer, $password);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ if ($response == Password::PASSWORD_RESET) {
|
|
|
|
|
+ $customer = $this->customerRepository->findOneByField('email', $data->email);
|
|
|
|
|
+
|
|
|
|
|
+ if ($customer) {
|
|
|
|
|
+ Event::dispatch('customer.password.update.after', $customer);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return (object) [
|
|
|
|
|
+ 'success' => true,
|
|
|
|
|
+ 'message' => __('bagistoapi::app.graphql.reset-password.password-reset'),
|
|
|
|
|
+ ];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return (object) [
|
|
|
|
|
+ 'success' => false,
|
|
|
|
|
+ 'message' => trans($response),
|
|
|
|
|
+ ];
|
|
|
|
|
+
|
|
|
|
|
+ } catch (\Exception $e) {
|
|
|
|
|
+ return (object) [
|
|
|
|
|
+ 'success' => false,
|
|
|
|
|
+ 'message' => __('bagistoapi::app.graphql.reset-password.error-resetting'),
|
|
|
|
|
+ ];
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Reset the given customer password.
|
|
|
|
|
+ */
|
|
|
|
|
+ protected function resetPassword($customer, $password)
|
|
|
|
|
+ {
|
|
|
|
|
+ $customer->password = Hash::make($password);
|
|
|
|
|
+
|
|
|
|
|
+ $customer->setRememberToken(Str::random(60));
|
|
|
|
|
+
|
|
|
|
|
+ $customer->save();
|
|
|
|
|
+
|
|
|
|
|
+ event(new PasswordReset($customer));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private function broker()
|
|
|
|
|
+ {
|
|
|
|
|
+ return Password::broker('customers');
|
|
|
|
|
+ }
|
|
|
|
|
+}
|