[英]Auth::Attempt(username,password) doesn't work
我对 laravel 真的很陌生,但有使用 PHP 的经验,我在使用 Auth::attempt() 方法时遇到了这个问题。 似乎即使认为 $request->get() 获得了尝试方法不起作用的值。 我尝试使用 Schema::hasTable 检查我的数据库表,是的,它存在。 我没有使用 email 和密码,而是使用用户名和密码进行登录。
登录控制器.php
class LoginController extends Controller
{
public function login(){
return view('pages.login');
}
public function dashboard(){
return view('pages.dashboard');
}
public function checklogin(Request $request)
{
$rules = array(
'Username' => 'required|string|min:5',
'Password' => 'required|min:6'
);
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
return Redirect::to('/login')
->withErrors($validator) // send back all errors to the login form
->withInput($request->except('Password')); // send back the input (not the password) so that we can repopulate the form
}else{
$userdata = array(
'Password' => $request->get('Password'),
'Username' => $request->get('Username')
);
if (Auth::attempt($userdata,true)) {
// validation successful!
echo 'SUCCESS!';
echo Auth::user()->FirstName;
} else {
// validation not successful, send back to form
echo "ERROR!";
}
}
//Just checking if the value was really been posted
echo $request->get('username').$request->get('password');
}
public function logout()
{
Auth::logout();
return redirect('/');
}
}
这是我创建的 model
用户信息.php
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Auth\Authenticatable as AuthAuthenticatable;
class UsersInfo extends Model implements Authenticatable
{
use AuthAuthenticatable;
protected $table = 'UsersInfo';
public $primaryKey = 'id';
protected $fillable = [
'Username','Password','MobileNum','AccountNum','FirstName','LastName','Address'
];
public function getAuthPassword(){
return $this->Password;
}
}
auth.php
<?php
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'UsersInfo',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'UsersInfo',
],
'api' => [
'driver' => 'token',
'provider' => 'UsersInfo',
'hash' => false,
],
],
'providers' => [
'Users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'UsersInfo' => [
'driver' => 'eloquent',
'model' => App\UsersInfo::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
'passwords' => [
'UsersInfo' => [
'provider' => 'UsersInfo',
'table' => 'password_resets',
'expire' => 60,
'throttle' => 60,
],
],
'password_timeout' => 10800,
];
在传递凭据以attempt
保存密码的字段时,必须将其命名为password
(在这种情况下)。 这就是用户提供者知道凭证中的哪个字段应该是密码的方式。 密码是在找到记录后检查的,不是查询的一部分。
$userdata = [
'password' => $request->input('Password'),
'Username' => $request->input('Username'),
];
密码是散列的,您不能直接将其与数据库值进行比较, password
字段不是查找用户的查询的一部分。 凭证数组中的所有其他内容都是 where 条件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.