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

bbmattieu9's avatar

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.

0 likes
41 replies
bbmattieu9's avatar

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

rin4ik's avatar

like this?

<a href="{{ route('logout') }}" onclick="event.preventDefault();
document.getElementById('logout-form').submit();">
                                Logout
</a>
36864's avatar

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.

bbmattieu9's avatar

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

``

  • My Account Posts Another action Something Logout
  • `

    bbmattieu9's avatar

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

    rin4ik's avatar
    public function getLogout()
    {
        \Auth::logout();
        return redirect('/');
    }
    
    bbmattieu9's avatar

    @rin4ik not working. still getting logged in message and the user id printed on the home page

    rin4ik's avatar

    what error you got? look at network tab

    bbmattieu9's avatar

    Uncaught TypeError: Cannot read property 'submit' of null at HTMLAnchorElement.onclick

    rin4ik's avatar

    use this it will work view

    <li>
    <a href="route('logout')">
        Logout
    </a>
    </li>
    

    method

    public function getLogout()
    {
        \Auth::logout();
        return redirect('/');
    }
    
    bbmattieu9's avatar

    it redirects to /logout And generates the

    Sorry, the page you are looking for could not be found.

    rin4ik's avatar
    make sure use it inside of auth
    @auth 
    <li>
    <a href="route('logout')">
        Logout
    </a>
    </li>
    @endauth
    
    bbmattieu9's avatar

    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

    rin4ik's avatar

    @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('/');
    }
    
    36864's avatar

    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('/');
    }
    
    36864's avatar
    36864
    Best Answer
    Level 13

    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.

    rin4ik's avatar

    @bbmattieu9 please show your view , your route for logout and method for it . all steps you've done

    bbmattieu9's avatar

    Route::get('auth/logout',[ 'uses' => 'Auth\LoginController@getLogout', 'as' => 'logout' ]);

    36864's avatar

    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.

    bbmattieu9's avatar

    ``public function getLogout() {

        dd(\Auth::logout());
        return redirect('/');
    
    }``
    
    Next

    Please or to participate in this conversation.