Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trouble with logging out users #23

Open
krasidankov opened this issue Sep 8, 2018 · 6 comments
Open

Trouble with logging out users #23

krasidankov opened this issue Sep 8, 2018 · 6 comments

Comments

@krasidankov
Copy link

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">
{{ config('app.name', 'Laravel') }}
			<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>
						<li class="nav-item"><a class="nav-link" href="{{ route('register') }}">{{ __('Register') }}</a></li>
					@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>
								Hello {{ Auth::user()->first_name }} <span class="caret"></span>
							</a>

							<div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdown">
								@if (Auth::guard('admin'))
										
								@endif
								<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>
							</div>
						</li>
					@endguest
				</ul>
			</div>
		</div>
	</nav>
		 
	<main class="py-4">
		@yield('content')
	</main>
</div>
`

If someone knows to do it, please post the solution to this problem. Thank you.

@juliosouzam
Copy link

juliosouzam commented Sep 8, 2018

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
Copy link
Author

@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
Copy link

juliosouzam commented Sep 9, 2018

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
Copy link
Author

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?

@juliosouzam
Copy link

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
Copy link

In order to logout a specific user or admin, this is already setup in the commit 558bc66.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants