How to redirect to auth/login in Laravel 5.1 if visitor is not logged in?

Asked

Viewed 987 times

2

I am using Laravel 5.1 native authentication but when accessing a protected route the system does not redirect me to the login screen, instead it shows me the message "You don’t have permission to access this Resource".

The routes then this way:

Route::group(['prefix'=>'admin','before' => 'auth','middleware'=>['needsRole:Admin'],'namespace'=>'Admin'], function()
{
    Route::resource('/', 'HomeController');
    Route::resource('users','UsersController');
    Route::resource('stores','StoresController');
    Route::resource('templates','TemplatesController');
});
  • You have to do it by Middleware. Which one are you using ?

  • The routes then like this: http://pastebin.com/EK4JYx7Y

  • All right, but Middleware ? 'auth','middleware'=>['needsRole:Admin']. What’s in that one Middleware ? It will redirect the user.

1 answer

0

I was making a mistake:

['prefix'=>'admin','before' => 'auth','middleware'=>['needsRole:Admin'],'namespace'=>'Admin']

When it really is:

['prefix'=>'admin','middleware'=>['auth','needsRole:Admin'],'namespace'=>'Admin']

Browser other questions tagged

You are not signed in. Login or sign up in order to post.