簡體   English   中英

Laravel 7 驗證嘗試返回 false

[英]Laravel 7 auth attempt returns false

我似乎找不到解決方案,我的Auth::attempt() function 總是返回 false,我找不到我做錯了什么。 這是我對 tblusers 的遷移:

public function up()
{
    Schema::create('tblusers', function (Blueprint $table) {
        $table->id();
        $table->string('Username')->unique();
        $table->string('FirstName');
        $table->string('LastName');
        $table->string('ContactNo')->unique();
        $table->string('Email')->unique();
        $table->timestamp('EmailVerifiedOn')->nullable();
        $table->string('Password');
        $table->integer('AddedBy');
        $table->rememberToken();
        $table->timestamps();
    });
}

我定制了User model:

class User extends Authenticatable
{

    protected $table = 'tblusers';
    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'Password', 'remember_token',
    ];
}

因此密碼在User::查詢中不再可見,所以我使用Auth::attempt代替。 這就是我插入哈希密碼的方式:

    $password = $request->Password;
    $hash = Hash::make($password);

    $insertData = array(
        ...
        'Email' => $email,
        'Password' => $hash,
        'AddedBy' => 0,
        'created_at' => date('Y-m-d H:i:s')
    );

這就是我使用Auth::attempt的方式,有人可以指出我做錯了什么,因為它總是返回 false。

        $credentials = array(
            "Username" => $username,
            "Password" => Hash::make($password)
        );
        
        return dd(Auth::attempt($credentials));
        if (Auth::attempt($credentials)) {
            $response = true;
        } else {
            $response = false;
        }   

傳遞給attempt的“密碼”字段必須命名為password (完全像這樣)。 您還需要調整 Model 上的getAuthPassword方法,因為默認情況下它期望數據庫中的密碼字段為“密碼”。 如果這是一個新項目和數據庫,我強烈建議您遵循約定,但如果您想保持原樣,則需要進行以下更改:

$credentials = [
    'Username' => $username,
    'password' => $password,
];

請注意,密鑰是password ,我們沒有對密碼進行哈希處理,因為它需要密碼的純文本版本,因此它最終可以對其進行 hash 檢查。

在此 Model 上,您將需要覆蓋getAuthPassword方法:

public function getAuthPassword()
{
    return $this->Password;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM