1
votes

I'm trying simple logout functionality in laravel 5.2 but don't really understand where am I wrong. It would be great is someone can help.

here's Route

Route::get('logout', 'loginController@getLogout');

loginController getLogout method:

public function getLogout()
{
    //$this->auth->logout();
    Session::flush();
    Auth::logout();
    return redirect('/');
}

link in view that uses this function:

<a href="{{url('logout')}}">Logout</a>

session store code:

$request->session()->put('name', $username['name']);

AuthController constructor:

public function __construct()
{
    $this->middleware('guest', ['except' => ['logout', 'getLogout']]);
}

When user clicks on the logout link, it does redirect to root page but doesn't really destroy session or logout. It isn't requiring login to view pages (which it should).

2
Try to put a die("Stop here!"); at the begging of getLogout function to see if you are going though it. I'm not 100% but maybe you need to put '/' in url('/logout').Salvador P.
yes die does work. And without die, it does redirect to '/' (root page) My guess is maybe i"m using session()->put that is why it doesn't remove session with: Session::flush()? Can that be an issue?s develop
According with the doc, flush() removes all data in session. I'm using this route in my laravel apps. Route::get('logout', 'Auth\AuthController@logout'); and normally it works :)Salvador P.

2 Answers

1
votes

I too had the same problem and i have rectified by Method 1 and i had reference using Method 2.

Method 1:

Route::get('auth/logout', 'Auth\AuthController@logout');

Or Method 2: or in AuthController constructor add

public function __construct()
{
    $this->middleware('guest', ['except' => ['logout', 'getLogout']]);
}

Hope so this will clear up your Error. I had the same problem and i did like this alone

Session Destroy must be used like this

Session::forget('name');
$request->session()->flush(); // in your Controller
0
votes

Try to change the route in routes.php with this:

Route::get('logout', 'Auth\AuthController@logout');

And for the logout route I use:

{{ url('/logout') }}

Normally this works, if you need to use a different controller for something especial, try to use the:

$request->session()->flush() 

in the controller. Following the Laravel 5.2 documentation -> https://laravel.com/docs/5.2/session.

Other approximation, try to modify the order in your controller, maybe it will work. According to the doc, Auth:logout() will clean all user auth data, then you can clean the other session data.

public function getLogout()
{
    //$this->auth->logout();
    Auth::logout();
    Session::flush();
    return redirect('/');
}