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:
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
Usermodel details
Double-check that your model matches the structure of youradminstable:// 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
Openconfig/auth.phpand adjust the guards and providers to use youradminstable:// 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
adminguard instead of modifying thewebguard.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 yourLoginController:// 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
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 thewebguard to use theadminsprovider, your routes should use the standardauthmiddleware. If you created a dedicatedadminguard, useauth:admininstead:// routes/web.php Route::middleware(['auth'])->group(function () { // Your admin routes here });Check session configuration
Openconfig/session.phpand 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:clearVerify your
adminstable structure
Make sure your table has:- A unique identifier (like
emailorusername) for login - A
passwordfield that stores hashed values (useHash::make()when creating admin users)
If you’re using a field other thanemailfor login, define it in yourUsermodel:
public function getAuthIdentifierName() { return 'username'; // Replace with your login field }- A unique identifier (like
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()returnsfalse, check that yourUsermodel’s primary key matches the table, and that thestorage/framework/sessionsdirectory has write permissions (for file-based sessions).
内容的提问来源于stack exchange,提问作者Birendra Gurung

