ldc-legaulois/app/Http/Controllers/Club/Auth/ResetPasswordController.php
2026-10-19 15:49:33 +02:00

112 lines
3.3 KiB
PHP

<?php
namespace App\Http\Controllers\Club\Auth;
use App\Events\CustomerPasswordReset;
use App\Http\Controllers\Controller;
use App\Models\Customer;
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\Facades\Session;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use LDC\LDCCRM\Exceptions\ApiErrorException;
use LDC\LDCCRM\Exceptions\CrmException;
use LDC\LDCCRM\Exceptions\ValidationException as ExceptionsValidationException;
class ResetPasswordController extends Controller
{
use ResetsPasswords;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest:club');
}
/**
* Get the guard to be used during authentication.
*/
protected function guard(): StatefulGuard
{
return Auth::guard('club');
}
/**
* 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');
return view('club.auth.passwords.reset')->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 \LDC\LDCCRM\CrmApiV4 */
$crm = app('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('Une erreur est survenue, merci de réessayer ultérieurement.'),
]);
} catch (ExceptionsValidationException $e) {
throw ValidationException::withMessages([
'password' => $e->getMessage(),
]);
}
$customer->setRememberToken(Str::random(60));
$customer->saveOrFail();
event(new CustomerPasswordReset($customer, $customer->api_data->firstname));
$this->guard()->login($customer);
Session::flash('success', trans('Votre mot de passe à bien été réinitialisé, vous êtes à présent connecté.'));
}
/**
* 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');
}
}