在 Laravel 8 中使用 Auth::attempt 听起来不对

Using Auth::attempt sounds wrong in Laravel 8

我想验证信息并使用“auth :: attempts”进行交易。它 returns 在传入数据中为真,但在条件下为假。不知道哪里不对。

用户模型

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
    use HasFactory;

    public $timestamps = false;
    public $table = 'User';
    public $primaryKey = 'UserID';
}

控制器

public function post_login(Request $request) {
    $username = $request->Username;
    $password = $request->Password;
    $remember = $request->Remember;
    $localDate = date("Y-m-d H:i:s", $request->input('LocalTime', time()));

    $user = DB::table('User')
      ->where('Username', '=', $username)
      ->where('StatusID', '=', eStatus::Active)
      ->first();

    if ($user) {
      if (Hash::check($password, $user->Password)) {
        if ((int)$user->UserTypeID == (int)eUserTypes::Customer) {
          $customer = DB::table('Customer')
            ->where('CustomerID', '=', $user->CustomerID)
            ->where('StatusID', '=', eStatus::Active)
            ->first();
        }
      }
    }

    $credentials = ['Username' => $username, 'Password' => $password, 'StatusID' => eStatus::Active];
    if (Auth::attempt($credentials)) { //<===========Wrong return condition=====
      $user = Auth::user();
      $s = new Sessionn;
      $s->update([
        'UserID' => Auth::id(),
        'IP' => $request->ip(),
        'Session' => $request->session()->put('id'),
        'LoginDate' => new DateTime(),
        'LocalLoginDate' => $localDate
      ]);
      $s->save();

      echo 'Passed <br>';
      return "success=" . base64_encode("true") . "&msg=" . base64_encode(__('common.login_success_redirect'));
    } else {
      echo 'Do not pass <br>';
      return "success=" . base64_encode("false") . "&errmsg=" . base64_encode(__('common.login_error'));
    }
  }

config/auth.php

return [
    'defaults' => [
        'guard' => 'web',
        'passwords' => 'User',
    ],

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'User',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'User',
            'hash' => false,
        ],
    ],

    'providers' => [
        'User' => [
            'driver' => 'eloquent',
            'model' => App\Models\User::class,
        ],

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],

    'passwords' => [
        'User' => [
            'provider' => 'User',
            'table' => 'password_resets',
            'expire' => 60,
            'throttle' => 60,
        ],
    ],

    'password_timeout' => 10800,
];

信息正确,我要登录的用户名和密码存储在我的数据库中。密码在数据库中进行哈希处理。

我检查了 Auth::user(),结果为空。我对授权交易没有太多经验。如果你能帮上忙,我会很高兴。

模型中的默认密码命名已更改。通过在用户模型中定义用作自定义的密码字段解决了该问题。

/**
 * Get the password for the user.
 *
 * @return string
*/
public function getAuthPassword() {
  return $this->Password;
}