Search code examples
laravelsession-cookies

419 - Session Expired without event authentication after deployment | Laravel 5.7


My Laravel application works fine in on my local machine and on my staging server. But when I deployed to my production server the login form is not working. Every time I try to login to the admin panel it shows my

419

Sorry, your session has expired. Please refresh and try again.

My Head Tag contains <meta name="csrf-token" content="XXX">

My login form contains <input type="hidden" name="_token" value="XXX">

What have I tried so far:

  • Tried generating new key php artisan key:generate
  • Tried clearing all caches
  • php artisan cache:clear
  • php artisan route:clear
  • php artisan view:clear
  • php artisan cache:clear
  • Checked php.ini max_execution_time and memory limits.
  • Tried changing SESSION_DRIVER from file to database

The only thing that kind of worked but was unsafe and was for debugging purpose

In the VerifyCsrfToken class, I added "api/login" & "api/register" in protected $except and the form worked. Like this


namespace FleetCart\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;

class VerifyCsrfToken extends BaseVerifier
{
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        "api/login",
        "api/register"
    ];
}

Please help me understand how to resolve this.

Thanks in advance.


Solution

  • Finally after spending 3 days I found the solution.

    @Snapey helped me fixing this issue.

    You can see the answer here: https://laracasts.com/discuss/channels/laravel/419-session-expired-without-event-authentication-after-deployment-laravel-57

    In my case, there was an extra space before starting <?php tag.