2

I've found out, that my Laravel (5.7) App has a strange behavior in some case where it performs two requests of form submits. Last week everything worked fine.

E.g: There is a registration form. When a user wants to register and submits the form, he fails validation, because the entered email-address already exists (which it doesn't right before he submitted the form). A glance in my database shows me, that the user was created and logged in. So it seems, that the form somehow performs two requests.

This behavior happens on several forms all over the application but not on all. This causes some very bad actions (the one above, emails are sent double, images are uploaded twice etc etc).

One possible problem-causer could be a new Middleware, where I access the current request and perform some actions on it:

class CookiebannerMiddleware
{
    public function handle(Request $request, Closure $next)
    {
        $cookie_name = config('cookiebanner.cookie_key');
        $cookie_value = config('cookiebanner.cookie_value');
        $cookie_lifetime = config('cookiebanner.cookie_lifetime');

        $response = $next($request);

        if(!$request->hasCookie($cookie_name)){
            $response->cookie($cookie_name, $cookie_value, $cookie_lifetime);
            return $response;
        }

        return $next($request);
    }
}

Or are there any other possibilities which could cause such problems?


Update

The problem was my CookiebannerMiddleware. The problem is fixed with the following solution:

class CookiebannerMiddleware
{
    public function handle(Request $request, Closure $next)
    {

        $cookie_name = config('cookiebanner.cookie_key');
        $cookie_value = config('cookiebanner.cookie_value');
        $cookie_lifetime = config('cookiebanner.cookie_lifetime');

        if(!$request->hasCookie($cookie_name)){
            return $next($request)->withCookie($cookie_name, $cookie_value, $cookie_lifetime);
        }

        return $next($request);
    }
}
Brotzka
  • 2,959
  • 4
  • 35
  • 56
  • are you sure its only one request hitting laravel? nothing wrong on the client? you can replicate it locally? *So it seems, that the form somehow performs two requests.* this part make me thinking its someone clicking the form submit button twice. – Bagus Tesa Nov 06 '18 at 10:34
  • This is a problem, I've discovered locally. And I'm 100% that I've clicked the submit button only once ;) – Brotzka Nov 06 '18 at 10:45
  • @Brotzka your network tab (with `Preserve log` enabled) shows only one request? – Travis Britz Nov 06 '18 at 10:49
  • @TravisBritz the problem was in my middleware. I've updated my question. It seems that I've submitted the request twice. I don't know how but the update above fixed the problem. – Brotzka Nov 06 '18 at 10:53

2 Answers2

5

Avoid calling $next($request) twice:

class CookiebannerMiddleware
{
    public function handle(Request $request, Closure $next)
    {
        $cookie_name = config('cookiebanner.cookie_key');
        $cookie_value = config('cookiebanner.cookie_value');
        $cookie_lifetime = config('cookiebanner.cookie_lifetime');

        $response = $next($request);

        if(!$request->hasCookie($cookie_name)){
            $response->cookie($cookie_name, $cookie_value, $cookie_lifetime);
        }

        return $response;
    }
}
Elisha Senoo
  • 3,489
  • 2
  • 22
  • 29
3

you are calling twice

$response = $next($request);

you should do simply

    $response = $next($request);
    if(!$request->hasCookie($cookie_name)){
        $response->cookie($cookie_name, $cookie_value, $cookie_lifetime);
    }
    return $response;
simonecosci
  • 1,194
  • 7
  • 13
  • Yeah, that seems to be the problem. But how can I access the current request and response without performing a second request? – Brotzka Nov 06 '18 at 10:44