what kind of error occurred?
Can't log out in laravel 5.6
I created a custom login which works fine but the logout functionality isnt working. I created a getLogout action in the LoginController and called the Auth::logout() func.
I did
{{ Auth::check() ? "Logged In" : "Logged Out" }} {{ $id = Auth::id() }}
It printed Logged In and the user Id correctly which proves that the login func is working. Now i av a dropdown link for logout. And i passed my named route('logout') to its href but its not working cos i am still on the dashboard and its still printing user data
like this?
<a href="{{ route('logout') }}" onclick="event.preventDefault();
document.getElementById('logout-form').submit();">
Logout
</a>
did you assign auth check on your route controller?
@Ishatanjeeb how do you mean..assign auth check on route controller...give me an example.
Please post the route definition, your controller code, and the relevant view code for your logout link, it'll be easier to find what's wrong.
@rin4ik your solution does not work. it is not loggin out.
use AuthenticatesUsers;
protected $redirectTo = '/';
protected function authenticated(Request $request, $user)
{ return redirect('/'); }
public function __construct()
{
$this->middleware('guest')->except('logout');
}
public function getLogin()
{
return view('auth.login');
}
public function getLogout()
{
return Auth::logout();
}
public function login(Request $request)
{
$this->validate($request, [
'email' => 'required',
'password' => 'required',
]);
//Store Email field Value
$loginValue = $request->input('email');
//Get Login Type
$login_type = $this->getLoginType($loginValue);
//Change request type based on user input
$request->merge([
$login_type => $loginValue
]);
//Check Credentials and redirect
if (\Auth::attempt($request->only($login_type, 'password'))) {
return redirect()->intended($this->redirectPath());
}
return redirect()->back()->withInput()->withErrors([ 'email' => "These credentials do not match our records." ]);
}
public function getLoginType($loginValue) {
return filter_var($loginValue, FILTER_VALIDATE_EMAIL ) ? 'email'
: ( (preg_match('%^(?:(?:\(?(?:00|\+)([1-4]\d\d|[1-9]\d?)\)?)?[\-\.\ \\/]?)?((?:\(?\d{1,}\)?[\-\.\ \\/]?){0,})(?:[\-\.\ \\/]?(?:#|ext\.?|extension|x)[\-\.\ \\/]?(\d+))?$%i', $loginValue)) ? 'mobile' : 'username' );
} ``
``Route::get('auth/register',[ 'uses' => 'Auth\RegisterController@getRegister', 'as' => 'registra' ]);
Route::post('auth/register',[ 'uses' => 'Auth\RegisterController@register' ]);
Route::get('auth/login',[ 'uses' => 'Auth\LoginController@getLogin' ]);
Route::post('auth/login',[ 'uses' => 'Auth\LoginController@login', 'as'=> 'logger' ]);
Route::get('auth/logout',[ 'uses' => 'Auth\LoginController@getLogout', 'as' => 'logout' ]); `
``
`Route::get('auth/register',[ 'uses' => 'Auth\RegisterController@getRegister', 'as' => 'registra' ]);
Route::post('auth/register',[ 'uses' => 'Auth\RegisterController@register' ]);
Route::get('auth/login',[ 'uses' => 'Auth\LoginController@getLogin' ]);
Route::post('auth/login',[ 'uses' => 'Auth\LoginController@login', 'as'=> 'logger' ]);
Route::get('auth/logout',[ 'uses' => 'Auth\LoginController@getLogout', 'as' => 'logout' ]); ``
public function getLogout()
{
\Auth::logout();
return redirect('/');
}
@rin4ik not working. still getting logged in message and the user id printed on the home page
what error you got? look at network tab
Uncaught TypeError: Cannot read property 'submit' of null at HTMLAnchorElement.onclick
use this it will work view
<li>
<a href="route('logout')">
Logout
</a>
</li>
method
public function getLogout()
{
\Auth::logout();
return redirect('/');
}
it redirects to /logout And generates the
Sorry, the page you are looking for could not be found.
make sure use it inside of auth
@auth
<li>
<a href="route('logout')">
Logout
</a>
</li>
@endauth
Still logged in...but not redirecting this time. I checked the response on network.. found 302 but it is still the printing the Auth::check and user id on the page and i cannot access /auth/register and /auth/login because user is still logged in
@bbmattieu9 ok use it like this
<li>
<a href="auth/logout">
Logout
</a>
</li>
@bbmattieu9 or like this
<li>
<a href="{{ route('logout') }}">
Logout
</a>
</li>
Still the same result as before
@bbmattieu9 it should work . save it like above and hard refresh your browser ctrl+f5.
your method now this?
public function getLogout()
{
\Auth::logout();
return redirect('/');
}
Could you add a dd() to your getLogout controller code just to make sure you're actually hitting it?
function getLogout()
{
dd(Auth::logout());
return redirect('/');
}
Still the same. Nothing changed
By "nothing changed" do you mean you just got redirected like normal? dd() should stop the execution of your script, so at best you should have gotten a blank page.
If you still got redirected, you're not actually hitting that controller method at all. The redirect is probably happening in middleware.
EDIT: went back through your code, you have the guest middleware applied to all methods in your LoginController except logout, but the method you're using is getLogout. Add that to the middleware's exception list.
@bbmattieu9 please show your view , your route for logout and method for it . all steps you've done
@36864 i did
` public function __construct() { $this->middleware('guest')->except('getLogout'); }
``
It redirected me to http://127.0.0.1:8000/auth/logout
with the message null
Route::get('auth/logout',[ 'uses' => 'Auth\LoginController@getLogout', 'as' => 'logout' ]);
It redirected me to http://127.0.0.1:8000/auth/logout with the message null
That means you finally hit your controller code. dd() stops the execution of your script after printing out whatever you pass into it, in this case Auth::logout() which returns null.
Remove the dd() from your controller code now and it should work.
``public function getLogout() {
dd(\Auth::logout());
return redirect('/');
}``
@bbmattieu9 remove dd
\Auth::logout();
return redirect('/');
Please or to participate in this conversation.