laravel sentry redirect::intended not working

后端 未结 4 664
走了就别回头了
走了就别回头了 2021-02-06 09:45

I\'m trying to get the sentry package set up in my app correctly.

I can log a user in and out and protect routes but I can\'t seem to get the redirect::intended

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-06 10:22

    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. You need two things to implement intended redirection:

    1.in your auth filter:

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

    2.After logged in an 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('/');
    

提交回复
热议问题