multiauth_tutorial icon indicating copy to clipboard operation
multiauth_tutorial copied to clipboard

Trouble with logging out users

Open krasidankov opened this issue 7 years ago • 6 comments
trafficstars

Hello. I have trouble logging out different types of users. Here is the code for the nav:

`

<!-- 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="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet" type="text/css">

<!-- Styles -->
<link href="{{ asset('css/app.css') }}" rel="stylesheet">

krasidankov avatar Sep 08 '18 09:09 krasidankov

Try with this.

@if (Auth::guard('admin')->check())
    <a class="dropdown-item" href="{{ route('user.logout') }}" onclick="event.preventDefault();document.getElementById('logout-form').submit();">
        {{ __('Logout') }}
    `</a>`

    <form id="logout-form" action="{{ route('user.logout') }}" method="GET" style="display: none;">
        @csrf
    </form>
@endif

juliosouzam avatar Sep 08 '18 18:09 juliosouzam

@juliosouzam Yes, this works but my point is that when a user is currently in the "/home" page I want them to logout only as users, and when is logged as admin and visits "/admin" to show the logout button and only logout as admin on that page.

krasidankov avatar Sep 09 '18 16:09 krasidankov

I use this in component
it's worked for me.

@if (auth()->guard('admin')->check())
    <a class="dropdown-item" href="{{ route('admin.logout') }}" onclick="event.preventDefault();document.getElementById('admin-logout-form').submit();">
        <i class="fa fa-fw fa-sign-out"></i>
        {{ __('Logout') }}
    </a>
    <form id="admin-logout-form" action="{{ route('admin.logout') }}" method="POST" style="display: none;">
        @csrf
    </form>
@endif
@if(auth()->guard('web')->check())
    <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>
@endif

juliosouzam avatar Sep 09 '18 21:09 juliosouzam

In my situation it shows two buttons for a logging out with they go to it's define location. But here again I have a problem with your code. When I click on the logout for the admin it shows me this error.

screenshot 103

I have modified your code a little bit for the users logout. As this code is logging me out from all types of users, I have just added

@if(auth()->guard('web')->check()) <a class="dropdown-item" href="{{ route('user.logout') }}" onclick="event.preventDefault(); document.getElementById('logout-form').submit();"> {{ __('Logout') }} </a> <form id="logout-form" action="{{ route('user.logout') }}" method="POST" style="display: none;"> @csrf </form> @endif

But again it shows the same error for the user logout. What does this error mean?

krasidankov avatar Sep 12 '18 09:09 krasidankov

This error means what you tried to send a request with different method http. How method are in route? Route should be: Route::post('/admin/logout', 'AdminLoginController@logout')->name('admin.logout');

juliosouzam avatar Sep 14 '18 13:09 juliosouzam

In order to logout a specific user or admin, this is already setup in the commit https://github.com/DevMarketer/multiauth_tutorial/commit/558bc66d783841c724cc556679fa9218f184c3f2.

To summarize, in the AdminLoginController.php file, we are defining the function logout() which takes the admin guard and then logs out the admin.

public function logout()
    {
        Auth::guard('admin')->logout();
        return redirect('/');
    }

In the case of the user, let's look at the LoginController.php file. Now remember, we're not using the logout() function as previously defined by Laravel. We have created a brand new logout method called userLogout(). This method takes the web guard and attempts to log the user out.

public function userLogout()
    {
        Auth::guard('web')->logout();
        return redirect('/');
    }

Make sure that you have defined your routes correctly for the user guard. Route::get('/users/logout', 'Auth\LoginController@userLogout')->name('user.logout'); Then setup an anchor tag that points to the user.logout route. No need for a POST request to log out the user. This is a simple GET request. Same thing applies for the Admin Logout (admin.logout) route as well.

BippyMiester avatar Oct 15 '19 02:10 BippyMiester