-
Notifications
You must be signed in to change notification settings - Fork 24.4k
Add user(): helper function #6582
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 8000 privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 12.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,21 @@ | ||||||||||
<?php | ||||||||||
|
||||||||||
use App\Models\User; | ||||||||||
|
||||||||||
/** | ||||||||||
* Get the currently authenticated user model. | ||||||||||
*/ | ||||||||||
function user(): User | ||||||||||
{ | ||||||||||
$currentUser = auth()->user(); | ||||||||||
|
||||||||||
if ($currentUser === null) { | ||||||||||
throw new Exception('The current user is not authenticated.'); | ||||||||||
} | ||||||||||
Comment on lines
+12
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you want, you could shorten this
Suggested change
|
||||||||||
|
||||||||||
if (! $currentUser instanceof User) { | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As the function has a return type of If an object which is not an instance of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would throw a TypeError you're right, but I made it explicit here so that static analysis tools can tell what's going on Another option would be to docblock the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We already have the user model defined in the config: I’m experiencing the same frustration with PHPStan. I’ll probably implement something like this in my projects, but I feel like a better version should be implemented at the framework level. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Helpful idea, but I think using Also, when calling |
||||||||||
throw new Exception('The currently authenticated user is not an instance of '.User::class); | ||||||||||
} | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This breaks in apps with multiple auth guards. It assumes only User gets authenticated. |
||||||||||
|
||||||||||
return $currentUser; | ||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe, this could be a dedicated
UnauthenticatedException
or the like 🤔