Laravel 5.2 登录总是抛出“这些凭据与我们的记录不匹配”。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35948112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:23:56  来源:igfitidea点击:

Laravel 5.2 login always throws "These credentials do not match our records."

phplaravellaravel-5

提问by PeterTheLobster

I am trying to implement authentification using Laravel 5.2. I have been on this for hours, but I always get "These credentials do not match our records." when attempting to login.

我正在尝试使用 Laravel 5.2 实现身份验证。我已经研究了几个小时,但我总是得到“这些凭据与我们的记录不符”。尝试登录时。

I have tried messing with the routes, adjusting password column size in users table, trying custom login validator etc. Just cannot get it to work.

我试过弄乱路由,调整用户表中的密码列大小,尝试自定义登录验证器等。只是无法让它工作。

This is how I made my users table via migration:

这就是我通过迁移制作用户表的方式:

Notes:

笔记:

  1. I have to use raw statements for a school project.
  2. Laravel suggests password fields need to be 60 characters (tried even 100 already)
  3. Laravel requires 100 char column for remember_token
  4. Edit: The database and the users table were created successfully and user data gets saved on registration.
  1. 我必须在学校项目中使用原始语句。
  2. Laravel 建议密码字段需要 60 个字符(甚至已经尝试了 100 个)
  3. Laravel 需要 100 个字符列作为 remember_token
  4. 编辑:数据库和用户表已成功创建,用户数据在注册时保存。

Code:

代码:

    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)
    )");

Here is my login form:

这是我的登录表单:

<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>

My routes:

我的路线:

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');
});

AuthControllerclass:

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');
    }
}

采纳答案by PeterTheLobster

After messing around for a while I figured out it had to do something with the database. Sure enough, after some testing I figured out the email and password columns should look like this:

折腾了一段时间后,我发现它必须对数据库做一些事情。果然,经过一些测试,我发现电子邮件和密码列应该是这样的:

 password CHAR(60) NOT NULL,
 email VARCHAR(255) character set utf8 collate utf8_bin not null

My guess is that the password column was the problem and that the string comparison Laravel performs does not evaluate well against VARCHAR password columns.

我的猜测是密码列是问题所在,并且 Laravel 执行的字符串比较对 VARCHAR 密码列的评估效果不佳。