Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

towhid's avatar

Multi Auth Sytem log out problem

Hi , i am use here two type Auth Admin and User , i use two guards and two providers

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

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

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

  'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],

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

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],


    'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
        ],

        'admins' => [
            'provider' => 'admins',
            'table' => 'password_resets',
            'expire' => 60,
        ],
    ],

i am using built in Auth system , and overright the Another auth guard , this is my admin login controller

    public function __construct()
    {
        $this->middleware('guest:admin')->except('logout');
    }
    /**
     * Show the application's login form.
     *
     * @return \Illuminate\Http\Response
     */
    public function showLoginForm()
    {
        return view('admin.auth.login');
    }

    protected function guard()
    {
        return Auth::guard('admin');
    }

this is my guest -redirectauthenticated middle ware

    public function handle($request, Closure $next, $guard = null)
    {
        switch ($guard) {
            case 'admin':
                if (Auth::guard($guard)->check()) {
                    return redirect('admin/home');
                }
                break;
            default:
                if (Auth::guard($guard)->check()) {
                    return redirect('/home');
                }
                break;
        }

        return $next($request);
    }

this is my handle .php file code

protected function unauthenticated($request, AuthenticationException $exception)
    {
        if ($request->expectsJson()) {
            return response()->json(['error' => 'unauthenticated.'], 401);
        }
        $guard = array_get($exception->guards(), 0);
        switch ($guard) {
            case 'admin':
                $login = 'admin.login';
                break;
            default:
                $login = 'login';
                break;
        }
        return redirect()->guest(route($login));
    }

this is my Route file

Auth::routes();

Route::get('/', function () {
    return view('welcome');
});

Route::get('/home', 'HomeController@index')->name('home');

Route::get('admin/home', 'AdminController@index');

Route::get('admin', 'Admin\LoginController@showLoginForm')->name('admin.login');
Route::post('admin', 'Admin\LoginController@login');

Route::post('admin-password/email', 'Admin\ForgotPasswordController@sendResetLinkEmail')->name('admin.password.email');
Route::get('admin-password/reset', 'Admin\ForgotPasswordController@showLinkRequestForm')->name('admin.password.request');

Route::post('admin-password/reset', 'Admin\ResetPasswordController@reset');
Route::get('admin-password/reset/{token}', 'Admin\ResetPasswordController@showResetForm')->name('admin.password.reset');

this is y app file for layout master page

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- CSRF Token -->
    <meta name="csrf-token" content="{{ csrf_token() }}">

    <title>{{ config('app.name', 'Laravel') }}</title>

    <!-- Scripts -->
    <script src="{{ asset('js/app.js') }}" defer></script>

    <!-- Fonts -->
    <link rel="dns-prefetch" href="//fonts.gstatic.com">
    <link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet">

    <!-- Styles -->
    <link href="{{ asset('css/app.css') }}" rel="stylesheet">
</head>
<body>
    <div id="app">
        <nav class="navbar navbar-expand-md navbar-light bg-white shadow-sm">
            <div class="container">
                <a class="navbar-brand" href="{{ url('/') }}">
                    {{ config('app.name', 'Laravel') }}
                </a>
                <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="{{ __('Toggle navigation') }}">
                    <span class="navbar-toggler-icon"></span>
                </button>

                <div class="collapse navbar-collapse" id="navbarSupportedContent">
                    <!-- Left Side Of Navbar -->
                    <ul class="navbar-nav mr-auto">

                    </ul>

                    <!-- Right Side Of Navbar -->
                    <ul class="navbar-nav ml-auto">
                        <!-- Authentication Links -->
                        @guest
                            <li class="nav-item">
                                <a class="nav-link" href="{{ route('login') }}">{{ __('Login') }}</a>
                            </li>
                            @if (Route::has('register'))
                                <li class="nav-item">
                                    <a class="nav-link" href="{{ route('register') }}">{{ __('Register') }}</a>
                                </li>
                            @endif
                        @else
                            <li class="nav-item dropdown">
                                <a id="navbarDropdown" class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre>
                                    {{ Auth::user()->name }} <span class="caret"></span>
                                </a>

                                <div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdown">
                                    <a class="dropdown-item" href="{{ route('logout') }}"
                                       onclick="event.preventDefault();
                                                     document.getElementById('logout-form').submit();">
                                        {{ __('Logout') }}
                                    </a>

                                    <form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
                                        @csrf
                                    </form>
                                </div>
                            </li>

                        @endguest
                        
                    </ul>
                </div>
            </div>
        </nav>

        <main class="py-4">
            @yield('content')
        </main>
    </div>
</body>
</html>

if i want i can login two user same browser at a time . but

now i want to logout - but i don't understand how to manage built in logout process for my all guard admin and user also

but now problem is when i click log out - its log out all user type , user and admin also ..

i want if i am user panel logout button rout to user logout method and if i am admin panel then only admin logout --

please help me expert Brother thank you

0 likes
15 replies
Miaababikir's avatar

Because you using the same route for logout for users and admin, make new route for the admin logout method and that will solve your problem.


Route::post('admin/logout', 'Admin\LoginController@logout')->name('admin.logout');

And in your blade file you should specify the route you want to use.

towhid's avatar

@miaababikir

but i have same problem still because i create this route as like your instructon

Route::post('admin-logout', 'Admin\LoginController@adminLogout')->name('admin.logout');

and in login controller i add this code

    public function adminLogout(Request $request)
    {
        $this->guard('admin')->logout();

        $request->session()->invalidate();

        return $this->loggedOut($request) ?: redirect('/admin-login');
    }

but now think i ahve two button one is user log out and other is admin log out two route two method

like


<a href="{{route('logout')}}">user log out </a>
<a href="{{route('admin.logout')}}">admin logout </a>

but now show all button i want to view button only guard wise - if logged person user auth then show user logout button if logged user person admin then show admin log out

how to check , i hope you understand my quarry thank you

jlrdw's avatar

Why not just forget that user: https://laravel.com/docs/5.8/session#deleting-data

Also see logout in the API code:

https://github.com/laravel/framework/blob/5.8/src/Illuminate/Foundation/Auth/AuthenticatesUsers.php#L156

ref: https://laravel.com/api/5.8/Illuminate/Foundation/Auth/AuthenticatesUsers.html#method_logout

May require a little custom code on your part.

if i want i can login two user same browser at a time

But from previous conversations, it would be easier to have one Auth system, and not attempt to login more than one at a time in a browser.

towhid's avatar

@jlrdw

Brother do you understand what i want to do ? == please check my last comment about my view things after that then i am not using route url then how and where i do that ?

Miaababikir's avatar

If i understand you correctly, You can use this

@if(Auth::guard('admin')->check())
    <a href="{{route('admin.logout')}}">admin logout </a>
@else
    <a href="{{route('logout')}}">user log out </a>
@endif

I don't know if it's the best way to solve it, but it will work for now.

1 like
Snapey's avatar

you have two users logged in using the same browser?

Presumably when you invalidate the session, you kill both users.

If this is the case, thats a really dumb solution. Why would you allow a user to login twice in the same browser?

jlrdw's avatar

Have a person (admin or user) logged in. Next, if an admin fine show their page, if user, fine show their page.

But

The logged in user can be stored in the same table. That's what the role is for.

User is logged in, now ask yourself:

  • What can this person do
  • what can't this person do

Admin is logged in

Ask the same questions.

Just don't have two logged in at the same time. But one user table should be all you need.

Think like this:

All of the following are users:

  • admin
  • user
  • owner of company
  • chief engineer
  • head scientist
  • accountant
  • bookkeeper
  • etc

All are users.

The question is, once logged in what does their role allow (can do) or dis-allow (cannot do) them to do.

An example:

The head scientist cannot do bookkeeping.

But yes one user per computer at a time will make your life easier.

towhid's avatar

@snapey

If this is the case, thats a really dumb solution. Why would you allow a user to login twice in the same browser? Thanks for your Reply = i am just following one video from you tube - where show how to make multi Auth system , so every thing is ok. login panel multi type . Auth type multi type depend on guard type ... same time browser login in multi panel . now i want to know i have now one Logout button

route('logout')

if i logout then both panel also logout - from browser ..

bu ti want when admin logout from only admin panel and if user want to logout then only logout from user panel .. so i want to know first thing , if i want to show guard wise how to code for check guard type and secondly how to do this by multi type logout system thank you

towhid's avatar

@jlrdw

The logged in user can be stored in the same table. That's what the role is for.

i understand this thing which one you suggested : for single table one extra field where i use role type or admin type .. and i also know how to check for difference role type view section lik =e example

@if(Auth::user()->roleType == 'admin')
<a href="{{route('admin.logout')}}">admin logout </a>
@else
 <a href="{{route('logout')}}">user log out </a>
@endif

but my question is how to logout button show fro check system when i used multi type guard for multi table like i have two type auth guard one is default web and another id my customize Auth guard admin

so to do that please help me out , if its not possible for builtin auth by any overweight system then how to do ? thank you

jlrdw's avatar

Only one user login at a time on the same computer.

It doesn't matter if the logged in user is it regular user or an admin.

If only one is logged in at a time on that particular computer they can only do what they are allowed to do.

Edit: to add I am still confused why you want an admin and a user logged in at the same time on the same computer.

towhid's avatar

@jlrdw

Edit: to add I am still confused why you want an admin and a user logged in at the same time on the same computer.

ok i forget it for same login both in same browser ..

BTW tell one thing i have Two guard one is web another id admin - can any view section show for check guard type ?

@if(Auth::gurad('admin'))
<a href="{{route('button.one')}}">button one </a>
@else
 <a href="{{route('button.two')}}">button two  </a>
@endif

is it work for guard type check process ?

towhid's avatar

OK its work perfectly - my last query is if i want to use two type auth table user and admin , and i want to use two login panel , but if any type auth person logged in no one can browse others auth access , like : i have one user logged in user panel in this time any admin or super admin hit this login page then redirect auto my user panel . please help m ,,,,eeee

Snapey's avatar

study authorization.

Your efforts so far are for authentication which is a different topic

Lolimo's avatar

Oh there is still no way to logout individually?

I have same problem as this thread.

i want to use two login panel

Yea I want to do this too.

The problem is simple. There are many ways to implement multi-auth using "guard" but there is no way to logout individually.

Please or to participate in this conversation.