简体   繁体   中英

laravel 5.1 login not working

I used laravel 5.1 authentication for login . but Auth::attempt($credentials) returns false always!

I used the following route to create user:

Route::get('newuser', function () {
  return User::create([
    'username' => 'admin',
    'email' => 'admin@gmail.com',
    'password' => Hash::make('123'),
  ]);
});

I added a simple postLogin function to override this function in AuthController:

public function postLogin(Request $request) {
    $credentials = $this->getCredentials($request);
    if(Auth::attempt($credentials)) {
        return 'ok';
    }
    return 'nok';
}

getCredentials is laravel function with this content:

protected function getCredentials(Request $request)
{
    return $request->only($this->loginUsername(), 'password');
}

There could be multiple reasons for this thing. I would suggest you to check the lenght of the password column in phpMyAdmin in your user table.

Change the password column to maximum lenght in database in the table where you are storing the password.

Another I would suggest you try with following code if it is working.

 public function postLogin()
   {
    // Login credentials
    $credentials = array(
        'username'    => 'admin@gmail.com',
        'password' => '123',
    );
   // Authenticate the user
   if (Auth::attempt($credentials))
{
    return 'ok';
}
else{
    return "failed";
}

Hope it would help.

  1. Dont forget to change the password column length in database,Change it to maximum.

  2. Change the password datatype to LONGTEXT with length to maximun or 1000.

Last and important is to delete all the records from the table and restart the process,because the priviously saved passed was saved with another lenght and beacuse of that the currunt hashed password does not matches the old one.

So truncate that table and try it again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM