Laravel 5.7 认证
Laravel 5.7 Authentication
我是新手,正在学习laravel 5.7。注册已完成,但在登录期间验证时遇到问题,Auth::attempt 总是 returns false
我尝试使用 Hash::check('plain-text','Hashed password)
结果 returns 正确
问题是 Auth::attempt
总是 returns false
UserController.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','y$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();
}
}
Users.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"
}
Laravel 默认使用 email
来授权用户,因此请尝试覆盖您在 LoginController
中使用的字段,添加以下内容:
public function username()
{
return 'username';
}
您不需要尝试重写登录功能(既不注册),因为这些功能在 Laravel 中开箱即用。您需要做的是:
在您的 login.blade.php
中排名第一
<form action="/login" method="post" autocomplete="on">
然后在你的 web.php
Route::post('login', 'Auth\LoginController@login');
现在去你的App\Http\Auth\LoginController
// Add this function to overwrite default email to username
public function username()
{
return 'username';
}
我是新手,正在学习laravel 5.7。注册已完成,但在登录期间验证时遇到问题,Auth::attempt 总是 returns false
我尝试使用 Hash::check('plain-text','Hashed password)
结果 returns 正确
问题是 Auth::attempt
总是 returns false
UserController.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','y$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();
}
}
Users.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"
}
Laravel 默认使用 email
来授权用户,因此请尝试覆盖您在 LoginController
中使用的字段,添加以下内容:
public function username()
{
return 'username';
}
您不需要尝试重写登录功能(既不注册),因为这些功能在 Laravel 中开箱即用。您需要做的是:
在您的 login.blade.php
<form action="/login" method="post" autocomplete="on">
然后在你的 web.php
Route::post('login', 'Auth\LoginController@login');
现在去你的App\Http\Auth\LoginController
// Add this function to overwrite default email to username
public function username()
{
return 'username';
}