dimanche 28 février 2016

Laravel 5.1 Authentication views

I'm using laravel 5.1 and the modular package. In my controller I use the following login method:

public function postLogin(Request $request)
{   
   $email = $request->input('email');
   $password = $request->input('password');

   if (Auth::attempt(['email' => $email, 'password' => $password])) {
       return redirect()->intended('admin/dashboard');
   }

   return redirect('/login')->withErrors([
   'email' => 'These credentials do not match our records.']);
}

My route:

Route::group(array('module' => 'Admin', 'namespace' => 'App\Modules\Admin\Controllers'), function() {

Route::get('admin/dashboard', [
    'middleware' => 'auth',
    'uses' => 'AdminController@index'
]);
}}

My controller:

public function index()
{
    return view("Admin::index");
}

My Middleware/Authenticate:

public function handle($request, Closure $next)
  {
     if ($this->auth->guest()) {
         if ($request->ajax()) {
             return response('Unauthorized.', 401);
         } else {
             return redirect()->guest('auth/login');
         }
     }

     return $next($request);
 }

This works and redirects me to the index view after login. When the user is not logged in, it is still possible to access the index view by accessing the url: localhost/admin/dashboard.

How can I redirect the user to a custom page which shows an error to the user that it is not possible to access the url localhost/admin/dashboard when he is not logged in?

Any ideas? Thank you



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire