Search code examples
phplaravellaravel-4cartalyst-sentry

Laravel 4 Sentry 2 Authentication Check Without Using `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.


Solution

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