55 lines
1.7 KiB
PHP
55 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Auth;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Setting;
|
|
use App\Models\User;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\RateLimiter;
|
|
use Illuminate\Support\Str;
|
|
|
|
class LoginController extends Controller
|
|
{
|
|
public function __invoke(Request $request): RedirectResponse
|
|
{
|
|
if (! Setting::isSetupComplete() || User::count() === 0) {
|
|
return redirect()->route('setup.index');
|
|
}
|
|
|
|
$credentials = $request->validate([
|
|
'email' => ['required', 'email'],
|
|
'password' => ['required', 'string', 'min:6'],
|
|
'remember' => ['nullable', 'boolean'],
|
|
]);
|
|
|
|
$throttleKey = Str::transliterate(Str::lower($credentials['email']) . '|' . $request->ip());
|
|
|
|
if (RateLimiter::tooManyAttempts($throttleKey, 5)) {
|
|
$seconds = RateLimiter::availableIn($throttleKey);
|
|
|
|
return back()
|
|
->withErrors(['email' => "Troppi tentativi. Riprova tra {$seconds} secondi."])
|
|
->withInput($request->only('email', 'remember'));
|
|
}
|
|
|
|
if (! Auth::attempt([
|
|
'email' => $credentials['email'],
|
|
'password' => $credentials['password'],
|
|
], $request->boolean('remember'))) {
|
|
RateLimiter::hit($throttleKey);
|
|
|
|
return back()
|
|
->withErrors(['email' => 'Credenziali non valide.'])
|
|
->withInput($request->only('email', 'remember'));
|
|
}
|
|
|
|
RateLimiter::clear($throttleKey);
|
|
$request->session()->regenerate();
|
|
activity()->causedBy(auth()->user())->log('login');
|
|
|
|
return redirect()->intended(route('dashboard'));
|
|
}
|
|
} |