s develop
s develop

Reputation: 115

Laravel logout not working

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).

Upvotes: 1

Views: 2507

Answers (2)

Salvador P.
Salvador P.

Reputation: 1489

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('/');
}

Upvotes: 0

Naresh Kumar P
Naresh Kumar P

Reputation: 4210

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

Upvotes: 1

Related Questions