2013-09-27 19 views
7

我試圖讓我的應用程序設置正確的哨兵軟件包。laravel哨兵重定向::打算不工作

我可以登錄用戶進出,並保護路線,但我似乎無法讓redirect::intended正常工作。我的理解是,用戶在被引導到登錄頁面之前將被帶回他們最初呼叫的路線。目前它只是保持重定向到默認頁面。

在我的routes.php文件我有以下組設置:

Route::group(array('before' => 'sentryAuth'), function() {...} 

在這一組中我已經放在所有受保護的途徑。

在我filters.php我有以下過濾器:

Route::filter('sentryAuth', function() { 

if (!Sentry::check()) { 

    return Redirect::route('login'); 
} 
}); 

路線::過濾器( 'sentryGuest',函數(){

if (Sentry::check()) { 
    return Redirect::intended('dashboard'); 
} 
}); 

在我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'); 
    } 
} 

我試圖訪問一個頁面'預訂/創建'而不登錄。我得到t o登錄頁面,登錄,但它然後帶我到儀表板不預訂/創建。

AM我在這裏錯過了什麼?有沒有額外的代碼,我需要得到預期的工作?

回答

8

我不確定這一點,因爲我沒有使用Sentry來處理當前的項目..所以這只是一個預感。

看來,因爲你使用的SentryAuth而不是在laravel 4本機驗證類,爲目的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); 
} 

並且如代碼所示,會話密鑰是'url.intended'。我使用本地身份驗證過濾器驗證了此事件,並且預期的URL設置爲Session::get('url.intended'),如預期的那樣。

因此,可能的解決方案是在會話中手動設置它。一個例子將是:

在您的濾波器

Route::filter('sentryAuth', function() { 

    if (!Sentry::check()) { 
     Session::put('loginRedirect', Request::url()); 
     return Redirect::route('login'); 
    } 
}); 

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

份從here採取參考..^_^

+0

出色答卷 - 修改工作,併發生重定向如預期。謝謝 – Ray

4

@ reikyoushin的回答非常好。這是一個稍微不同的版本。

路線。PHP

// NOTE: do NOT name your filter "auth" as it will not override 
//  Laravel's built-in auth filter and will not get executed 
Route::filter('sentryAuth', function() 
{ 
    // check if logged in or not 
    if (! Sentry::check()) 
    { 
     // the guest() method saves the intended URL in Session 
     return Redirect::guest('user/login'); 
    } else { 
     // now check permissions for the given route 
     $user = Sentry::getUser(); 
     if (! $user->hasAccess(Route::currentRouteName())) 
     { 
      // redirect to 403 page 
      return Response::make('Forbidden', 403); 
     } 
    } 
}); 

// Protected routes 
Route::group(array('before' => 'sentryAuth', function() 
{ 
    Route::get('admin', function() { return View::make('admin.index'); }); 
}); 

,並在您登錄功能:

public function login() { 
    try { 
     $creds = array(
      'email' => Input::get('email'), 
      'password' => Input::get('password') 
     ); 
     $user = Sentry::authenticate($creds); 
     return Redirect::intended('dashboard'); 
    } catch (Exception $e) { 
     // handle exceptions 
    } 
} 
10

在你filters.php確保使用:的

return Redirect::guest('login'); 

代替

return Redirect::route('login'); 

客人函數將設置t他糾正了預期的()能夠正常工作的會話變量。

0

最簡單的方法。

在您的驗證過濾器使用以下命令:

Route::filter('sentryAuth', function() 
{ 
    if (! Sentry::check()) 
    {  
     return Redirect::guest(route('login')); 
    } 
}); 

在你的控制器:

public function postAuthenticate() 
{ 
    try { 
      $credentials = array(
      'email' => Input::get('email'), 
      'password' => Input::get('password') 
     ); 

     $user = Sentry::authenticate($credentials, false); 
    } catch() { 
     // validation errors 
    } 

    //firstly, Laravel will try to redirect intended page. 
    //if nothing saved in session, it will redirect to home. 
    //you can use any route instead of home. 

    return Redirect::intended(route('home')); 
} 

完成。

外哨兵的:

此代碼可以與任何類型的認證庫的使用。你需要兩樣東西來實現預期的重定向:

1.in您的身份驗證過濾器:

 
    Route::filter('auth', function() 
    { 
     if (! Sentry::check()) 
      { 
      return Redirect::guest(route('login')); 
      } 
    }); 

2.After登錄的用戶:

 
//firstly, Laravel will try to redirect intended page. 
//if nothing saved in session, it will redirect to home. 
//you can use any url instead of '/' 

return Redirect::intended('/');