Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 authentication without remember_token

I am using an existing database, and I'm not allowed to modify the tables, so adding a remember_token is not an option, but without it I'm unable to login. When I try to login Laravel does check the credentials and returns whether they match the records, but it only refreshes the page. I am pretty sure the remember_token is the cause since I've encountered this problem before, but this time I can't add a column to my users table.

Is there a way to use the out-of-the-box authentication without the remember_token?

like image 655
Willem Avatar asked Apr 18 '17 08:04

Willem


People also ask

What is auth ()- user () in Laravel?

Laravel includes built-in authentication and session services which are typically accessed via the Auth and Session facades. These features provide cookie-based authentication for requests that are initiated from web browsers. They provide methods that allow you to verify a user's credentials and authenticate the user.

How do I log in manually Laravel?

Manually Logging In A User If you need to log an existing user instance into your application, you may call the login method with the user instance: Auth::login($user); This is equivalent to logging in a user via credentials using the attempt method.


2 Answers

In your User model add:

  /**
   * Overrides the method to ignore the remember token.
   */
  public function setAttribute($key, $value)
  {
    $isRememberTokenAttribute = $key == $this->getRememberTokenName();
    if (!$isRememberTokenAttribute)
    {
      parent::setAttribute($key, $value);
    }
  }

Credits: https://laravel.io/forum/05-21-2014-how-to-disable-remember-token

like image 53
Autista_z Avatar answered Sep 20 '22 12:09

Autista_z


Since Laravel v5.3.27 you can also disable the remember me functionality by setting the $rememberTokenName to false in your User model.

class User extends Authenticatable
{
    use Notifiable;


    protected $rememberTokenName = false;

    // ...
}

source: this commit

like image 24
Roland Starke Avatar answered Sep 19 '22 12:09

Roland Starke