我是新手,正在学习 laravel 5.7。注册已完成,但在登录期间进行身份验证时出现问题,Auth::attempt 始终返回 false
我尝试使用Hash::check('plain-text','Hashed password)
它返回 true 问题Auth::attempt
总是返回 false
用户控制器.php:
// Registration code
public function register(Request $request){
$request->validate([
'firstName' => 'required',
'lastName' => 'required',
'username' => 'required|min:3',
'password' => 'min:6|required_with:confirm_password|same:confirm_password',
'confirm_password' => 'min:6'
]);
Users::create([
'firstName' => request('firstName'),
'lastName' => request('lastName'),
'username' => request('username'),
'password' => Hash::make(request('password'))
]);
notify()->success('Registered Successfully!');
return redirect('/');
}
// Login code
public function login(Request $request){
// This returns true
// if(Hash::check('123456','$2y$10$BrOg1JtnX7hAX05gbT9p0OZFQB9mFKtcz0m5Ks2rSHIN//B20dODgA.')){
// return 'OK';
// }
$credentials = request([
'username',
'password'
]);
// Always returns false
if(Auth::attempt($credentials)){
notify()->success('Welcome!');
return back();
}else{
notify()->warning('Credentials not found!');
return back();
}
}
用户.php:
//Model
class Users extends Model
{
//
protected $guarded = [];
}
auth.php:
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Users::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
迁移:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('username')->unique();
$table->string('password');
$table->string('firstName');
$table->string('lastName');
$table->rememberToken();
$table->timestamps();
});
}
login.blade.php:
<form action="/" method="post" autocomplete="on">
@csrf
<h1>Log in</h1>
<p>
<label for="Username" data-icon="u"> Username</label>
<input id="Username" name="username" required="required" type="text" placeholder=""/>
</p>
<p>
<label for="Password" data-icon="p"> Password </label>
<input id="Password" name="password" required="required" type="password" placeholder=""/>
</p>
<p class="login button">
<input type="submit" value="Login"/>
</p>
<p class="change_link">
Not a member yet ?
<a href="/register" class="to_register">Join us</a>
</p>
</form>
笔记
我很肯定我得到了正确的输入
// Returns correct information
{
"username": "yeah",
"password": "123456"
}