Closed mohammadgh1370 closed 1 month ago
Thank you for reporting this issue!
As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.
If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.
Thank you!
@mohammadgh1370 Please post your entire ProductController
, as you're setting a repository
property on it that we're not able to see which may be the source of your issue, as well as the repository itself. This is likely being set in your controllers constructor, which may be calling Auth::id()
before authentication has actually been done on your application.
Also, your missing several steps in your "Steps to Reproduce", such as models, migrations, etc. Can you please create a replication repository with everything set up so it's immediately reproducible for us?
Hey there,
We're closing this issue because it's inactive, already solved, old, or not relevant anymore. Feel free to open up a new issue if you're still experiencing this problem.
Sanctum Version
3.3.3
Laravel Version
9.52.16
PHP Version
8.1.0
Database Driver & Version
No response
Description
Route Definition I have a route defined as follows:
This route does not have the auth middleware applied, but an authentication token is sent.
Controller The controller method is as follows:
Relation in Model The log relation is defined in the model as follows:
I am using Auth::id() to get the current authenticated user's data. However, Auth::id() is not working correctly and always returns the user with ID 1 from the users table.
Investigation in Guard.php Upon investigation, I found that in the Guard.php class, it retrieves the tokenable relation like this:
The query generated is as follows:
Fix
I made the following change in the PersonalAccessToken model within the findToken method, which resolved the issue:
Instead of this:
I used:
The resulting query was:
This change fixed the issue, and now Auth::id() returns the correct user ID.
Steps To Reproduce
Steps To Reproduce
laravel new bug-report
.routes/web.php
:ProductController
with the following method:log
relationship in the model as follows:/show
route with an authentication token.Auth::id()
returns the user with ID 1 regardless of the actual authenticated user.