Reputation: 181
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');
Upvotes: 0
Views: 1547
Reputation: 2523
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
});
});
Upvotes: 0
Reputation: 50481
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".
Upvotes: 3