Closed mjzavar closed 6 months ago
Sanctum will check each guard in your config('sanctum.guard')
value in turn, until one provides a valid user. It will do this as soon as the guard is invoked.
If none return a valid user, then personal_access_tokens
are queried by the data provided in the Bearer request header. That ID query in your example comes from here:
/**
* Find the token instance matching the given token.
*
* @param string $token
* @return static|null
*/
public static function findToken($token)
{
if (strpos($token, '|') === false) {
return static::where('token', hash('sha256', $token))->first();
}
[$id, $token] = explode('|', $token, 2);
if ($instance = static::find($id)) {
return hash_equals($instance->token, hash('sha256', $token)) ? $instance : null;
}
}
I don't believe the code above should ever call a query like
select * from `users` where `users`.`id` = ? and `users`.`deleted_at` is null limit 1
so my hunch is that you have customised the PeronsalAccessToken
model and have other queries happening?
I think you would need to put this into a reproduction repo.
Hey there, thanks for reporting this issue.
We'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue as one separate commit on the main/master branch and share the repository here?
Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up.
laravel new bug-report --github="--public"
Do not amend and create a separate commit with your custom changes. After you've posted the repository, we'll try to reproduce the issue.
Thanks!
Closing this issue because it's inactive, already solved, old or not relevant anymore. Feel to open up a new issue if you're still experiencing this.
Laravel Version
11.5.0
PHP Version
8.2.12
Database Driver & Version
No response
Description
laravel sanctum is producing a different user from authenticated user when it's accessed via auth()->guard('sanctum')->user() in the model / without using middleware on the route
Steps To Reproduce
i have a controller which is accessible for both registered user and guests so im not using the auth middleware on the route , but still i need to access the user in my model relation if logged in , so im using
auth()->guard('sanctum')->user()
this is my controller
and this is the relation method in my model
so after not getting the expected results i've outputted the queries and saw it's using a different user id from the authenticated in the relation query
here is the output
it's using user with id of 1 in the query and also in my user output after loading the relation , but my auth token belongs to user with id of 2 , i can even see the query reading
personal_access_tokens
with id of 2 in the first query which belongs tousers.id
of 2also noticed if run the
auth()->guard('sanctum')->user()
before loading the relation it works fine and it will use the correct user id in the relation query , but for some reason when it's called inside the relation function it'll produce a different userso i've updated the code to run auth before relation
and just like that everything works fine and i get user with id of 2 in all queries and outputs