Hm, can you explain a little more how your global scope works and why is it global and not a local scope for the users or profile model?
Ignore Global scopes for Auth
Hi,
I'm building a website where users are able to make their profile private or public. I order to achieve this I applied a global scope that filters users who set their profile to private.
This causes problems when authenticating and when using auth()->user() as users will not be found even though they exist but are private.
Is a custom User Provider / Guard necessary or is there a simpler way?
Thanks
Well, you could implement a user provider that extends the base EloquentUserProvider and then override the methods you need to remove that global scope.
class CustomUserProvider extends EloquentUserProvider
{
public function retrieveById($identifier)
{
return $this->createModel()->newQuery()->withoutGlobalScopes()->find($identifier);
}
}
I only did one as an example, but that is the basic idea. To activate it for use you have to take a little trip to the AuthServiceProvider.
Auth::provider('custom', function ($app, $config) {
return new CustomUserProvider($app['hash'], $config['model']);
});
Then you have to change your auth config.
'providers' => [
'users' => [
'driver' => 'custom',
'model' => App\User::class,
],
]
Please or to participate in this conversation.