Ray*_*Ray 7 authentication laravel cartalyst-sentry
我正在尝试正确地在我的应用程序中设置哨兵包.
我可以登录和退出用户并保护路线,但我似乎无法redirect::intended
正常工作.我的理解是,在被定向到登录页面之前,用户将被带回他们最初呼叫的路由.目前它只是保持重定向到默认页面.
在我的routes.php中,我设置了以下组:
Route::group(array('before' => 'sentryAuth'), function () {...}
Run Code Online (Sandbox Code Playgroud)
在这个小组中,我放置了所有受保护的路线.
在我的filters.php中,我有以下过滤器:
Route::filter('sentryAuth', function () {
if (!Sentry::check()) {
return Redirect::route('login');
}
});
Run Code Online (Sandbox Code Playgroud)
Route :: filter('sentryGuest',function(){
if (Sentry::check()) {
return Redirect::intended('dashboard');
}
});
Run Code Online (Sandbox Code Playgroud)
在我的userController中,我有以下代码:
public function postAuthenticate()
{
try {
// Set login credentials
$credentials = array(
'email' => Input::get('email'),
'password' => Input::get('password')
);
// Try to authenticate the user
$user = Sentry::authenticate($credentials, false);
} catch (Cartalyst\Sentry\Users\LoginRequiredException $e) {
echo 'Login field is required.';
}
catch (Cartalyst\Sentry\Users\PasswordRequiredException $e) {
echo 'Password field is required.';
}
catch (Cartalyst\Sentry\Users\UserNotFoundException $e) {
echo 'User was not found.';
}
catch (Cartalyst\Sentry\Users\WrongPasswordException $e) {
echo 'Wrong password, try again.';
}
catch (Cartalyst\Sentry\Users\UserNotActivatedException $e) {
echo 'User is not activated.';
}
if (!Sentry::check()) {
return Redirect::to('user/login');
} else {
return Redirect::intended('dashboard');
}
}
Run Code Online (Sandbox Code Playgroud)
我试图在没有登录的情况下访问页面'预订/创建'.我被带到登录页面,登录但是它带我到仪表板而不是预订/创建.
我错过了什么吗?我需要额外的代码才能使其工作吗?
Tom*_*Tom 10
在您的filters.php中,请务必使用:
return Redirect::guest('login');
Run Code Online (Sandbox Code Playgroud)
代替
return Redirect::route('login');
Run Code Online (Sandbox Code Playgroud)
guest函数将为expected()设置正确的会话变量以使其正常工作.
我不确定这一点,因为我没有将Sentry用于我当前的项目..所以这只是一种预感.
看来既然你在laravel 4中使用了SentryAuth而不是原生的Auth类,那么目标url的会话项没有设置..我查看了Redirector类的API ,我看到了这个:
public function intended($default, $status = 302, $headers = array(), $secure = null)
{
$path = $this->session->get('url.intended', $default);
$this->session->forget('url.intended');
return $this->to($path, $status, $headers, $secure);
}
Run Code Online (Sandbox Code Playgroud)
并且如代码所示,会话密钥是'url.intended'.我使用原生Auth过滤器验证了这一点,Session::get('url.intended')
并按预期设置了预期的URL .
所以可能的解决方案是在会话中手动设置它.一个例子是:
在你的过滤器上
Route::filter('sentryAuth', function () {
if (!Sentry::check()) {
Session::put('loginRedirect', Request::url());
return Redirect::route('login');
}
});
Run Code Online (Sandbox Code Playgroud)
在postAuthenticate()方法上
if (!Sentry::check()) {
return Redirect::to('user/login');
} else {
// Get the page we were before
$redirect = Session::get('loginRedirect', 'dashboard');
// Unset the page we were before from the session
Session::forget('loginRedirect');
return Redirect::to($redirect);
}
Run Code Online (Sandbox Code Playgroud)
部分代码来自这里以供参考.. ^ _ ^
归档时间: |
|
查看次数: |
5455 次 |
最近记录: |