laravel sentry redirect::intended not working

后端 未结 4 663
走了就别回头了
走了就别回头了 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:20

    @reikyoushin's answer is excellent. Here's a slightly different version.

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

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

提交回复
热议问题