Reputation: 4152
When I use the built-in Authentication and try to log the user out at /auth/logout
- it does not work as hoped. It appears to keep the user logged in. But when I clear my browser cache, I can see that is has actually logged the user out.
I don't get any errors on the page nor errors in the log file.
I am guessing that Session::flush()
at the logout method would possibly solve this - but I don't know where to put it.. Can someone point me in the right direction?
Upvotes: 22
Views: 59096
Reputation: 614
in case no any solutions is working try this
for multiple custom guards if you use auth()->logout
then it wont work
just use auth('your-guard-name')->logout();
then it will work fine.
Upvotes: 0
Reputation: 3105
For logout in laravel 5.6 and later version :
use in your view page:
<a href="{{ url('logout') }}">Logout</a>
use this in your web.php
Route::get('logout', 'Auth\LoginController@logout');
Upvotes: 3
Reputation: 1457
In 5.4 this worked for me...
<a href="#" onclick="event.preventDefault(); document.getElementById('logout-form').submit();">Logout</a>
<form id="logout-form" action="/logout" method="POST" style="display: none;">{{ csrf_field() }}</form>
Upvotes: 0
Reputation: 362
It is a quite old thread but finally I found a simple trick to log the user out from the server:
return Auth::logout();
Upvotes: -1
Reputation: 1512
I had the same problem after upgrading to Laravel 5.3. To fix it, I noticed that the traits used in
App\Http\Controllers\Auth\AuthController
are outdated, so I changed the line
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
into
use AuthenticatesUsers
;
and also the line in the constructor
$this->middleware($this->guestMiddleware(), ['except' => 'logout']);
into
$this->middleware('guest', ['except' => ['logout', 'getLogout']]);
Then the method App\Http\Controllers\Auth\AuthController@logout
started to work properly.
Upvotes: 0
Reputation: 496
this ocurrs because the middleware is called for every route. you can add a exception to " logout route" in App\Http\Middleware\RedirectIfAuthenticated.php
class RedirectIfAuthenticated
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
if (!$request->is('/logout') && Auth::guard($guard)->check()) {
return redirect('/home');
}
return $next($request);
}
}
Upvotes: 4
Reputation: 31
Laravel 5.2 the url is a little different ...
Use this
php artisan make:auth
This will generate the routes for auth and some templates for login e register...
Be careful to use this with existing projects, it can make changes to your code
Upvotes: 2
Reputation: 521
Solution is very very simple
in Http->Middleware->Authenticate.php change "login" in else statement to "/"
return redirect()->guest('/');
and define following route in routes.php
Route::get('/', function () {
return view('login');
});
for logout call following function: public function getlogout(){ \Auth::logout(); return redirect('/home'); } this is important redirect to "/home" instead of "/" that first calls $this->middleware('auth'); and then in middleware redirect to "/"
Upvotes: 0
Reputation: 299
I had the same problem. The problem was actually a simple little error in the configuration of the route and controller.
You see the route actually specifies a method of getLogout
and the controller exception is looking for logout
.
The only thing you need to do is change the exception in the controller. No need for any additional methods. The getLogout
method already exists and works perfectly.
Here is the actual code
Route::get('auth/logout', 'Auth\AuthController@getLogout');
public function __construct()
{
$this->middleware($this->guestMiddleware(), ['except' => 'logout']);
}
The _construct
method should look like that:
public function __construct()
{
$this->middleware($this->guestMiddleware(), ['except' => 'getLogout']);
}
Upvotes: 8
Reputation: 1616
For anyone that has problems solving it with the accepted solution: I started with Laravel 5.1 and updated to 5.2. The following fix worked for me:
Try changing your 'logout' route to
Route::get('auth/logout', 'Auth\AuthController@logout');
or in AuthController constructor add
public function __construct()
{
$this->middleware('guest', ['except' => ['logout', 'getLogout']]);
}
Taken from: https://stackoverflow.com/a/34667356/1275778 (also check the other answers there if you're still having problems afterwards)
Upvotes: 60
Reputation: 1650
I had the same problem with updated laravel 5.2. I have used laravel's auth controller and I solved this problem using like,
/logout
instead of /auth/logout
same for /register
and /login
in instead of using /auth/register
and /auth/login
.
Upvotes: 2
Reputation: 7
Not enough on browsers that recover your tabs after crash (Chrome doesn't delete session cookies) . Also, after redirect a new session is created. Solution: in AuthController, as mentioned above, in getLogout, set a variable and pass it to redirect:
$data['logout'] = true;
return redirect('/')->with('data',$data);
In your home view do this:
@if(session()->has('data') && session('data')['logout'])
{{session_unset()}}
{{setcookie('laravel_session', "", -1, "/")}}
@endif
I believe Laravel redirect reinitialises Session. So after redirect, in view, reset delete cookie. Anybody can comment on this? Is this the right reason this works?
Upvotes: 1
Reputation: 5444
Try this..
Put In following code "class AuthController extends Controller"
public function getLogout()
{
$this->auth->logout();
Session::flush();
return redirect('/');
}
Upvotes: 13
Reputation: 2527
To log out a user with Laravel using the built in authentication tools, it is as simple as using Auth::logout();
.
Please also check the various session settings in config/session.php
if the sessions behaves unpredictably.
Upvotes: 0