我正在尝试使用Laravel 5.2实现身份验证。我已经在这几个小时,但我总是得到"这些凭证与我们的记录不符。"在尝试登录时。
我试过搞乱路由,调整用户表中的密码列大小,尝试自定义登录验证器等等。只是无法让它工作。
这是我通过迁移创建用户表的方式:
注意:
代码:
DB::statement("CREATE TABLE users(
id INT PRIMARY KEY AUTO_INCREMENT,
firstname VARCHAR(50) NOT NULL,
lastname VARCHAR(50) NOT NULL,
password VARCHAR(60) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
bio VARCHAR(500),
gender ENUM('F','M') NOT NULL,
birthday DATE NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
updated_at TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
level INT DEFAULT 0,
remember_token VARCHAR(100)
)");
这是我的登录表单:
<form method="POST" action="login">
{!! csrf_field() !!}
<table>
<tr>
<td> <label for="email">E-mail: </label> </td>
<td> <input type="email" name="email" value="{{ old('email') }}"> </td>
</tr>
<tr>
<td> <label for="password">Password: </label> </td>
<td> <input type="password" name="password" id="password"> </td>
</tr>
<tr>
<td> <label for="remember">Remember me: </label> </td>
<td> <input type="checkbox" name="remember"> </td>
</tr>
</table>
<div>
<button type="submit">Login</button>
</div>
</form>
我的路线:
Route::group(['middleware' => ['web']], function () {
Route::get('registration', 'MainController@getRegister');
Route::get('login', 'MainController@getLogin');
Route::get('logout', 'Auth\AuthController@logout');
Route::post('registration', 'Auth\AuthController@postRegister');
Route::post('login', 'Auth\AuthController@postLogin');
});
Route::group(['middleware' => ['auth','web']], function () {
Route::get('/', 'MainController@getIndex');
});
AuthController
上课:
class AuthController extends Controller
{
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
/**
* Where to redirect users after login / registration.
*
* @var string
*/
protected $redirectTo = '/';
protected $loginPath = 'login';
/**
* Create a new authentication controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('web', ['except' => 'logout']);
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'firstname' => 'required|max:50',
'lastname' => 'required|max:50',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
'birthday' => 'required',
'gender' => 'required'
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return User
*/
protected function create(array $data)
{
return User::create([
'firstname' => $data['firstname'],
'lastname' => $data['lastname'],
'birthday' => $data['birthday'],
'gender' => $data['gender'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
protected function logout(){
if (Auth::check()){
Auth::logout();
}
return redirect('login');
}
}
答案 0 :(得分:4)
在搞乱了一段时间后,我发现它必须对数据库做一些事情。果然,经过一些测试,我发现电子邮件和密码列应如下所示:
password CHAR(60) NOT NULL,
email VARCHAR(255) character set utf8 collate utf8_bin not null
我的猜测是密码列是问题,Laravel执行的字符串比较不能很好地对VARCHAR密码列进行评估。