簡體   English   中英

Laravel Auth::attemp() 總是假的

[英]Laravel Auth::attemp() always false

首先,我已經看到了這個主題(以及許多其他主題):

Laravel Auth::attempt() 總是假的?

Laravel 5 Auth:attempt() 總是返回 false

但是我一直無法登錄我的應用程序。 我使用的是 Laravel 5.6 ,我的表名為oc_users

DB::table('oc_users')->insert(
[
    'email'     => 'myemail@gmail.com',
    'pwd'       => bcrypt('123456'),
    'active'    => true
]);

我轉到文件 Laravel > App > User.php 並更改為:

namespace App;

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

class User extends Authenticatable
{
    use Notifiable;

    protected $table = 'oc_users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['email', 'pwd'];

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

以及執行登錄的代碼:

public function form_login(Request $request)
{   
    if($request->filled('email') == FALSE || $request->filled('pwd') == FALSE)
        return Redirect::back()->withErrors('Fill the credentials');

    if (Auth::attempt(['email' => $request->email, 'pwd' => $request->pwd]))
        return redirect()->intended('app');

    return Redirect::back()->withErrors('Email or password wrong');       
}

我還轉到文件 App > Config > Auth.php 試圖找出是否有我需要更改的表,但它似乎在 Laravel 5.6 中不存在

根據我以前的經驗,auth :: attempt()函數對password列進行了默認檢查。

為了使您的代碼正常工作,您可以嘗試一下。

在用戶模型中添加:

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

此函數將覆蓋Authenticatable.php的默認getAuthPassword()函數。

Laravel 現在使用bcrypt

class User extends Model {

    public function setPasswordAttribute($value) {
         $this->attributes['password'] = bcrypt($value);
    }

}

暫無
暫無

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

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