Laravel 5.6.7登录成功后始终重定向至登录页问题求助
Hey there! Let's break down this frustrating issue you're hitting—when valid login credentials just send you right back to the login page, but invalid ones trigger the correct error. Since you used php artisan make:auth and only modified the login template, here are the most likely fixes to check:
1. Double-Check the CSRF Token in Your Custom Login Form
It's easy to accidentally omit this when swapping templates, and Laravel will silently reject the login request (without throwing an error) if the CSRF token is missing or invalid.
Make sure your login form includes the @csrf Blade directive right inside the <form> tag:
<form method="POST" action="{{ route('login') }}"> @csrf <!-- Your email/password fields here --> </form>
If you're not using Blade, manually add the token field:
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
2. Verify Authentication Guard Configuration
Laravel uses "guards" to handle user authentication, and a mismatch here can cause silent redirects.
- First, check your
LoginController.php—does it specify the correct guard? By default, it should use thewebguard, so ensure you have this line (or that you haven't overridden it incorrectly):protected $guard = 'web'; - Next, open
config/auth.phpand confirm thedefaults.guardis set toweb, and theguards.webconfiguration points to the correct provider (usuallyusers).
3. Fix Session/Cookie Issues
If sessions aren't being saved properly, Laravel can't remember that you've logged in, so it sends you back to login. Try these steps:
- Clear your browser's cookies and cache for your local domain (often localhost or 127.0.0.1).
- Run these Artisan commands to clear Laravel's caches:
php artisan cache:clear php artisan config:clear php artisan session:clear - Check your
.envfile:- Ensure
SESSION_DRIVERis set tofile(the default) ordatabase(if you've run the session migration). - Confirm
SESSION_DOMAINis set correctly—for local development, this can usually be left blank or set tolocalhost.
- Ensure
4. Ensure Your User Model is Set Up Correctly
Your User.php model needs to properly implement Laravel's authentication traits. Double-check:
- It should extend
Illuminate\Foundation\Auth\User(which includes theAuthenticatabletrait):use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { // ... rest of your model code } - If you're using a field other than
emailfor login (like a username or phone number), you need to override theusername()method in yourLoginController.php:public function username() { return 'username'; // Replace with your actual login field }
5. Check Redirect Path Logic
If the redirect destination after login has authentication requirements (or doesn't exist), Laravel might bounce you back to login.
- In
LoginController.php, verify theredirectToproperty is set to a valid, accessible route:protected $redirectTo = '/home'; // Or your desired post-login route - If you're using a custom
redirectTo()method instead, make sure it returns a valid path and doesn't accidentally redirect back to login.
6. Rule Out Custom Login Logic Overrides
If you modified any methods in LoginController.php that override the default AuthenticatesUsers trait behavior, you might have skipped a critical step (like logging the user into the session). For example, if you overrode the login() method, ensure you're calling:
$this->guard()->login($user);
This is what tells Laravel to persist the user's session after authentication.
If none of these fixes work, sharing the full code of your User.php and LoginController.php would help narrow down the exact issue!
内容的提问来源于stack exchange,提问作者Khrisna Gunanasurya

