0
votes

I have a middleware group of auth inside that i want to apply another middleware to one specific route in that view that is if the profile is not completed user cant go to any other route until he complete his profile and submit.

More Specifically, middle ware is causing loop on redirect because i have 2 middleware.

i created middleware with laravel php artisan and checking the user if profile is incomplete he should redirect to the profile/edit page but its not even working with just check on incomplete empty companyname.

Middlware

class incompleteProfile
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if(empty(Auth::user()->details['companyname'])){
            return redirect()->route('profile');
        }
        return $next($request);
    }
}

Routes File

Routes
Route::group(['middleware'=>['auth'] ], function(){


// User Profile
Route::get('/profile/edit', 'UserController@profile')->name('profile')->middleware('incompleteProfile');
Route::post('/profile/edit', 'UserController@editProfile')->name('editProfile');
2
There is a mistake in your if-statement. It should be if(Auth::user()->details['companyname'] == ""){...}, without the empty(). If companyname is a direct attribute of user you can write if(Auth::user()->companyname == ""){...}Brotzka
Also you have to register your middleware in app/Http/kernel.php (laravel.com/docs/5.5/middleware#registering-middleware)Brotzka
my middle ware is register but when condition match it redirects again and again and browser crash thenHassam Ali
if(empty(Auth::user()->details['companyname']) this thing is now working but it redirects again and again and browser gives this error redirected you too many times.Hassam Ali
Another problem could be, that your edit-page is only reachable via POST. So every redirect goes into Route::get() instead of Route::post(). You should rename your first route (the protected one) to /profile. Your edit-route should be Route::match(['post','get'],'/profile/edit','UserController@editProfile'). In your controller you have to check if current method is post or get and return a view with the edit-form or handle the form-input.Brotzka

2 Answers

3
votes

If you put that middleware on the profile route ... how can they ever get to the profile route to get the form to update the profile to add the missing information?

You are saying ... if user details for company name are empty, then redirect to profile, but you have that middleware on profile ... so its going to forever redirect to profile because the middleware is telling it to. Your user can never get to profile in this case.

This is the equivalent of assigning the auth middleware to the login page. The auth middleware checks if the user is currently authenticated. Which means if a user is not authenticated they would never be able to get to login, in this scenario, as login requires them to be "logged in".

0
votes

lagbox answer pretty much says the logic of why it doesn't work. Try it like this.

Route::group(['middleware'=>['auth'] ], function(){


// User Profile

    Route::get('/profile/edit', 'UserController@profile')->name('profile');

    Route::group(['middleware'=>['incompleteProfile'] ], function(){
      Route::post('/profile/edit', 'UserController@editProfile')->name('editProfile');
      //ETC
    });

});