【问题标题】:Using Auth::attempt sounds wrong in Laravel 8在 Laravel 8 中使用 Auth::attempt 听起来是错误的
【发布时间】:2021-03-17 12:56:14
【问题描述】:

我想验证信息并使用“auth ::尝试”进行交易。它在传入数据中返回 true,但在条件下返回 false。我不知道哪里出了问题。

用户模型

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() 并显示为空。我对身份验证交易没有太多经验。如果您能提供帮助,我会很高兴。

【问题讨论】:

  • 你的路由在 web 中间件路由组内吗?
  • dd(eStatus::Active) 看看你得到了什么
  • database 中你的password 的length是什么
  • 密码类型以'blob'格式保存在数据库中。
  • eStatus::Active 值为 1。我得到的那些“StatusID”为 1。所以在这种情况下它必须为真。

标签: php laravel eloquent authorization


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-27
    • 2016-05-17
    • 2017-01-06
    • 2018-02-27
    • 2018-08-03
    • 2013-08-03
    相关资源
    最近更新 更多