2017-10-16 33 views
0

我需要保護他們免於觀看。這意味着如果沒有登錄它不應該重定向到任何其他頁面,如果有人試圖訪問,它應該回到登錄頁面。我該如何保護我的laravel認證

我使用Laravel 5.4和定期認證

php artisan make:auth 

而且在登錄控制器

protected $redirectTo = '/home'; 

而在中間件RedirectifAuthenticated我想是這樣,但它無法正常工作。

public function handle($request, Closure $next, $guard = null) 
{ 
    if (Auth::guard($guard)->check()) { 
     return redirect('/home'); 
     return redirect('/leadsadd'); 
     return redirect('/leadslist'); 
     return redirect('/opporadd'); 
     return redirect('/opporlist'); 
     return redirect('/accadd'); 
     return redirect('/acclist'); 
     return redirect('/selftask'); 
} 

    return $next($request); 

} 

這不起作用。我認爲我所做的方法是錯誤的。任何人都可以幫助我如何防止它,不應該重定向到任何路線或URL。 路線

Route::get('/', function() { 
return view('auth.login'); 
}); 

Auth::routes(); 
Route::get('/home', '[email protected]')->name('home'); 

Route::get('leadsadd','[email protected]'); 
Route::get('leadslist', '[email protected]'); 
Route::any('leadview/{id}','[email protected]'); 
Route::get('leadedit/{id}','[email protected]'); 

在此先感謝。

+3

你如何重定向到8次不同的頁面?每個請求只能重定向一次。如果用戶登錄,該函數將始終重定向到'/ home'。所有其他返回碼都是無法訪問的。 – Jerodev

+0

沒有不喜歡,我告訴我們將有像上面指定的(localhost:8000 \ add,localhost:8000 \ view,localhost:8000 \ home)不同的路線。只有當我試圖訪問本地主機:8000 \ home它重定向到登錄頁面。但其他人直接打開沒有任何登錄請求。 – Hemanth

+1

這應該使用中間件在您的路線文件中定義。你能顯示你的'routes/web.php'的內容嗎? – Jerodev

回答

3

您應該group your routesauth中間件添加到它。如果任何未經過身份驗證的用戶試圖訪問任何這些頁面,則此中間件將自動重定向到登錄頁面。

Route::get('/home', '[email protected]')->name('home'); 

Route::middleware(['auth'])->group(function() { 
    Route::get('leadsadd','[email protected]'); 
    Route::get('leadslist', '[email protected]'); 
    Route::any('leadview/{id}','[email protected]'); 
    Route::get('leadedit/{id}','[email protected]'); 
}); 
+0

非常感謝它幫助我 – Hemanth

0

有如下措施來保護您的航線Laravel 5.4

一種方式兩種方式與途徑附上web.php文件中的身份驗證的中間件

Route::get('profile', function() { 
    // Only authenticated users may enter... 
})->middleware('auth'); 

另一種方式是附加處理您的路由的控制器的構造函數中的中間件。

public function __construct() 
{ 
    $this->middleware('auth'); 
} 
1

你需要做到這一點在你的路由這樣

Route::group(['middleware' => 'auth'], function() { 
     // all authenticated users will have the access here! 
     ]]); 

和你RedirectIfAuthenticated.php應恢復到原來的

public function handle($request, Closure $next, $guard = null) 
    { 
     if (Auth::guard($guard)->check()) { 
      return redirect('/yourdashboardorwhatever'); 
     } 

     return $next($request); 
    }