Home > other >  Laravel how to use @auth and @guest with multi authentication
Laravel how to use @auth and @guest with multi authentication

Time:05-09

I am trying to make multi authentication in Laravel project

I will make four tables and every table have its authentication system

1 - Guest

2 - Instructor

3 - Supervisor

4 - Admin

I started with the admin table and created the table and modified the auth.php config file like this

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

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

    'api' => [
        'driver' => 'token',
        'provider' => 'users',
        'hash' => false,
    ]
],

And added this to the providers array

'admins' => [
    'driver' => 'eloquent',
    'model' => App\Models\Admin::class,
]

And this is the AdminController

<?php

namespace App\Http\Controllers;

use App\Models\Admin;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

class AdminController extends Controller
{
    public function signup() {
        return view('auth/admin/signup');
    }

    public function handleSignup(Request $request) {
        $request->validate([
            'account' => 'required|string|max:100|min:3',
            'password' => 'required|string|max:100|min:8',
            'country' => 'required|string|max:100',
            'phone' => 'required|string|max:11',
            'visa' => 'nullable|string|max:19|min:10',
        ]);

        $admin = Admin::create([
            'account' => $request->account,
            'password' => Hash::make($request->password),
            'country' => $request->country,
            'phone' => $request->phone,
            'visa' => $request->visa
        ]);

        Auth::login($admin);

        return redirect(route('home'));
    }

    public function signin() {
        return view('auth/admin/signin');
    }

    public function handleSignin(Request $request) {
        $request->validate([
            'account' => 'required|string|max:100|min:3',
            'password' => 'required|string|max:100|min:8',
        ]);

        $is_login = Auth::guard('admin')->attempt(['account' => $request->account, 'password' => $request->password]);

        if (!$is_login) {
            return redirect(route('auth.admin.signin'));
        }

        return redirect(route('home'));
    }

    public function signout() {
        Auth::guard('admin')->logout();

        return redirect(route('home'));
    }
}

In the blade I need to hide the login and signup buttons for the logged in users and just show the logout so I made this

@guest
    <li >
            <a  href="{{ route('auth.signin') }}">Login</a>
    </li>
    <li >
            <a  href="{{ route('auth.signup') }}">Sign Up</a>
    </li>
@endguest
@auth
    <li >
        <a  href="{{ route('auth.signout') }}">Logout</a>
    </li>
@endauth

But when I login it redirects me the home correctly but I still can see the login and signup buttons and the logout button doesn't show

Can anyone help me with this please?

CodePudding user response:

First of all for example you can check admin guards guess like this;

@guest('admin')
    <li >
            <a  href="{{ route('auth.signin') }}">Login</a>
    </li>
    <li >
            <a  href="{{ route('auth.signup') }}">Sign Up</a>
    </li>
@endguest

But i recommend this one, you can check each guard and include a partial for that guards;

@if (Auth::guard('admin')->check())
  @include('admin.auth-links')
@elseif(Auth::guard('supervisor')->check())
  @include('supervisor.auth-links')
@elseif(Auth::guard('instructor')->check())
  @include('instructor.auth-links')
@else
  @include('auth-links')
@endif

// Logout link can be single endpoint which logouts multiple guards.


  • Related