Make Session Expiration Redirect Back To Login?

When user logs in and is authenticated, I use Auth::user()->username; to show username of user on dashboard. However, for some reason when session expires the class Auth doesn't seem to work and dashboard page throws error as trying to get property of non-object for Auth::user()->username;. How can I redirect the user back to the login page when he clicks any link or refreshes the page after the session has expired?

I tried the Authenticate.php middleware but it always redirects back to login page,whatever you put the credentials either correct or incorrect.However,when I don't use this middleware it logins the user.Am I missing something?



| Application Routes
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.

Actions Handled By Resource Controller

Verb        Path                    Action      Route Name
GET         /photo                  index       photo.index
GET         /photo/create           create      photo.create
POST        /photo                  store
GET         /photo/{photo}          show
GET         /photo/{photo}/edit     edit        photo.edit
PUT/PATCH   /photo/{photo}          update      photo.update
DELETE      /photo/{photo}          destroy     photo.destroy

Adding Additional Routes To Resource Controllers

If it becomes necessary to add additional routes to a resource controller beyond the default resource routes, you should define those routes before your call to Route::resource:

Route::get('photos/popular', '[email protected]');

Route::resource('photos', 'PhotoController');


// Display all SQL executed in Eloquent
// Event::listen('illuminate.query', function($query)
// {
//     var_dump($query);
// });


Route::group(['namespace' => ADMIN], function () {
    Route::get('/','[email protected]'); 

////////////////////////////////////Routes for backend///////////////////////////////////////////////////
Route::group(['prefix' => ADMIN,'middleware' => 'auth'], function () {
    Route::group(['namespace' => ADMIN], function () {
    //Route::get('/','[email protected]');

        //sumit routes for user registration
        Route::get('/users/destroy/{id}','[email protected]');
        Route::get('UserProf','[email protected]');
        Route::get('users','[email protected]');
        Route::get('/users/create','[email protected]');
        Route::get('/users/adminEdit/{id}','[email protected]');
        Route::post('/users/adminUpdate','[email protected]');
        Route::post('/users/store','[email protected]');
        Route::get('/users/edit/{id}','[email protected]');
        Route::post('/users/update/{id}','[email protected]');

        //airlines route
        Route::get('airlines','[email protected]');
        Route::get('/airlines/create','[email protected]');
        Route::post('/airlines/store','[email protected]');
        Route::get('/airlines/edit/{id}','[email protected]');
        Route::post('/airlines/update','[email protected]');
        Route::get('/airlines/destroy/{id}','[email protected]');
        //end sumit routes

        //flight routes
        Route::get('flights','[email protected]');
        Route::get('showFlightBook','[email protected]');
        Route::get('flights/create','[email protected]');
        Route::post('flights/store','[email protected]');
        Route::get('flights/book','[email protected]');
        Route::get('flights/edit/{id}','[email protected]');
        Route::post('flights/update','[email protected]');
        Route::get('flights/destroy/{id}','[email protected]');


        //hotels route
        Route::get('hotels','H[email protected]');
        Route::get('/hotels/create','[email protected]');
        Route::post('/hotels/store','[email protected]');
        Route::get('/hotels/edit/{id}','[email protected]');
        Route::post('/hotels/update','[email protected]');
        Route::get('/hotels/destroy/{id}','[email protected]');
        //end sumit routes

        //book-hotel routes
        Route::get('hotel-book','[email protected]');
        Route::get('showHotelBook','[email protected]');
        Route::get('hotel-book/create','[email protected]');
        Route::post('hotel-book/store','[email protected]');
        Route::get('hotel-book/book','[email protected]');
        Route::get('hotel-book/edit/{id}','[email protected]');
        Route::post('hotel-book/update','[email protected]');
        Route::get('hotel-book/destroy/{id}','[email protected]');

        //close flight routes

        //for admin login
        //Route::get('initlogin','[email protected]');
        Route::get('login','[email protected]');
        // Route::get('privilegeLogin','[email protected]');
        // Route::post('privilegeCheck','[email protected]');
        Route::post('login','[email protected]');
        Route::get('/dashboard','[email protected]');
        Route::get('logout','[email protected]');
        //user login 
        //Route::get('userLogin','[email protected]');
        //Route::post('userLogin','[email protected]');
        Route::get('/userDashboard','[email protected]');
        Route::get('Logout','[email protected]');
        //password reset
        Route::get('forget-pass','[email protected]');
        //Route::get('home', '[email protected]');


    'auth' => 'Auth\AuthController',
    'password' => 'Auth\PasswordController',


    <?php namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Auth\Guard;

class Authenticate {

     * The Guard implementation.
     * @var Guard
    protected $auth;

     * Create a new filter instance.
     * @param  Guard  $auth
     * @return void
    public function __construct(Guard $auth)
        $this->auth = $auth;

     * Handle an incoming request.
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
    public function handle($request, Closure $next)
        if ($this->auth->guest())
            if ($request->ajax())
                return response('Unauthorized.', 401);
                // return redirect()->guest('auth/login');
                return redirect()->guest('/');

        return $next($request);



If you want a middleware to be run during every HTTP request to your application, simply list the middleware class in the $middleware property of your app/Http/Kernel.php class. So, to protect every route from being accessed without authentication do this

protected $middleware = [
        'App\Http\Middleware\Authenticate',// add this line according to your namespace

it will redirect the user if not logged in. UPDATE Keep in mind that adding auth middleware as global will create redirect loop so avoid it.

Or if you want specific routes to be protected then attach the middleware auth to that route

Route::get('admin/profile', ['middleware' => 'auth', function () {

I think you are not attaching the auth middleware to your routes.