Ad

Check User's Each Request After User Logged Into The With Conditions In Laravel 6.18

- 1 answer

Ok let me explain my requirement shortly. I have a portal where suppliers can register and add their content to our site. Usually, registration has 5 steps where after the supplier completes the first registration step then the user will be logged into the system and also it also update the registration_step column in the DB table. After the supplier completes his 1st step he can log out and then once he logged in again then the user will not be able to go another URL till he completes all steps in registration. SO I have tried to accomplish this with middleware like below. Please note there are 3 different types of users in this system.

 class CheckUserRegistrationStep
{
    public function handle($request, Closure $next)
    {
        $supplier = Auth::guard('supplier');
        if ($supplier->check()) {
            $supplierRegistrationStep = Supplier::findOrFail($supplier->user()->id)->registration_step;
            if ($supplierRegistrationStep == 2) {
                return redirect()->route('suppliers.registration-step02-otp-verification');
            } else if ($supplierRegistrationStep == 3) {
                return redirect()->route('suppliers.registration-step03-nature-of-business');
            } else if ($supplierRegistrationStep == 4) {
                return redirect()->route('suppliers.registration-step04-categories-and-locations');
            } else if ($supplierRegistrationStep == 5) {
                return $next($request);
            }
        } else {
            return back();
        }
    }
}

But the problem is that when I use this middleware in the controller then it gives me the error too many redirects. I know this error occurs because the user gets redirected to the same URL again and again. So I have tried with so many methods to accomplish my requirement but still no luck.

Simply I'm trying to stop the user get navigated to other URLs till he completes the registration and I want to redirect the user for the place he stops the registration step.

Update

An error that occurs after modifying the code @CleanCode 's answer

ErrorException

Trying to get property 'headers' of non-object

This is the controller

 public function __construct()
{
    $this->middleware('auth:supplier', ['except' => ['step01', 'step01Store']]);
 }
Ad

Answer

Your'e getting redirected because it's checking the condition and redirecting to the same route but you don't check if the current route is not the redirecting route. You can add another check to the condition to check if the route isn't the redirecting route.

class CheckUserRegistrationStep
{
    public function handle($request, Closure $next)
    {
        $supplier = Auth::guard('supplier');
        if ($supplier->check()) {
            $supplierRegistrationStep = Supplier::findOrFail($supplier->user()->id)->registration_step;
            if ($supplierRegistrationStep == 2 && !$request->route()->named('suppliers.registration-step02-otp-verification')) {
                return redirect()->route('suppliers.registration-step02-otp-verification');
            } else if ($supplierRegistrationStep == 3 && !$request->route()->named('suppliers.registration-step03-nature-of-business')) {
                return redirect()->route('suppliers.registration-step03-nature-of-business');
            } else if ($supplierRegistrationStep == 4 && !$request->route()->named('suppliers.registration-step04-categories-and-locations')) {
                return redirect()->route('suppliers.registration-step04-categories-and-locations');
            } else if ($supplierRegistrationStep == 5) {
                return $next($request);
            }
        } else {
            return back();
        }
    }
}
Ad
source: stackoverflow.com
Ad