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.
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:
Create a ServiceProvider to boot it up:
A Facade:
And now you just have to add it all to ServiceProviders
and Aliases in your
config/app.php
:And use it: