shop + cart + user login with school
This commit is contained in:
@@ -16,6 +16,7 @@ use Vanguard\Repositories\Session\SessionRepository;
|
||||
use Vanguard\Repositories\User\UserRepository;
|
||||
use Vanguard\Services\Auth\ThrottlesLogins;
|
||||
use Vanguard\User;
|
||||
use Vanguard\Models\School;
|
||||
|
||||
class LoginController extends Controller
|
||||
{
|
||||
@@ -30,32 +31,58 @@ class LoginController extends Controller
|
||||
/**
|
||||
* Show the application login form.
|
||||
*/
|
||||
public function show(): View
|
||||
public function show($school = null): View
|
||||
{
|
||||
// Debug: aggiungiamo un log per verificare se arriviamo qui
|
||||
\Log::info('LoginController::show chiamato con school = ' . ($school ?? 'null'));
|
||||
|
||||
// Cerca la scuola in base allo slug
|
||||
$schoolData = null;
|
||||
if ($school) {
|
||||
$schoolData = School::where('slug', $school)->first();
|
||||
}
|
||||
|
||||
return view('auth.login', [
|
||||
'socialProviders' => config('auth.social.providers'),
|
||||
'school_slug' => $school,
|
||||
'school_logo' => $schoolData ? $schoolData->logo : null,
|
||||
]);
|
||||
}
|
||||
|
||||
public function login(LoginRequest $request, SessionRepository $sessions): Response|RedirectResponse
|
||||
{
|
||||
// Debug: aggiungiamo un log per verificare se arriviamo qui
|
||||
\Log::info('LoginController::login chiamato con input: ' . json_encode($request->all()));
|
||||
|
||||
// In case that request throttling is enabled, we have to check if user can perform this request.
|
||||
// We'll key this by the username and the IP address of the client making these requests into this application.
|
||||
$throttles = (bool) setting('throttle_enabled');
|
||||
|
||||
//Redirect URL that can be passed as hidden field.
|
||||
// Redirect URL that can be passed as hidden field.
|
||||
$to = $request->has('to') ? '?to=' . $request->get('to') : '';
|
||||
|
||||
if ($throttles && $this->hasTooManyLoginAttempts($request)) {
|
||||
return $this->sendLockoutResponse($request);
|
||||
}
|
||||
|
||||
// Validazione del campo school
|
||||
$schoolSlug = $request->input('school');
|
||||
if ($schoolSlug) {
|
||||
$school = School::where('slug', $schoolSlug)->first();
|
||||
if (!$school) {
|
||||
return redirect()->to('login' . $to)
|
||||
->withErrors(['school' => trans('auth.school_not_found')]);
|
||||
}
|
||||
// Salva lo school_id nella sessione
|
||||
$request->session()->put('school_id', $school->id);
|
||||
} else {
|
||||
// Se il campo school è vuoto, possiamo gestire il caso di default
|
||||
return redirect()->to('login' . $to)
|
||||
->withErrors(['school' => trans('auth.school_required')]);
|
||||
}
|
||||
|
||||
$credentials = $request->getCredentials();
|
||||
|
||||
if (! Auth::validate($credentials)) {
|
||||
// If the login attempt was unsuccessful we will increment the number of attempts
|
||||
// to log in and redirect the user back to the login form. Of course, when this
|
||||
// user surpasses their maximum number of attempts they will get locked out.
|
||||
if ($throttles) {
|
||||
$this->incrementLoginAttempts($request);
|
||||
}
|
||||
@@ -94,7 +121,7 @@ class LoginController extends Controller
|
||||
$this->clearLoginAttempts($request);
|
||||
}
|
||||
|
||||
// Redirezione basata sul ruolo con la prima lettera maiuscola e prefisso 'userarea/'
|
||||
// Redirezione basata sul ruolo
|
||||
if ($user->hasRole('Admin')) {
|
||||
return redirect()->to('userarea/admin.php');
|
||||
} elseif ($user->hasRole('User')) {
|
||||
@@ -105,11 +132,9 @@ class LoginController extends Controller
|
||||
return redirect()->to('userarea/school_dashboard.php');
|
||||
}
|
||||
|
||||
// Fallback nel caso il ruolo non corrisponda
|
||||
return redirect()->intended('userarea/default.php');
|
||||
}
|
||||
|
||||
|
||||
protected function logoutAndRedirectToTokenPage(Request $request, $user, ?string $redirectPage): RedirectResponse
|
||||
{
|
||||
Auth::logout();
|
||||
|
||||
Reference in New Issue
Block a user