2026-04-28 16:38:04 +02:00
|
|
|
<?php
|
|
|
|
|
|
2026-06-05 17:05:46 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2026-04-28 16:38:04 +02:00
|
|
|
namespace App\Http\Controllers\Auth;
|
|
|
|
|
|
|
|
|
|
use App\Http\Requests\Auth\ForgotPasswordRequest;
|
|
|
|
|
use App\Http\Requests\Auth\LoginRequest;
|
|
|
|
|
use App\Http\Requests\Auth\ResetPasswordRequest;
|
|
|
|
|
use App\Mails\Auth\ResetPassword;
|
|
|
|
|
use App\Models\User;
|
|
|
|
|
use App\Models\UserPassword;
|
|
|
|
|
use Illuminate\Auth\Events\PasswordReset;
|
2026-06-05 17:05:46 +02:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
2026-04-28 16:38:04 +02:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Routing\Controller;
|
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
|
use Illuminate\Support\Facades\Hash;
|
2026-06-05 12:08:07 +02:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2026-04-28 16:38:04 +02:00
|
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
|
use Illuminate\Support\Facades\Password;
|
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
use Inertia\Inertia;
|
2026-06-05 17:05:46 +02:00
|
|
|
use Inertia\Response;
|
2026-04-28 16:38:04 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Manages backend authentication sessions (login/logout).
|
|
|
|
|
*/
|
|
|
|
|
class AuthenticatedSessionController extends Controller
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Display the login view.
|
|
|
|
|
*/
|
2026-06-05 17:05:46 +02:00
|
|
|
public function login(): Response
|
2026-04-28 16:38:04 +02:00
|
|
|
{
|
|
|
|
|
return Inertia::render('back/Pages/Auth/Login');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handle an incoming authentication request.
|
|
|
|
|
*/
|
2026-06-05 17:05:46 +02:00
|
|
|
public function postLogin(LoginRequest $request): RedirectResponse
|
2026-04-28 16:38:04 +02:00
|
|
|
{
|
|
|
|
|
$request->authenticate();
|
|
|
|
|
$request->session()->regenerate();
|
|
|
|
|
|
|
|
|
|
return redirect()->intended(route(config('uwil-backend.routes.name') . 'dashboard.index', absolute: false));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Destroy an authenticated session.
|
|
|
|
|
*/
|
2026-06-05 17:05:46 +02:00
|
|
|
public function logout(Request $request): RedirectResponse
|
2026-04-28 16:38:04 +02:00
|
|
|
{
|
|
|
|
|
Auth::guard('backend')->logout();
|
|
|
|
|
|
|
|
|
|
$request->session()->invalidate();
|
|
|
|
|
$request->session()->regenerateToken();
|
|
|
|
|
|
|
|
|
|
return to_route(config('uwil-backend.routes.name') . 'login');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Display the forgotten password view.
|
|
|
|
|
*/
|
2026-06-05 17:05:46 +02:00
|
|
|
public function forgotPassword(): Response
|
2026-04-28 16:38:04 +02:00
|
|
|
{
|
|
|
|
|
return Inertia::render('back/Pages/Auth/ForgotPassword');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handle an incoming forgotten password request.
|
|
|
|
|
*/
|
2026-06-05 17:05:46 +02:00
|
|
|
public function postForgotPassword(ForgotPasswordRequest $request): RedirectResponse
|
2026-04-28 16:38:04 +02:00
|
|
|
{
|
2026-06-05 12:08:07 +02:00
|
|
|
$status = Password::broker('backend')->sendResetLink(
|
|
|
|
|
$request->validated()
|
|
|
|
|
);
|
2026-04-28 16:38:04 +02:00
|
|
|
|
2026-06-05 12:08:07 +02:00
|
|
|
if ($status !== Password::RESET_LINK_SENT) {
|
|
|
|
|
Log::warning('Password reset link not sent', [
|
|
|
|
|
'status' => $status,
|
|
|
|
|
'email' => $request->input('email'),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return to_route(config('uwil-backend.routes.name') . 'login');
|
2026-05-21 13:27:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Send a reset password link to the user.
|
|
|
|
|
*/
|
2026-06-05 17:05:46 +02:00
|
|
|
public function sendResetPasswordLink(User $user): RedirectResponse
|
2026-05-21 13:27:47 +02:00
|
|
|
{
|
|
|
|
|
$status = Password::broker('backend')->sendResetLink(['email' => $user->email]);
|
|
|
|
|
|
|
|
|
|
return $status === Password::RESET_LINK_SENT
|
2026-05-21 15:08:53 +02:00
|
|
|
? back()
|
|
|
|
|
: back()->withErrors(['email' => trans('auth.throttled')]);
|
2026-04-28 16:38:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Display the view.
|
|
|
|
|
*/
|
2026-06-05 17:05:46 +02:00
|
|
|
public function resetPassword(string $token): Response
|
2026-04-28 16:38:04 +02:00
|
|
|
{
|
|
|
|
|
return Inertia::render('back/Pages/Auth/ResetPassword', [
|
|
|
|
|
'token' => $token,
|
|
|
|
|
'email' => request()->query('email', ''),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handle an incoming resetting password request.
|
|
|
|
|
*/
|
2026-06-05 17:05:46 +02:00
|
|
|
public function postResetPassword(ResetPasswordRequest $request): RedirectResponse
|
2026-04-28 16:38:04 +02:00
|
|
|
{
|
|
|
|
|
$status = Password::broker('backend')->reset(
|
|
|
|
|
$request->validated(),
|
|
|
|
|
function (User $user, string $password): void {
|
|
|
|
|
$user->setRememberToken(Str::random(60));
|
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
|
|
UserPassword::updateOrCreate(
|
|
|
|
|
['user_id' => $user->getKey()],
|
|
|
|
|
['password' => Hash::make($password)],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
event(new PasswordReset($user));
|
|
|
|
|
|
|
|
|
|
Mail::send(new ResetPassword($user));
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return $status === Password::PASSWORD_RESET
|
|
|
|
|
? to_route(config('uwil-backend.routes.name') . 'login')
|
2026-06-05 17:05:46 +02:00
|
|
|
: back()->withErrors(['email' => trans('auth.token')]);
|
2026-04-28 16:38:04 +02:00
|
|
|
}
|
|
|
|
|
}
|