Roman
Roman

Reputation: 598

Protect routes with middleware Laravel

I have implemented middleware roles and permissions control in my app, but I cannot figure out why it only allows me to define one '/' route. The second one is still pointing to '/home' even though I override AuthController redirectTo variable.

My routes:

Route::group(['middleware' => 'role:user'], function()
{
 Route::get('/', 'ScoresController@user');

});

Route::group(['middleware' => 'role:admin'], function()
{
Route::get('/', 'PagesController@home');
});

In any case after authentication user with user role redirecting to '/home'.

Upvotes: 0

Views: 747

Answers (1)

Shnoo
Shnoo

Reputation: 48

Like Simon says your second route will override the first one what you could do is load another controller wich redirects you to another page via redirect() or write it as a route itself.

Could look like this:

Route::get('/', function(){
    $user = Auth::user();

    if($user->is('admin') {
        return redirect('admin');
    }else{
        return redirect('user');
    }
});

Route::get('admin', ['middleware' => 'role:admin', 'uses'=> 'PagesController@home']);

This is just one of many possibilities hope it helps you.

Upvotes: 1

Related Questions