Laravel 5.0 Hash :: check和Auth :: Attempt总是返回false

时间:2016-03-08 17:43:21

标签: laravel authentication hash laravel-5

我现在正在开发一个网站。我总是使用Laravel Auth::attempt进行身份验证,之前它运行良好,但现在它不起作用。我刚刚从PHPMyAdmin删除了我的数据库并迁移了我的所有迁移。从那时起,Auth::attemptHash::check始终返回false。

这是我的DevelopmentSeeder:

\App\Models\Employee\UserAccount::create([
    'AccountName' => 'admin',
    'Username' => 'superadmin',
    'Password' => \Illuminate\Support\Facades\Hash::make('hello'),
    'UserType' => 'Super Admin'
]);

我在控制器中尝试了这个:

$acc = UserAccount::where('AccountName', '=', 'admin')
                    ->where('Username', '=', 'superadmin')
                    ->where('IsActive', '=', 1)->get();
if(count($acc) > 0){
    dd(Hash::check('hello', $acc[0]->Password));
}

模具转储总是返回false。我已经尝试了Auth::attempt但它也返回了错误。

这是我的UserAccount模型:

use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class UserAccount extends Model implements AuthenticatableContract, CanResetPasswordContract {

    use Authenticatable, CanResetPassword;

    protected $table = "UserAccounts";
    protected $fillable = ['AccountName', 'EmployeeID', 'Username', 'Password', 'Email', 'UserType'];
    protected $hidden = ['Password', 'remember_token'];

    public function getAuthIdentifier(){
        return $this->getKey();
    }

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

    public function getRememberToken(){
        return $this->remember_token;
    }

    public function setRememberToken($value){
        $this->remember_token = $value;
    }

    public function getRememberTokenName(){
        return 'remember_token';
    }

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

    public function getEmailForPasswordReset(){
        return $this->Email;
    }
}

1 个答案:

答案 0 :(得分:1)

我发现我哪里出错了。因此,在我的UserAccount模型中,我将密码设置为setPasswordAttributebcrypt,然后再将其存储在数据库中。

但是在我的DevelopmentSeeder中,我使用create()方法加密了密码。

因此,密码被双重加密。

解决方案是删除Hash::make中的DevelopmentSeeder