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

Laravel多认证登录无响应:修改用户表字段后跳转无报错

Hey there! Let's tackle this multi-auth login issue you're facing. That silent redirect back to /login without any error messages usually means Laravel's authentication system is still looking for the default email and password fields—while your users table uses u_ prefixed versions. Here's how to fix this step by step:

1. Update Your User Model

First, you need to tell Laravel to use your prefixed fields for authentication. Override the default authentication methods in your app/Models/User.php (or app/User.php depending on your Laravel version):

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;

    // Confirm the table name (you mentioned it's still `users`, so this is just explicit)
    protected $table = 'users';

    // Define fillable fields with their u_ prefixes
    protected $fillable = [
        'u_name', 'u_email', 'u_password', // Add other prefixed fields as needed
    ];

    // Override the default identifier field (instead of `email`, use `u_email`)
    public function getAuthIdentifierName()
    {
        return 'u_email';
    }

    // Override the password field to point to `u_password`
    public function getAuthPassword()
    {
        return $this->u_password;
    }
}

2. Adjust Authentication Guard Configuration

Next, update your config/auth.php file to ensure the authentication provider uses your prefixed username field. Modify the users provider section:

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\Models\User::class,
        // Specify the username field Laravel should check
        'username' => 'u_email',
    ],

    // If you have a custom guard for multi-auth (e.g., admins), define it similarly here
],

3. Fix Your Login Form Fields

Make sure your login.blade.php form uses the correct input names that match your setup. The email input should use u_email as its name, while the password can stay password (Laravel will map it to u_password via your model):

<form method="POST" action="{{ route('login') }}">
    @csrf

    <div class="form-group">
        <label for="u_email">Email Address</label>
        <input 
            id="u_email" 
            type="email" 
            class="form-control @error('u_email') is-invalid @enderror" 
            name="u_email" 
            value="{{ old('u_email') }}" 
            required 
            autocomplete="u_email" 
            autofocus
        >

        @error('u_email')
            <span class="invalid-feedback" role="alert">
                <strong>{{ $message }}</strong>
            </span>
        @enderror
    </div>

    <div class="form-group">
        <label for="password">Password</label>
        <input 
            id="password" 
            type="password" 
            class="form-control @error('password') is-invalid @enderror" 
            name="password" 
            required 
            autocomplete="current-password"
        >

        @error('password')
            <span class="invalid-feedback" role="alert">
                <strong>{{ $message }}</strong>
            </span>
        @enderror
    </div>

    <!-- Remember me & submit button -->
    <div class="form-group">
        <div class="checkbox">
            <label>
                <input type="checkbox" name="remember" {{ old('remember') ? 'checked' : '' }}> Remember Me
            </label>
        </div>
    </div>

    <div class="form-group">
        <button type="submit" class="btn btn-primary">Login</button>

        @if (Route::has('password.request'))
            <a class="btn btn-link" href="{{ route('password.request') }}">
                Forgot Your Password?
            </a>
        @endif
    </div>
</form>

4. Update the Login Controller (If Using Default Trait)

If you're using Laravel's default LoginController (in app/Http/Controllers/Auth/LoginController.php), override the username() method to explicitly tell the AuthenticatesUsers trait to use u_email:

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginController extends Controller
{
    use AuthenticatesUsers;

    protected $redirectTo = '/home';

    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    // Override the default username field
    public function username()
    {
        return 'u_email';
    }
}

5. Verify Password Hashing During Registration

Double-check that your registration logic is hashing the u_password field correctly. In your RegisterController:

protected function create(array $data)
{
    return User::create([
        'u_name' => $data['name'],
        'u_email' => $data['email'],
        'u_password' => Hash::make($data['password']), // Critical: hash the password here
    ]);
}

If you skip hashing u_password, Laravel won't be able to match it during login, leading to that silent redirect.

6. Clear Configuration Cache

After making these changes, clear Laravel's config cache to ensure the new settings take effect:

php artisan config:clear

Quick Final Checks

  • Confirm your users table has u_email (unique) and u_password (varchar(255)) fields.
  • Temporarily enable APP_DEBUG=true in your .env file to catch any hidden errors that might be causing the redirect.
  • Ensure your session storage is working correctly (session issues can sometimes cause silent redirects).

Try these steps in order, and your login functionality should start working as expected!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 06:57:40