Harry Geo
Harry Geo

Reputation: 1173

Storing remember_token to a separate table than users?

is it possible to set another table & column to store my remember tokens? I know that the framework tries to automatically find a remember_token column in my "users" model, but I want to store it separately from users. Is there a way to configure my default tokens table? Thank you

P.S - I'm using laravel 5

Upvotes: 1

Views: 886

Answers (2)

Joshua Rinzlo Arco
Joshua Rinzlo Arco

Reputation: 11

I believe the way Laravel works uses a seperate column in the users table to store a single remember_me token. From my understanding it seems that logging out resets this token regardless of storing the token in a cookie (correct me if I'm wrong). https://github.com/laravel/ideas/issues/971 If you log in with remember_me checked on your personal computer, then again on your phone, and maybe again with any other device, then finally the act of signing out on any device using the remember_me token or not will reset this token in the DB.

If Laravel had a separate table, able to remember each device, it might solve the problem.

Upvotes: 1

Konstantin L
Konstantin L

Reputation: 301

First, you need to create separate model for storing remember tokens and define a relationship on your User model like so

public function rememberToken() {
    return $this->hasOne('RememberToken');
}

Then you need to override methods on your User model, originally defined in Authenticatable trait. Override getRememberToken() and setRememberToken() methods. You will also need to override getRememberTokenName() as it is used in where clause in EloquentUserProvider::retrieveByToken() see EloquentUserProvider line 60. In order for this to work properly you probably have to add global scope to your User model to join remember_tokens table on every query, and return 'remember_tokens.token' from getRememberTokenName() method.

Think twice as it seems more trouble than it is worth. Why would you want to store your tokens separately anyway?

Upvotes: 3

Related Questions