$model */ public function __construct( Hasher $hasher, string $model, private readonly CrmService $crmService ) { parent::__construct($hasher, $model); } /** * The model to use, specified in auth config guard. * * @var class-string<\App\Models\Customer> */ private $usedModel = Customer::class; /** * Retrieve a user by the given credentials. * * @param array $credentials * * @throws \RuntimeException If connection to server fails. * @throws \LDC\LDCCRM\Exceptions\CrmException On CRM call error. * @throws \Exception On CRM call error. */ #[\Override] public function retrieveByCredentials(array $credentials): ?Authenticatable { $model = parent::retrieveByCredentials($credentials); if ($model === null) { return null; } $result = $this->crmService->getCustomerFromEmail(Arr::string($credentials, 'email')); if ($result instanceof CrmServiceException) { report($result); throw ValidationException::withMessages([ \trans('An error has occurred. Please try again later'), ]); } return $result !== null ? $model : null; } /** * Validate a user against the given credentials. * * @param array $credentials * * @throws \RuntimeException If model provided is not a Customer or connection to server fails. * @throws \LDC\LDCCRM\Exceptions\CrmException On CRM call error. * @throws \Exception On CRM call error. */ #[\Override] public function validateCredentials(Authenticatable $model, array $credentials): bool { if ($model::class !== $this->usedModel) { throw new \UnexpectedValueException("Cannot authenticate any thing else than '{$this->usedModel}'"); } if (! in_array(MustVerifyEmail::class, \class_implements($model))) { throw new \UnexpectedValueException("Model '{$this->usedModel}' should implement 'MustVerifyEmail'"); } /** @var \Illuminate\Contracts\Auth\Authenticatable&\Illuminate\Contracts\Auth\MustVerifyEmail $model */ $requestEmail = Arr::string($credentials, 'email'); $requestPassword = Arr::string($credentials, 'password'); try { $this->crmService->login( $requestEmail, $requestPassword ); return true; } catch (CrmCustomerNotActivatedException) { try { $model->sendEmailVerificationNotification(); } catch (\Throwable $e) { report($e); } return true; } catch (CrmAuthenticationFailedException) { return false; } catch (CrmServiceException $e) { report($e); throw ValidationException::withMessages([ trans('An error has occurred. Please try again later'), ]); } } /** @param array $credentials */ #[\Override] public function rehashPasswordIfRequired(Authenticatable $user, #[\SensitiveParameter] array $credentials, bool $force = false): void { // No hashing. } }