You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 5.6.7登录成功后始终重定向至登录页问题求助

Troubleshooting Laravel Auth Redirect Loop After Valid Credentials

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 the web guard, so ensure you have this line (or that you haven't overridden it incorrectly):
    protected $guard = 'web';
    
  • Next, open config/auth.php and confirm the defaults.guard is set to web, and the guards.web configuration points to the correct provider (usually users).

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 .env file:
    • Ensure SESSION_DRIVER is set to file (the default) or database (if you've run the session migration).
    • Confirm SESSION_DOMAIN is set correctly—for local development, this can usually be left blank or set to localhost.

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 the Authenticatable trait):
    use Illuminate\Foundation\Auth\User as Authenticatable;
    
    class User extends Authenticatable
    {
        // ... rest of your model code
    }
    
  • If you're using a field other than email for login (like a username or phone number), you need to override the username() method in your LoginController.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 the redirectTo property 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:02:10