As the title states, the content inside the guest tags is shown, even though the user is authenticated.
I want to display in the Navbar a dashboard route when the standard user is logged in, and a specific logout button when the "portal" user is logged in.
I got two guards in my auth.php file (One is the standard, portal is my custom):
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'portal' => [
'driver' => 'session',
'provider' => 'portals'
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
'portal' => [
'driver' => 'eloquent',
'model' => App\Models\Portal::class,
],
Here is the code with the @auth tags in my app.blade.php:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Finanzraketen</title>
<link rel="stylesheet" href="{{ asset('css/app.css') }}">
<style>
.hintergrund{
background-image: url('/background.jpeg');
background-size: cover;
}
</style>
</head>
<body class="hintergrund">
<!-- Navbar goes here -->
<nav class="bg-white shadow-lg bg-gradient-to-r from-green-400 to-blue-500 mb-10">
<div class="max-w-full mx-auto px-4">
<div class="flex justify-between">
<div class="flex space-x-7">
<div>
<!-- Website Logo -->
<a href="#" class="flex items-center py-4 px-2">
<img src="/rakete-2.png" alt="Logo" class="h-8 w-8 mr-2">
<span class="font-semibold text-black text-lg">Finanzraketen</span>
</a>
</div>
<!-- Primary Navbar items -->
<div class="hidden md:flex items-center space-x-3 ">
<ul class="flex items-center pl-10 grid grid-cols-6 gap-10">
<a class="py-2 px-2 font-medium text-black rounded shadow transition
duration-500 ease-in-out transform hover:-translate-y-1 hover:scale-100" href="{{ route('home') }}" class="p-3">Home</a>
<a class="py-2 px-2 font-medium text-black rounded shadow transition
duration-500 ease-in-out transform hover:-translate-y-1 hover:scale-100" href="{{ route('overview') }}" class="p-3">Stellenanzeigen</a>
@auth('web')
<a href="{{ route('dashboard') }}" class="col-start-3 py-2 px-2 font-medium text-black rounded shadow transition
duration-500 ease-in-out transform hover:-translate-y-1 hover:scale-100">Dashboard</a>
<a href="" class="col-start-4 py-2 px-2 font-medium text-black rounded shadow transition border-gray-500
duration-500 ease-in-out transform hover:-translate-y-1 hover:scale-100">{{ auth()->user()->email }}</a>
<form action="{{ route('logout') }}" method="post" >
@csrf
<button type="submit" class="col-start-5 py-2 px-2 font-medium text-black rounded shadow transition
duration-500 ease-in-out transform hover:-translate-y-1 hover:scale-100">Logout</button>
</form>
@endauth
@auth('portal')
<form action="{{ route('portal_logout') }}" method="post">
@csrf
<button type="submit" class="col-start-5 py-2 px-2 font-medium text-black rounded shadow transition
duration-500 ease-in-out transform hover:-translate-y-1 hover:scale-100">Portal Logout</button>
</form>
@endauth
@guest
<a class="py-2 px-2 font-medium text-black rounded shadow transition
duration-500 ease-in-out transform hover:-translate-y-1 hover:scale-100" href="{{ route('login') }}" class="p-3">Mitarbeiter Login</a>
<a class="py-2 px-2 font-medium text-black rounded shadow transition
duration-500 ease-in-out transform hover:-translate-y-1 hover:scale-100" href="{{ route('register') }}" class="p-3">Registrieren</a>
<a class="py-2 px-2 font-medium text-black rounded shadow transition
duration-500 ease-in-out transform hover:-translate-y-1 hover:scale-100" href="{{ route('portal_login') }}" class="p-3">Portal Login</a>
<a class="py-2 px-2 font-medium text-black rounded shadow transition
duration-500 ease-in-out transform hover:-translate-y-1 hover:scale-100" href="{{ route('portal_register') }}" class="p-3">Portal Registrieren</a>
@endguest
</ul>
</div>
</div>
<!-- Mobile menu button -->
<div class="md:hidden flex items-center">
<button class="outline-none mobile-menu-button">
<svg class=" w-6 h-6 text-black hover:text-green-400"
x-show="!showMenu"
fill="none"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
viewBox="0 0 24 24"
stroke="currentColor"
>
<path d="M4 6h16M4 12h16M4 18h16"></path>
</svg>
</button>
</div>
</div>
</div>
<!-- mobile menu -->
<div class="hidden mobile-menu">
<ul class="">
<li> <a class="block text-sm px-2 py-4 hover:bg-green-500 transition duration-300" href="{{ route('home') }}">Home</a></li>
<li> <a class="block text-sm px-2 py-4 hover:bg-green-500 transition duration-300" href="{{ route('overview') }}">Stellenanzeigen</a> </li>
@auth()
<li>
<a href="{{ route('dashboard') }}" class="block text-sm px-2 py-4 hover:bg-green-500 transition duration-300">Dashboard</a>
</li>
<li>
<a href="" class="block text-sm px-2 py-4 hover:bg-green-500 transition duration-300">{{ auth()->user()->email }}</a>
</li>
<li>
<form action="{{ route('logout') }}" method="post" class="block text-sm px-2 py-4 hover:bg-green-500 transition duration-300">
@csrf
<button type="submit">Logout</button>
</form>
</li>
@endauth
@guest
<li>
<a class="block text-sm px-2 py-4 hover:bg-green-500 transition duration-300" href="{{ route('login') }}" class="p-3">Mitarbeiter Login</a>
</li>
<li>
<a class="block text-sm px-2 py-4 hover:bg-green-500 transition duration-300" href="{{ route('register') }}" class="p-3">Registrieren</a>
</li>
<li>
<a class="block text-sm px-2 py-4 hover:bg-green-500 transition duration-300" href="{{ route('portal_register') }}" class="p-3">Bewerbungsportal Login</a>
</li>
@endguest
</ul>
</div>
<script>
const btn = document.querySelector("button.mobile-menu-button");
const menu = document.querySelector(".mobile-menu");
btn.addEventListener("click", () => {
menu.classList.toggle("hidden");
});
</script>
</nav>
@yield('content')
</body>
</html>
Here is my web.php file:
Route::get('home', function () {
return view('home');
})->name('home');
Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
Route::post('/dashboard', [DashboardController::class, 'storePost']);
Route::post('/logout', [LogoutController::class, 'store'])->name('logout');
Route::get('/login', [LoginController::class, 'index'])->name('login');
Route::post('/login', [LoginController::class, 'login']);
Route::get('/register', [RegisterController::class, 'index'])->name('register');
Route::post('/register', [RegisterController::class, 'store']);
Route::get('/posts', [PostController::class, 'index'])->name('posts');
Route::get('/index', [PostController::class, 'index'])->name('overview');
Route::get('/portal/register', [PortalRegController::class, 'index'])->name('portal_register');
Route::post('/portal/register', [PortalRegController::class, 'store']);
Route::get('/portal/login', [PortalLoginController::class, 'showLoginForm'])->name('portal_login');
Route::post('/portal/login', [PortalLoginController::class, 'login'])->name('portal_login');
Route::post('/portal/logout', [PortalLogoutController::class, 'store'])->name('portal_logout');
I also tried implementing it the "ugly" way with @if(Auth::guard('portal')->check()
but it didn't change anything.
I am not sure whether you need Controller code too, but if so I will show it to you of course, just don't want to make the post unnecessarily larger.
Edit: The @auth('web')
works totally fine!
CodePudding user response:
If you need to check authentication state of non-default guard, you should pass guard name to directive, like @guest('portal')
, see here - https://laravel.com/docs/8.x/blade#authentication-directives