Authentication

In this section, we will cover authentication and login.

Sentry::authenticate()

Authenticates a user based on the provided credentials.

If the authentication was successful, password reset fields and any invalid authentication attempts will be cleared.

Param Required Default Type Description
$credentials true null array Array that should contain the user credentials like email and password.
$remember false false bool Flag to wether Sentry should remember the user. It sets a Cookie.

Example

  1. try
  2. {
  3. // Login credentials
  4. $credentials = array(
  5. 'email' => 'john.doe@example.com',
  6. 'password' => 'password',
  7. );
  8. // Authenticate the user
  9. $user = Sentry::authenticate($credentials, false);
  10. }
  11. catch (Cartalyst\Sentry\Users\LoginRequiredException $e)
  12. {
  13. echo 'Login field is required.';
  14. }
  15. catch (Cartalyst\Sentry\Users\PasswordRequiredException $e)
  16. {
  17. echo 'Password field is required.';
  18. }
  19. catch (Cartalyst\Sentry\Users\WrongPasswordException $e)
  20. {
  21. echo 'Wrong password, try again.';
  22. }
  23. catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
  24. {
  25. echo 'User was not found.';
  26. }
  27. catch (Cartalyst\Sentry\Users\UserNotActivatedException $e)
  28. {
  29. echo 'User is not activated.';
  30. }
  31. // The following is only required if the throttling is enabled
  32. catch (Cartalyst\Sentry\Throttling\UserSuspendedException $e)
  33. {
  34. echo 'User is suspended.';
  35. }
  36. catch (Cartalyst\Sentry\Throttling\UserBannedException $e)
  37. {
  38. echo 'User is banned.';
  39. }

Exceptions

In this section, we provide a list of all exceptions that are thrown by Sentry.

Exception Description
Cartalyst\Sentry\Users\LoginRequiredException When you don’t provide the required login field, this exception will be thrown.
Cartalyst\Sentry\Users\PasswordRequiredException When you don’t provide the password field, this exception will be thrown.
Cartalyst\Sentry\Users\UserNotActivatedException When the provided user is not activated, this exception will be thrown.
Cartalyst\Sentry\Users\UserNotFoundException If the provided user was not found, this exception will be thrown.
Cartalyst\Sentry\Users\WrongPasswordException When the provided password is incorrect, this exception will be thrown.
Cartalyst\Sentry\Throttling\UserSuspendedException When the provided user is suspended, this exception will be thrown.
Cartalyst\Sentry\Throttling\UserBannedException When the provided user is banned, this exception will be thrown.

Sentry::authenticateAndRemember()

Authenticates and Remembers a user based on credentials. This is an helper function for the authenticate() which sets the $remember flag to true so the user is remembered (using a cookie). This is the “remember me” you are used to see on web sites.

Example

  1. Sentry::authenticateAndRemember($credentials);

Sentry::login()

Logs in the provided user and sets properties in the session.

If the login is successful, password reset fields and any invalid authentication attempts will be cleared.

Param Required Default Type Description
$user true null object A user object Cartalyst\Sentry\Users\UserInterface.
$remember false false bool Flag to wether Sentry should remember the user. It sets a Cookie.

Example

  1. try
  2. {
  3. // Find the user using the user id
  4. $user = Sentry::findUserById(1);
  5. // Log the user in
  6. Sentry::login($user, false);
  7. }
  8. catch (Cartalyst\Sentry\Users\LoginRequiredException $e)
  9. {
  10. echo 'Login field is required.';
  11. }
  12. catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
  13. {
  14. echo 'User not found.';
  15. }
  16. catch (Cartalyst\Sentry\Users\UserNotActivatedException $e)
  17. {
  18. echo 'User not activated.';
  19. }
  20. // Following is only needed if throttle is enabled
  21. catch (Cartalyst\Sentry\Throttling\UserSuspendedException $e)
  22. {
  23. $time = $throttle->getSuspensionTime();
  24. echo "User is suspended for [$time] minutes.";
  25. }
  26. catch (Cartalyst\Sentry\Throttling\UserBannedException $e)
  27. {
  28. echo 'User is banned.';
  29. }

Exceptions

Below is a list of exceptions that this method can throw.

Exception Description
Cartalyst\Sentry\Users\LoginRequiredException When you don’t provide the required login field, this exception will be thrown.
Cartalyst\Sentry\Users\UserNotFoundException If the provided user was not found, this exception will be thrown.
Cartalyst\Sentry\Users\UserNotActivatedException When the provided user is not activated, this exception will be thrown.
Cartalyst\Sentry\Throttling\UserSuspendedException When the provided user is suspended, this exception will be thrown.
Cartalyst\Sentry\Throttling\UserBannedException When the provided user is banned, this exception will be thrown.

Sentry::loginAndRemember()

Logs in and Remembers a user based on credentials. This is an helper function for the login() which sets the $remember flag to true so the user is remembered (using a cookie). This is the “remember me” you are used to see on web sites.

Example

  1. Sentry::loginAndRemember($user);

Sentry::logout()

The logout method logs the user out and destroys all Sentry sessions / cookies for the user.

This method does not fail or throw any Exceptions if there is no user logged in.

Example

  1. Sentry::logout();

Sentry::check()

The check method returns a bool of whether the user is logged in or not, or if the user is not activated.

If it’s logged in, the current User is set in Sentry so you can easily access it via getUser().

A user must be activated to pass check().

Example

  1. if ( ! Sentry::check())
  2. {
  3. // User is not logged in, or is not activated
  4. }
  5. else
  6. {
  7. // User is logged in
  8. }