2016-02-04 54 views
1

當調度方法Auth::attemptAuth::check回報true,然後重定向到Route admin認證Laravel

class LoginController extends Controller { 

    public function login(LoginRequest $request) 
    { 
     if (Auth::attempt($request->only(['login', 'password']))) { 
      var_dump(Auth::check()); // true 
      return redirect()->intended('admin'); 
     } 

     return back(); 
    } 
} 

路線admin有中間件

class AdminMiddleware 
    { 
     public function handle($request, Closure $next) 
     { 
      var_dump(Auth::check()); // false 

      if (!Auth::check()) { 
       return view('login'); 
      } 

      return $next($request); 
     } 
    } 

,然後重定向方法Auth :: check返回false,這是爲什麼發生了什麼?

+0

這可能是一個會話問題,請檢查您的Cookie設置的設置是否正確。 – GiamPy

+0

具體有什麼設置?配置 - >會話? –

+0

確保您的管理中間件在會話初始化後出現在「web」組中間件之後。 – Gal

回答

0

替換爲您的代碼:

use Illuminate\Http\Response; 
use Closure; 
use Auth; 

class AdminMiddleware 
{ 
    public function handle($request, Closure $next) 
    { 
     if (!Auth::check()) { 
      return new Response(view('login')); 
     } 

     return $next($request); 
    } 
} 
+0

不要使用'try this'作爲你的答案。好的答案應該提供關於問題和解決方案的解釋。如果你確定你的答案是正確的,那麼不需要'試試這個',而只是'做這個事情'或'用'替換你的代碼'。如果您不確定,請使用評論而不是答案。 – zajonc

+0

感謝您的建議 –