Auth::attempt($credentials) 在 Laravel 8.76.2 中总是 returns false
Auth::attempt($credentials) Always returns false in Laravel 8.76.2
我在 Laravel 8.7 中使用 Auth::attempt($credentials),它总是 returns 错误。
我的登录Blade是
resources/views/login/login.blade.php
<form action="{{ route('login.custom') }}" method="post">
@csrf
<div class="input-group mb-3">
<input type="email" class="form-control" placeholder="Email" name="email">
<div class="input-group-append">
<div class="input-group-text">
<span class="fas fa-envelope"></span>
</div>
</div>
</div>
@error('email')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
<div class="input-group mb-3">
<input type="password" class="form-control" placeholder="Password" name="password">
<div class="input-group-append">
<div class="input-group-text">
<span class="fas fa-lock"></span>
</div>
</div>
</div>
@error('password')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
<div class="row">
<div class="col-8">
<div class="icheck-primary">
<input type="checkbox" id="remember">
<label for="remember">
Remember Me
</label>
</div>
</div>
<!-- /.col -->
<div class="col-4">
<button type="submit" class="btn btn-primary btn-block">Sign In</button>
</div>
<!-- /.col -->
</div>
</form>
路由器
Route::post('custom-login', [LoginController::class, 'customLogin'])->name('login.custom');
我的控制器代码是这样的 - 我正在使用路由器访问控制器
LoginController.php
public function customLogin(Request $request)
{
//dd(print_r($request));
$request->validate([
'email' => 'required',
'password' => 'required',
]);
// $email = $request->input('email');
// $password = $request->input('password');
$credentials = $request->only('email', 'password');
if (Auth::attempt($credentials))
{
return "Success";
}
else
{
return "Fail";
}
//return redirect("login")->withSuccess('Login details are not valid');
}
当我使用 Auth::attempt($credentials) 时,总是返回错误的陈述。请帮助我找出实际问题或建议我最好的解决方案。
我尝试了很多组合来解决这个问题,但仍然没有用。
This is my table structure
This is output
您需要在数据库中散列密码。身份验证系统期望在数据库中对密码进行哈希处理。它将对纯文本版本(用户提交的值)和数据库中的哈希密码进行哈希检查。
"If the user is found, the hashed password stored in the database will be compared with the password
value passed to the method via the array."
Laravel 8.x Docs - Authentication - Manually Authenticating Users
我在 Laravel 8.7 中使用 Auth::attempt($credentials),它总是 returns 错误。
我的登录Blade是
resources/views/login/login.blade.php
<form action="{{ route('login.custom') }}" method="post">
@csrf
<div class="input-group mb-3">
<input type="email" class="form-control" placeholder="Email" name="email">
<div class="input-group-append">
<div class="input-group-text">
<span class="fas fa-envelope"></span>
</div>
</div>
</div>
@error('email')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
<div class="input-group mb-3">
<input type="password" class="form-control" placeholder="Password" name="password">
<div class="input-group-append">
<div class="input-group-text">
<span class="fas fa-lock"></span>
</div>
</div>
</div>
@error('password')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
<div class="row">
<div class="col-8">
<div class="icheck-primary">
<input type="checkbox" id="remember">
<label for="remember">
Remember Me
</label>
</div>
</div>
<!-- /.col -->
<div class="col-4">
<button type="submit" class="btn btn-primary btn-block">Sign In</button>
</div>
<!-- /.col -->
</div>
</form>
路由器
Route::post('custom-login', [LoginController::class, 'customLogin'])->name('login.custom');
我的控制器代码是这样的 - 我正在使用路由器访问控制器
LoginController.php
public function customLogin(Request $request)
{
//dd(print_r($request));
$request->validate([
'email' => 'required',
'password' => 'required',
]);
// $email = $request->input('email');
// $password = $request->input('password');
$credentials = $request->only('email', 'password');
if (Auth::attempt($credentials))
{
return "Success";
}
else
{
return "Fail";
}
//return redirect("login")->withSuccess('Login details are not valid');
}
当我使用 Auth::attempt($credentials) 时,总是返回错误的陈述。请帮助我找出实际问题或建议我最好的解决方案。 我尝试了很多组合来解决这个问题,但仍然没有用。
This is my table structure
This is output
您需要在数据库中散列密码。身份验证系统期望在数据库中对密码进行哈希处理。它将对纯文本版本(用户提交的值)和数据库中的哈希密码进行哈希检查。
"If the user is found, the hashed password stored in the database will be compared with the
password
value passed to the method via the array."
Laravel 8.x Docs - Authentication - Manually Authenticating Users