Summer Sale! All accounts are 50% off this week.

rflipo-dev's avatar

TokenGuard (api) retrieving old user after second request

I'm testing my api the following way, by making two requests on the same test :

$this->getJson('v1/ratings', 'POST');
// v1/ratings?api_token=JcM6ftg6icLPGAbUX9E37HGSYsrAYU0GH1AJhoYxEfIi5LwhalRBf0iAcmJ2

Then i make the second request with a different token :

// v1/ratings?api_token=JgYdM890bIPHVYsiddHUp5vXaG9C1qhjLcYdZ1UioFZpPvPANGMqw26RJ3RB

But i'm still logged with the same user...

It comes from Illuminate\Auth\TokenGuard@user :

public function user()
    {
        // If we've already retrieved the user for the current request we can just
        // return it back immediately. We do not want to fetch the user data on
        // every call to this method because that would be tremendously slow.
        if (! is_null($this->user)) {
    // HERE IT GOES
            return $this->user;
        }
    ...
}

Why do my user persists on 2 different requests ?

UPDATE :

Because i'm never leaving my app during my test, so the TokenGuard is never re-build. Turn around by manually refreshing your guard user on your tests

Auth::guard('api')->setUser($user);
0 likes
1 reply
elias's avatar

Thanks for this, it was driving me crazy

Please or to participate in this conversation.