How to fix Laravel logging you out automatically for no reason

Recently, we were working on one of our project developed in Laravel 5.2.29 and all of the sudden Laravel logging out automatically. It started logging us out randomly, not with exact pattern. We started troubleshooting this problem and tried different solutions which are already on internet but this didn’t help solving our problem.

Fixed: Laravel logging out automatically

One thing for sure which causes this issue, are Laravel sessions management.

Solution 1:

Step 1

First things first, changing your session driver from file to database will solve your issue. Go to config/session.php file and change driver to database.

<?php

use Illuminate\Support\Str;

return [

    /*
    |--------------------------------------------------------------------------
    | Default Session Driver
    |--------------------------------------------------------------------------
    |
    | This option controls the default session "driver" that will be used on
    | requests. By default, we will use the lightweight native driver but
    | you may specify any of the other wonderful drivers provided here.
    |
    | Supported: "file", "cookie", "database", "apc",
    |            "memcached", "redis", "dynamodb", "array"
    |
    */

    'driver' => 'database',

OR you will need to make changes in .env file


BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DRIVER=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=database
SESSION_LIFETIME=120

Step 2:

Now we will create a session migration for sessions table by running following commands

php artisan session:table

Step 3:

After creating migration file for sessions table, we will migrate with following command which will create sessions table in database.

php artisan migrate

Next you need to flush Laravel configuration and cache

php artisan config:cache

Hopefully this will solve your issue. If not, then we will move to next solution. We have full article on How to store laravel sessions in database?

Solution 2:

User from your Laravel applications might be logging out automatically because of short time for sessions. By changing ‘lifetime’ => env(‘SESSION_LIFETIME’, 120), to bigger value will solve this issue. Either make these changes in .env file or config/session.php file.

In .env file

SESSION_LIFETIME=3600 

OR in config/session.php file make following changes

/*
    |--------------------------------------------------------------------------
    | Session Lifetime
    |--------------------------------------------------------------------------
    |
    | Here you may specify the number of minutes that you wish the session
    | to be allowed to remain idle before it expires. If you want them
    | to immediately expire on the browser closing, set that option.
    |
    */

    'lifetime' => 3600,

Making above changes and flushing configuration and cache of Laravel application will solve you issue of automatically logging you out.

php artisan config:cache

Solution 3:

If above two solution doesn’t solve your issue, then you need to check your sessions table in database. If number of records are growing very fast, it means your Laravel application is dealing with huge data in sessions which are crossing server limits. To solve this, you will know on which particular page your application is logging you out. I will explain this with a code example.

Lets say we have a route

Route::get('/overview-report', 'overviewController@getData')->name('overview');

Above route points to getData() method in overviewController controller. In this controller we process data from database and return data to overview.blade.php file. But we are setting sessions and saving process data in sessions. This will grow your sessions table records very quickly.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use App;

class overviewController extends Controller
{	

   public getData(){
     session(
            [
             'finalData'        =>  $this->finalResults, // thousands of records
             'total'            =>  $this->total,
             'customCheckbox'   => array('tagName'),
             'customTimezone'   => $this->custom_timezone,
             'rps'              =>  $this->showRpsFlag
            ]
        );

    return view('overview-report');

  }

As you can see i mentioned thousands of records saving in session variable finalData, will create entries in session table. So on next request, which will return user to overview-report.blade.php file, Laravel will try to process all sessions. With too many sessions, Laravel mix all sessions in which we have a session for logged in user as well and thus throw us to login page.

Solution to laravel logging out automatically:

If we change to store process data, which are thousands of records, in an array or variable and pass it to overview.blade.php file as i am doing in the following code. These changes will reduce number of session entries in sessions table in database. Now, Laravel application has to process very less session entries on next request.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use App;

class overviewController extends Controller
{	

   public getData(){

    return view('overview-report')
            ->with(
                [
                    'tableHeaders'  =>  $this->tableHeaders,
                    'finalData'     =>  $this->finalResults,
                    'total'         =>  $this->total,
                    'formParams'    =>  $this->formParams
                ]
            );

  }

Summary:

Logging out from laravel applications always involve sessions. Either the sessions are not saving or there are many sessions saving into filesystem, where Laravel mix up too many session reqeusts. Moving sessions into database helps fixing this issue. If you have any particular issue regarding Laravel sessions or anything in general, do let us in below comment box.

Cheers <3