amitshahc's avatar

Laravel blade `@auth` condition not working as expected

I am trying to pick the master blade template dynamically as per the current user roll logged in. (here it should go to the 'shopowner' auth block)

@auth('shopmanager')
@extends('theme::Admins.shopmanager.layout.master')
@endauth

@auth('shopowner')
@extends('theme::Admins.shopowner.layout.master')
@endauth

but this always gives error as it tries to compile the 'shopmanager' master template. It is not going into the 'shopmanager' @auth block because it's not printing anything if I print inside that block.

It only works if I completely comment that line.

P.S.: i tried using this syntax too.. but same results.

if (Auth::guard('shopmanager')->check())
...

also echo the condition check which works perfect without @extends(...) syntax. This goes in shopowner which is right.

@if(Auth::guard('shopmanager')->user())
   {{dd(Auth::guard('shopmanager')->user())}}
@else
   {{dd(Auth::guard('shopowner')->user())}}
@endif
0 likes
7 replies
amitshahc's avatar

@BOBBYBOUWMANN - inside my auth.php

  'guards' => [
        'superadmin' => [
            'driver' => 'session',
            'provider' => 'superadmins',
        ],

        'shopowner' => [
            'driver' => 'session',
            'provider' => 'shopowners',
        ],

        'shopadmin' => [
            'driver' => 'session',
            'provider' => 'shopadmins',
        ],

        'shopmanager' => [
            'driver' => 'session',
            'provider' => 'shopmanagers',
        ],
        
        'admin' => [
            'driver' => 'session',
            'provider' => 'admins',
        ],

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

        
    ],
Snapey's avatar

so the logged in user could be NEITHER shopowner or shopmanager and you would see the behaviour you describe.

HOW have you validated that your multiple guards are being applied correctly?

amitshahc's avatar

@SNAPEY - I didn't get your point. but as per my above code block to verify current login in user and print dd() it is printing correct user i.e. 'shopowner' who is logged in.

Snapey's avatar

Correct user ? Guards are not for specific user. They are for different security for types of user.

How have you changed login to make use of multiple guards?

amitshahc's avatar

@SNAPEY - "hesto/multi-auth" is the plugin i am using.

Routegroup for shopmanger

Route::group(['middleware' => ['web', 'auth:shopmanager', 'shopmanagerVerified'], 'prefix' => 'shopmanager', 'namespace' => 'Modules\ShopManager\Http\Controllers'],
    function () {
    ...
    }

Routegroup for shopowner

Route::group([
    'middleware' => ['web', 'auth:shopowner', 'shopmanagerVerified'],
    'prefix'     => 'shop',
    'namespace'  => 'Modules\ShopManager\Http\Controllers',
],
    function () {
    ...
    }
Snapey's avatar

"hesto/multi-auth" is the plugin i am using.

useful to know

Please or to participate in this conversation.