Laravel sentary redirect :: intended does not work

I am trying to correctly set the clock device in my application.

I can log in and out and protect the routes, but I cannot get redirect::intended to work correctly. I understand that the user will be returned to the route that they initially call before being redirected to the login page. At the moment, it is simply redirecting to the default page.

In my route.php, I have the following group installed:

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

In this group I placed all protected routes.

In my filters.php, I have the following filters:

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

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

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

In my userController, I have the following code:

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

I tried to access the booking / creation page without logging in. I get to the login page, log in, but then it brings me to the control panel, and does not order / creates.

AM Am I missing something? Is there any additional code I need to get it working?

+7
authentication laravel cartalyst-sentry
source share
4 answers

I am not sure about this because I am not using Sentry for my current project .. so this is just a hunch.

It seems that since you used SentryAuth and not the Auth native class in laravel 4, the session element for the intended URL is not set. I looked at the API on Redirector and I saw this:

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

and, as the code says, the session key is "url.intended". I checked this using my own Auth filter and the intended URL is set to Session::get('url.intended') as expected.

therefore, a possible solution to this is to manually set it in a session. an example would be:

On your filter

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

In your postAuthenticate () method

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

parts of the code were taken from here for reference .. ^ _ ^

+8
source share

In the filters.php file, be sure to use:

  return Redirect::guest('login'); 

instead

  return Redirect::route('login'); 

The guest function sets the correct session variables for the scheduled () to work correctly.

+10
source share

@reikyoushin the answer is excellent. Here is a slightly different version.

routes.php

 // NOTE: do NOT name your filter "auth" as it will not override // Laravel 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' ); } ); }); 

and in your login function:

 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 } } 
+4
source share

The easiest way.

In your Auth filter, use the following:

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

In your controller:

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

Done.

Outside of Sentry:

This code can be used with any type of authentication library. To implement the intended redirection, you need two things:

1. in your autofilter:

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

2. After logging in, the user:

  // 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 ('/');
0
source share

All Articles