donglizhan7848 2013-12-28 20:30
浏览 29
已采纳

Laravel 4 Sentry 2身份验证检查不使用`catch`?

Suppose I have this code for Sentry authentication:

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

            $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\WrongPasswordException $e) {
            echo 'Wrong password, try again.';
        }
        catch (Cartalyst\Sentry\Users\UserNotFoundException $e) {
            echo 'User was not found.';
        }
        catch (Cartalyst\Sentry\Users\UserNotActivatedException $e) {
            echo 'User is not activated.';
        }

I want to try to remove all the try and catch (I have my reason, it's multilevel authentication, I'm trying to shorten my code). So I tried this to check if whether authentication failed:

$credentials = array(
                'email'    => Input::get('email'),
                'password' => Input::get('password'),
            );

            $user = Sentry::authenticate($credentials, false);

            if (is_null($user)) {
                /* error logic here */
            }
else{
   /* login success! */
}

The is_null won't work, the code won't continue. Any suggestions or solutions?

EDIT

Using Chrome's console, var_dump($user) did not show anything.

展开全部

  • 写回答

1条回答 默认 最新

  • dqc2017 2013-12-28 20:49
    关注

    In Sentry 2 you can't, Sentry 3 will let you do it, but it still not finished. So you best bet is to create a service and a Facade for it:

    Create your service:

    <?php namespace Acme\Services\Authentication;
    
    use Cartalyst\Sentry\Sentry;
    
    class Service {
    
        private $this->error;
    
        public function __construct(Sentry $sentry)
        {
            $this->sentry = $sentry;    
        }
    
        public function getError()
        {
            return $this->error;
        }
    
        public function authenticate($credentials, $remember = true)
    
            try {
                return Sentry::authenticate($credentials, $remember);
            }
            catch (Cartalyst\Sentry\Users\LoginRequiredException $e) {
                $this->error = 'Login field is required.';
            }
            catch (Cartalyst\Sentry\Users\PasswordRequiredException $e) {
                $this->error = 'Password field is required.';
            }
            catch (Cartalyst\Sentry\Users\WrongPasswordException $e) {
                $this->error = 'Wrong password, try again.';
            }
            catch (Cartalyst\Sentry\Users\UserNotFoundException $e) {
                $this->error = 'User was not found.';
            }
            catch (Cartalyst\Sentry\Users\UserNotActivatedException $e) {
                $this->error = 'User is not activated.';
            }
        }
    
    }
    

    Create a ServiceProvider to boot it up:

    <?php namespace Acme\Services\Authentication;
    
    use Illuminate\Support\ServiceProvider as  IlluminateServiceProvider;
    use Acme\Services\Authentication\Service as Authentication;
    
    class ServiceProvider extends IlluminateServiceProvider {
    
        /**
         * Indicates if loading of the provider is deferred.
         *
         * @var bool
         */
        protected $defer = true;
    
        /**
         * Register the service provider.
         *
         * @return void
         */
        public function register()
        {
            $this->app->bind('authentication', function($app) {
    
                return new Authentication($app->make('sentry'));
    
            });
        }
    
        /**
         * Get the services provided by the provider.
         *
         * @return array
         */
        public function provides()
        {
            return array('authentication');
        }
    
    }
    

    A Facade:

    <?php namespace Acme\Services\Authentication;
    
    use Illuminate\Support\Facades\Facade as IlluminateFacade;
    
    class Facade extends IlluminateFacade {
    
        /**
         * Get the registered name of the component.
         *
         * @return string
         */
        protected static function getFacadeAccessor() { return 'authentication'; }
    
    }
    

    And now you just have to add it all to ServiceProviders

    'Acme\Services\Authentication\ServiceProvider',
    

    and Aliases in your config/app.php:

    'Authentication' => 'Acme\Services\Authentication\Facade',
    

    And use it:

    $credentials = array(
                    'email'    => Input::get('email'),
                    'password' => Input::get('password'),
                );
    
    if ( ! $user = Authentication::authenticate($credentials))
    {
        echo Authentication::getError();
    }
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部