Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$request->user() or Auth::user run query each time if token doesn't exists #49201

Closed
mnr73 opened this issue Nov 30, 2023 · 3 comments
Closed
Labels

Comments

@mnr73
Copy link

mnr73 commented Nov 30, 2023

Laravel Version

10.34.2

PHP Version

8.2.12

Database Driver & Version

8.2.0

Description

sanctum version : 3.3.2

Hi, I use Laravel with Sanctum, and I've noticed that the app runs a query each time I use request()->user()
image

I found that if the saved token is incorrect or doesn't exist in the database, the user resolver runs a query each time it is called, and this is not good for performance.

Steps To Reproduce

you can run a fresh Laravel app with sanctum and set wrong token in Bearer token that not exist and use request()->user() for times.

I check app and query log with Clockwork browser extension.

@crynobone crynobone added the bug label Dec 1, 2023
crynobone added a commit that referenced this issue Dec 1, 2023
…en doesn't exists

fixes #49201

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
@mpyw
Copy link
Contributor

mpyw commented Dec 10, 2023

@taylorotwell decided to get along with this behavior on #49205, we won't wix


You should check through Auth::hasUser() after your first call for Auth::user() (available since 5.6)

@driesvints
Copy link
Member

Thanks @mpyw

@mnr73 please see the above.

@mnr73
Copy link
Author

mnr73 commented Dec 11, 2023

thanks. I handled this 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
4 participants