-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
[5.6] Add ability to determine if the current user is ALREADY authenticated without triggering side effects #24518
[5.6] Add ability to determine if the current user is ALREADY authenticated without triggering side effects #24518
Conversation
It behave similarly to `GuardHelpers::check()`, but it doesn't trigger any side effects.
I not sure you can rely on the |
@browner12 I know. In some environment running driver specific code through Please tell me if you have the proper name for this method. (e.g. |
I would rather just say something like |
@taylorotwell Nice class Post extends Model
{
protected $visible = ['id', 'title', 'body', 'created_at', 'likes', 'liked'];
protected $with = ['likeByCurrentUser'];
protected $appends = ['liked'];
public function likes()
{
return $this->hasMany(Like::class);
}
public function likeByCurrentUser()
{
return $this->hasOne(Like::class)->where('user_id', Auth::hasUser() ? Auth::id() : null);
}
public function getLikedAttribute()
{
return Auth::hasUser() ? ! is_null($this->likeByCurrentUser) : null;
}
} |
I guess I'm a little confused. If your local environment throws exceptions when trying to authenticate over HTTP with your auth API, how does this change help you? Wouldn't |
@browner12 In this example, fetching class Post extends Model
{
protected $visible = ['id', 'title', 'body', 'created_at', 'likes', 'liked'];
protected $with = ['likeByCurrentUser'];
protected $appends = ['liked'];
public function likes()
{
return $this->hasMany(Like::class);
}
public function likeByCurrentUser()
{
return $this->hasOne(Like::class)->where('user_id', Auth::id());
}
public function getLikedAttribute()
{
return ! is_null($this->likeByCurrentUser);
}
} class PostContrller extends Controller
{
public function index()
{
// Always fails in my local environment
return Post::all();
}
} Note that this is just an example. Generally I think loading or formatting models should not trigger side effects except accessing database to fetch their rows. |
@taylorotwell Should missing public methods such as |
@mpyw next major release will be 5.7. Laravel isn't following SemVer. |
- laravel#24518 in this PR was added method `hasUser` to the GuardHelper trait. As for me it will be useful to have this method in the contract.
* [9.x] Add `hasUser` method to Guard contract - #24518 in this PR was added method `hasUser` to the GuardHelper trait. As for me it will be useful to have this method in the contract. * Update Guard.php Co-authored-by: Tetiana Blindaruk <t.blindaruk@gmail.com> Co-authored-by: Taylor Otwell <taylor@laravel.com>
* [9.x] Add `hasUser` method to Guard contract - laravel/framework#24518 in this PR was added method `hasUser` to the GuardHelper trait. As for me it will be useful to have this method in the contract. * Update Guard.php Co-authored-by: Tetiana Blindaruk <t.blindaruk@gmail.com> Co-authored-by: Taylor Otwell <taylor@laravel.com>
Auth::check()
internally callsAuth::user()
. It may trigger side effects.(e.g. accessing database or making an HTTP request for external Web API)
Auth::guard()->user
is protected.We currently do not have an ability to determine if
Auth::guard()->user
is already set without using reflection. This PR addsAuth::alreadyAuthenticated()
which returns boolean value.It will be useful in models or custom relations.
Example