8000 Add user(): helper function by imliam · Pull Request #6582 · laravel/laravel · GitHub
[go: up one dir, main page]

Skip to content

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

Draft
wants to merge 3 commits into
base: 12.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions bootstrap/helpers.php
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.');

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 🤔

}
Comment on lines +12 to +14

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want, you could shorten this

Suggested change
if ($currentUser === null) {
throw new Exception('The current user is not authenticated.');
}
throw_if($currentUser === null, Exception::class, 'The current user is not authenticated.');


if (! $currentUser instanceof User) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the function has a return type of App\Models\User this check is unnecessary.

If an object which is not an instance of App\Models\User is returned, PHP will error out automatically.

Copy link
Author
@imliam imliam Mar 18, 2025

Choose a reason for hiding this comment

The 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 $currentUser variable for static analysis and allow the TypeError to be thrown at runtime

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have the user model defined in the config: auth.providers.users.model.
It should use that.

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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Helpful idea, but I think using App\Models\User directly could be limiting. In many cases, the authenticated user isn't always a User instance — it can be any Authenticatable implementation, defined via auth.providers.users.model and possibly located in a different namespace.

Also, when calling auth()->user() (or this helper if added), developers typically expect an Authenticatable, not specifically a User model. This might lead to confusion or unexpected type errors in custom setups.

throw new Exception('The currently authenticated user is not an instance of '.User::class);
}

Choose a reason for hiding this comment

The 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;
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
},
"files": ["bootstrap/helpers.php"]
},
"autoload-dev": {
"psr-4": {
Expand Down
0