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

Laravel登录成功仍重定向至登录页及自定义admins表认证求助

Hey there! Let's tackle your Laravel authentication issues one by one—they’re super common when working with custom user tables, so let’s get you sorted:

1. Setting Up Authentication with the admins Table

You’ve already started by updating your User model to point to the admins table, but we need to tweak a few more configurations to make Laravel’s built-in auth system play nice with it:

  • Finalize the User model details
    Double-check that your model matches the structure of your admins table:

    // App/Models/User.php
    class User extends Authenticatable
    {
        use HasApiTokens, HasFactory, Notifiable;
    
        // You already have this line
        protected $table = 'admins';
    
        // Add these if your table uses a non-standard primary key
        protected $primaryKey = 'admin_id'; // Replace with your actual primary key
        public $incrementing = true; // Set to false if using a non-incrementing key
    
        // Disable timestamps if your table doesn't have created_at/updated_at
        public $timestamps = false;
    }
    
  • Update the auth configuration file
    Open config/auth.php and adjust the guards and providers to use your admins table:

    // config/auth.php
    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'admins', // Swap from 'users' to 'admins'
        ],
    ],
    
    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\User::class,
        ],
        // Add this provider for admins
        'admins' => [
            'driver' => 'eloquent',
            'model' => App\Models\User::class,
        ],
    ],
    

    If you want to keep separate guards for regular users and admins later, you can add a dedicated admin guard instead of modifying the web guard.

  • Tweak your login controller
    If you’re using Laravel’s built-in auth scaffolding (Breeze/UI), make sure the login controller uses the correct guard. Add this method to your LoginController:

    // App/Http/Controllers/Auth/LoginController.php
    use Illuminate\Support\Facades\Auth;
    
    protected function guard()
    {
        return Auth::guard('web'); // Match the guard you configured above
    }
    

    Also, set the redirect path for successful logins:

    protected $redirectTo = '/admin/dashboard'; // Your admin homepage
    
2. Fixing the "Redirect to Login Page After Successful Login" Issue

This usually happens when the authentication session isn’t being saved correctly, or there’s a mismatch between the guard used for login and the guard checked by the auth middleware. Here’s how to fix it:

  • Ensure guard consistency
    If you modified the web guard to use the admins provider, your routes should use the standard auth middleware. If you created a dedicated admin guard, use auth:admin instead:

    // routes/web.php
    Route::middleware(['auth'])->group(function () {
        // Your admin routes here
    });
    
  • Check session configuration
    Open config/session.php and verify these settings match your environment:

    'domain' => env('SESSION_DOMAIN', null), // Should match your site's domain
    'secure' => env('SESSION_SECURE_COOKIE', false), // Set to true if using HTTPS
    'path' => '/',
    'http_only' => true,
    

    Incorrect session settings can prevent the auth cookie from being stored.

  • Clear your application caches
    Sometimes old cached configurations cause issues. Run these commands in your terminal:

    php artisan config:clear
    php artisan cache:clear
    php artisan route:clear
    
  • Verify your admins table structure
    Make sure your table has:

    • A unique identifier (like email or username) for login
    • A password field that stores hashed values (use Hash::make() when creating admin users)
      If you’re using a field other than email for login, define it in your User model:
    public function getAuthIdentifierName()
    {
        return 'username'; // Replace with your login field
    }
    
  • Debug the auth state
    Add this line to your admin dashboard route to confirm if the user is actually authenticated:

    dd(Auth::check(), Auth::user());
    

    If Auth::check() returns false, check that your User model’s primary key matches the table, and that the storage/framework/sessions directory has write permissions (for file-based sessions).


内容的提问来源于stack exchange,提问作者Birendra Gurung

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 04:19:56