DevMarketer / multiauth_tutorial

156 stars 83 forks source link

Trouble with logging out users #23

Open krasidankov opened 5 years ago

krasidankov commented 5 years ago

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

`<!DOCTYPE html>

{{ config('app.name', 'Laravel') }} ` If someone knows to do it, please post the solution to this problem. Thank you.
juliosouzam commented 5 years ago
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
krasidankov commented 5 years ago

@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.

juliosouzam commented 5 years ago
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
krasidankov commented 5 years ago

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') }}

@endif`

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

juliosouzam commented 5 years ago

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');

BippyMiester commented 4 years ago

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.