2016-02-28 4 views
0

Я использую laravel 5.1 и модульный пакет. В мой контроллер я использую следующий метод регистрации:Laravel 5.1 Аутентификация просмотров

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.']); 
} 

Мой маршрут:

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

Route::get('admin/dashboard', [ 
    'middleware' => 'auth', 
    'uses' => '[email protected]' 
]); 
}} 

Мой контроллер:

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

Мои Middleware/Аутентифицировать:

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); 
} 

Это работает и перенаправляет меня в индексный вид после входа в систему. Когда пользователь не вошел в систему, по-прежнему можно получить доступ к представлению индекса, обратившись к URL-адресу: localhost/admin/dashboard.

Как перенаправить пользователя на пользовательскую страницу, которая показывает пользователю ошибку, что невозможно получить доступ к локальному URL-адресу url/admin/dashboard, когда он не вошел в систему?

Любые идеи? Спасибо

ответ

0
The issue is with your route the middleware should be at the top level as soon as you hit the controller it should redirect if not authenticated 

    Route::group(['middleware'=>'auth','module' => 'Admin', 'namespace' => 'App\Modules\Admin\Controllers'], function() 
    { 
     Route::get('admin/dashboard', ['uses' => '[email protected]']); 
    }); 

secondly if you want to redirect user to a custom page you can do this 

public function redirectUnAuthenticateUser() 
{ 

\Session::flash('login_error', 'Kindly login before you can access this page'); 
//create a view that user would be redirected to 
return view('session.unauthenticated') ; 
} 


and the change your auth function to below 

public function handle($request, Closure $next) 
    { 
    if ($this->auth->guest()) { 
     if ($request->ajax()) { 
      return response('Unauthorized.', 401); 
     } else { 
return redirect()->route('you-custom-rout-name-matching-above-function'); 
     } 
    } 

    return $next($request); 
} 

and on your view you can get the value of the flash message