ldc-gen-game-web/app/Http/Controllers/Fo/Auth/ResetPasswordController.php
2026-10-20 12:27:49 +02:00

117 lines
3.2 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Controllers\Fo\Auth;
use App\Http\Controllers\Controller;
use App\Models\Customer;
use Illuminate\Auth\Events\PasswordReset;
use Illuminate\Contracts\Auth\PasswordBroker;
use Illuminate\Contracts\Auth\StatefulGuard;
use Illuminate\Contracts\View\View;
use Illuminate\Foundation\Auth\ResetsPasswords;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Password;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use LDC\LDCCRM\CrmApiV4;
use LDC\LDCCRM\Exceptions\ApiErrorException;
use LDC\LDCCRM\Exceptions\CrmException;
use LDC\LDCCRM\Exceptions\ValidationException as ExceptionsValidationException;
use TypeError;
class ResetPasswordController extends Controller
{
use ResetsPasswords;
/**
* Create a new controller instance.
*/
public function __construct()
{
$this->middleware('guest:frontend');
$this->middleware('signed')->only('showResetForm');
$this->middleware('throttle:6,1')->only('showResetForm');
}
/**
* Get the guard to be used during authentication.
*/
protected function guard(): StatefulGuard
{
return Auth::guard('frontend');
}
/**
* Display the password reset view for the given token.
*
* If no token is present, display the link request form.
*/
public function showResetForm(Request $request): View
{
$token = $request->route()->parameter('token');
/** @var view-string $view */
$view = 'theme::auth.passwords.reset';
return view($view)->with(
['token' => $token, 'email' => $request->email]
);
}
/**
* Reset the given user's password.
*
* @throws \Illuminate\Validation\ValidationException If something goes wrong.
*/
protected function resetPassword(Customer $customer, string $password): void
{
/** @var CrmApiV4 */
$crm = resolve('crmAPI');
try {
if (! ($crmCustomer = $crm->getCustomer($customer->hashcode))) {
throw ValidationException::withMessages([
'error' => trans('Changement impossible, compte inexistant.'),
]);
}
$crmCustomer->password = $password;
$crmCustomer->save();
} catch (TypeError | CrmException | ApiErrorException $e) {
report($e);
throw ValidationException::withMessages([
'error' => trans('front.messages.unexpected_error_try_later'),
]);
} catch (ExceptionsValidationException $e) {
throw ValidationException::withMessages([
'password' => $e->getMessage(),
]);
}
$customer->setRememberToken(Str::random(60));
$customer->save();
event(new PasswordReset($customer));
$this->guard()->login($customer);
}
/**
* Get the post register / login redirect path.
*/
public function redirectPath(): string
{
return $this->authLoggedRedirectPath();
}
/**
* Get the broker to be used during password reset.
*/
public function broker(): PasswordBroker
{
return Password::broker('customers');
}
}