skip to Main Content

Currently have a classified site built using Laravel,

Laravel Framework 7.20.0

This is my routes code

Route::get('logout', 'AuthLoginController@logout');

this my logout method.

public function logout(Request $request)
    {
        // Get the current Country
        if (session()->has('country_code')) {
            $countryCode = session('country_code');
        }
        if (session()->has('allowMeFromReferrer')) {
            $allowMeFromReferrer = session('allowMeFromReferrer');
        }
        
        // Remove all session vars
        $this->guard()->logout();
        $request->session()->flush();
        $request->session()->regenerate();
        
        // Retrieve the current Country
        if (isset($countryCode) && !empty($countryCode)) {
            session(['country_code' => $countryCode]);
        }
        if (isset($allowMeFromReferrer) && !empty($allowMeFromReferrer)) {
            session(['allowMeFromReferrer' => $allowMeFromReferrer]);
        }
        
        $message = t('You have been logged out') . ' ' . t('See you soon');
        flash($message)->success();
        
        return redirect(property_exists($this, 'redirectAfterLogout') ? $this->redirectAfterLogout : '/');
    }

And finally my logout code

<a href="{{ lurl(trans('routes.logout')) }}" class="nav-link">
                                    <i class="icon-logout hidden-sm"></i> {{ t('log_out') }}
                                </a>

When clicking the logout button the header when logged in the page refreshes but no message is displayed and the session doesn’t seem to be deleted. Sometimes it does logout but when visiting the user account section it’s still logged in. I have tried setting the session to the following settings, Cookie, File and Redis. Currently using Redis for file cache. And have tried a POST method which does not work either.

Redis server v=4.0.9 sha=00000000:0 malloc=jemalloc-3.6.0 bits=64 build=9435c3c2879311f3

There is no JS errors in my page either and all packages are up to date.

2

Answers


  1. On your LoginController add this :

    public function __construct()
    {
        $this->middleware('guest', ['except' => ['logout']]);
    }
    
    Login or Signup to reply.
  2. Try this

    public function __construct()
    {
        Auth::logout();
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search