Search code examples
phplaravelauthenticationroutesmiddleware

Laravel adding custom middleware to Route::auth();


So I have a login area which was generated with artisan make:auth this has created the views & controllers correctly and has added Route::auth(); to my routes.php.. all is good, but now I want to restrict the login to a list of IPs.

I have created an IPRestrictions middleware and have referenced in Kernel.php

Middleware code:

namespace App\Http\Middleware;

use Closure;

class IPRestrictions {

    public function handle($request, Closure $next) {

        // Allowed IPs
        $allowed = ['xxx.xxx.xxx.xxx', 'xxx.xxx.xxx.xxx', 'xxx.xxx.xxx.xxx'];

        // Get the IP address
        $ip = $request->ip();

        // Check if the ip is valid or if allowed
        if (!filter_var($ip, FILTER_VALIDATE_IP) || !in_array($ip, $allowed)) return abort(404);

        return $next($request);
    }
}

Kernel:

'restrict-ips' => \App\Http\Middleware\IPRestrictions::class

I have tried applying this middleware to the routes like so:

Route::group(['middleware' => 'restrict-ips'], function() {
    Route::auth();
});

This works on my local virtual machine but as soon as its on the live server I receive the error:

Maximum function nesting level of '100' reached, aborting!

I have found workarounds by increasing the xdebug.max_nesting_level but this doesn't resolve the issue in the code.

Any ideas? Thanks.


Solution

  • Add below lines in your bootstrap/autoload.php file :

    ini_set('xdebug.max_nesting_level', 200);