0
votes

I use Laravel 5.6, I have 2 auth guards :

'user' => [
    'driver' => 'session',
    'provider' => 'users',
],

'member' => [
    'driver' => 'session',
    'provider' => 'members',
],

In my template, I just have a little problem, I want to use @auth('user') for example to load correct view, but when I use @guest, a 'member' is considered as a guest, I explain :

In my blade :

@auth('user')
   // content for user logged
@endauth

@auth('member')
   // content for member logged
@endauth

@guest
   // content for guest only
   // but this content is shown for members logged (in addition of the @auth('member') content
@endguest

I don't know why, when a member is logged, he will see the @guest content and the @auth('member') content. Do I need to configure something else ? I want the same behavior of user auth.

** EDIT **

Ok, with this ugly way, it works :

@guest('member')
   @guest('user')
     // only guest
   @endguest
@endguest
1
its an issue with guards .. you are checking 2 different guards and not explicitly defining what guard guest will use. So the guest call "could" be using neither guard you are checking, which means you will always have this section at the least in this example. sounds like what is currently the default guard does not have a logged in user. there is no general authenticated user or guest, it is always in relation to a guard ... in short, how do you know what guard the guest check is using?lagbox
So, can I use double guard for my guest section ? Or I guess it will be more simple to use a condition if the visitor is not logged (as a member, or as an user)Vincent Decaux
if ... elseif ... else your conditional checks are dependent upon one another not separatelagbox

1 Answers

0
votes

This is an issue with Guards. You are explicitly checking 2 Guards for a current User. You are not requesting the default Guard, but explicitly asking a specific Guard.

The guest check isn't specifying a Guard to use so the default Guard is used. There is no way from this code to know what the default Guard is or if it is even one of the 2 Guards you actually checked.

Putting the Guard issue aside there is an issue with the logic of your conditions. They are not 3 separate conditionals, they are dependent upon one another. It is not if this if this if this, it is if this, elseif this, else.

@auth('user')
    ...
@elseauth('member')
    ...
@else
    ...
@endif

which is:

@if (auth()->guard('user')->check())
    ...
@elseif (auth()->guard('member')->check())
    ...
@else
    ...
@endif

In the context of your example code, you defined what a guest is. You are defining it based on your code as any user who isn't an authenticated user or member. Not what a Guard tells you is a guest. Your 'guest' check isn't @guest it is your own logic.