I'm working on my first laravel project in which I made an admin panel to make admin able to controller the website. I tried to login admin into dashboard and everything was fine, when I tried to log him out nothing works and the page was just reloading and redirect back.
Here is my logout button:
<a class="navbar-brand btn btn-primary btn-lg"
href="{{route('admin.logout'))}}"> log out</a>
My route:
Route::get('logout', 'LoginController#logout')->name('admin.logout');
And my controller:
use Illuminate\support\Facades\Auth;
public function logout(Request $request){
Auth::logout();
return redirect()->route('get.admin.login')
->with(['success'=>'logged out successfully']);
}
I used Auth::logout(); method from laravel official documentation, however it didn't work.
I hope I can find help.
as your using Auth::logout()inside that function
public function logout(Request $request) {
Auth::logout();
return redirect('/login');
}
here Auth:: will be null
so apply middleware to get Auth instance and then you can logout
Route::get('logout', 'LoginController#logout')->name('admin.logout')->middleware('auth');
You can use the following in your controller:
return redirect('login')->with(Auth::logout());
You can use below code, for logout the admin.
Auth::logout();
return redirect('/login');
I'm trying to flush the session then redirect the user to the "activate" view on logout, but it keeps taking me to "login" instead. Here's what I've tried in my web.php routes file:
Route::get('/logout', function () {
Session::flush();
Auth::logout();
return Redirect::to('activate');
});
How do I flush the cache and take users to the "activate" page on logout?
Change your route url, or except it from AuthController constructor :
public function __construct()
{
$this->middleware('auth', ['except' => ['logout']]);
}
I use Laravel 5.3 and I have the following problem.
[UPDATE]
My initial trouble was the appearance of an error when performing actions on the site when the user was not logged in the system.
This happened when the browser is started, where cached information is displayed by default on the page. Site interface displayed for logged users, and in his system was not. At the same time, producing some action, I get an error that the user is not authorized.
I also have group auth middleware for all my routes. When I reboot page of the site, the middleware is activated and redirectedme to the login page. The main problem is the browser shows the cached information.
So, in addition to middleware for routes I decided to make auth check in controllers.
[/UPDATE]
I want to check user's auth in every controller's action. Making the auth check in every controllers' action manually isn't a solution, because there are many controllers and actions.
So I decided to make it globally.
As all controllers extends Main Controller (App\Http\Controllers\Controller.php), I decided write the
auth()->check() in constructor:
function __construct()
{
if(auth()->check()) dd('success');
}
But... nothing happened((( Then I found the callAction method in BaseController which Main Controller extends and made checking here:
public function callAction($method, $parameters)
{
if(auth()->check()) dd('success');
return call_user_func_array([$this, $method], $parameters);
}
This time everything's OK, but I don't like this solution, because editing the core files isn't good.
Finally, I redeclared callAction method in Main Controller with auth checking, but I don't like this way too.
Is any solution?
You should use middleware:
Route::get('profile', ['middleware' => 'auth', 'uses' => 'UserController#showProfile']);
Or:
Route::get('profile', 'UserController#show')->middleware('auth');
Or using middleware groups:
Route::group(['middleware' => ['auth']], function () {
// Controllers here.
});
Or using controller's construct:
public function __construct()
{
$this->middleware('auth');
}
You can use auth middleware in your controller
public function __construct()
{
$this->middleware('auth');
}
check here : https://laravel.com/docs/5.3/authentication
if there is a group of routes this would be the easiest way
Route::group(['middleware' => ['auth']], function()
{
// here all of the routes that requires auth to be checked like this
Route::resource('user','UsersController');
}
another ways
function __construct()
{
$this->middleware('auth');
}
another way is specified on controller routes
Route::get('profile', [
'middleware' => 'auth',
'uses' => 'UserController#showProfile'
]);
see documentation
https://laravel.com/docs/5.0/controllers#controller-middleware
I use laravel 5.1 with original setup. If I login with user1's id, and then click back button to the login page, and login again with user2's id, I still get user1's content. Try refreshing the page after login user2, but still get user1's content.
Then I override function
public function postLogin(\Illuminate\Http\Request $request)
and added below line:
Auth::logout();
Is there anyway to fix this?
After digging into the code, I found the problem was in the middleware. Two modifications were made to AuthController to fix the problem:
first, change below code
public function __construct()
{
$this->middleware('guest', ['except' => 'getLogout']);
}
to
public function __construct()
{
$this->middleware('guest', ['except' => ['getLogout', 'postLogin']]);
}
second, add \Auth::logout() at the beginning of function postLogin(), then it will work!
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?
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)
Try this..
Put In following code "class AuthController extends Controller"
public function getLogout()
{
$this->auth->logout();
Session::flush();
return redirect('/');
}
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
app/Http/routes.php
Route::get('auth/logout', 'Auth\AuthController#getLogout');
app/Http/Controllers/Auth/AuthController.php
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']);
}
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);
}
}
For logout in laravel 5.6 and later version :
use in your view page:
Logout
use this in your web.php
Route::get('logout', 'Auth\LoginController#logout');
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.
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
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?
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.
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 "/"
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.
In 5.4 this worked for me...
Logout
<form id="logout-form" action="/logout" method="POST" style="display: none;">{{ csrf_field() }}</form>
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.
It is a quite old thread but finally I found a simple trick to log the user out from the server:
return Auth::logout();