我正在制作此身份验证过滤器,但当它到达Reguest::intended()
时,它会将我重新发送回主页。
示例:
尝试访问forum.dev/admin
- >强迫我登录 - >密码和用户名是正确的 - >返回forum.dev/
,转到forum.dev/admin
我该如何解决这个问题?
Route::filter('auth.role', function()
{
// Get Current logged in user
$user = Auth::user();
// Check if the user is logged in
if( Auth::check() && $user->role >= 2 )
{
// User is admin, let him through!
return Redirect::intended();
}
else
{
// User is not logged in
return Redirect::to('/login')->withErrors("You need to be logged in or admin!");
}
});
答案 0 :(得分:2)
下面怎么样?在未经身份验证的用户上将Redirec::to
替换为Redirect::guest
。 Redirect::guest
会在会话中保存目标网址,而Redirect ::打算可以在以后使用它。
Route::filter('auth.role', function()
{
// Get Current logged in user
$user = Auth::user();
// Check if the user is logged in
if( Auth::check() && $user->role >= 2 )
{
// User is admin, let him through!
return Redirect::intended();
}
else
{
// User is not logged in
return Redirect::guest('/login')->withErrors("You need to be logged in or admin!");
}
});
答案 1 :(得分:1)
Redirect::intended()
检查会话是否有预期路由,然后重定向到该路由(如果存在),否则重定向到回退网址。
尝试以下方法:
Route::filter('auth.role', function()
{
// Get Current logged in user
$user = Auth::user();
// Check if the user is logged in
if( Auth::check() && $user->role >= 2 )
{
// User is admin, let him through!
return Redirect::intended('/'); // not required, but best practice to use a fallback URL
}
else
{
if ( ! Session::has('url.intended')) {
Session::flash('url.intended', URL::current());
}
// User is not logged in
return Redirect::to('/login')->withErrors("You need to be logged in or admin!");
}
});
据我所知,Auth::check()
默认情况下不会将预期的网址保留在会话中。但是,Auth::guest()
会自动将预期的URL放入会话中。