Authorization and Authentication of Users in laravel 5 Using Sentry

This tutorial help to integrate sentry framework with laravel 5 for user authorization and authentication.The sentry has very powerful features like groups, permissions and additional security etc.You can get more information form Here.

I have updated new post Simple Example of Laravel 5 Login System Using Sentry.

There are lot of sentry methods to check user authentication,permission and roles such as Sentry_isGuest and Sentry_isLoggedIn.

You can also check other recommended tutorials of Lumen/Laravel,

The Sentry has following features,

  • Authorization
  • Configurable authentication (can use any type of authentication required, such as username or email)
  • Activation of user (optional)
  • Groups and group permissions
  • “Remember me”
  • Password resetting
  • User data
  • Interface driven – switch out your own implementations at will
  • User suspension
  • Login throttling (optional)
  • User banning

You can defined routing based on user authentication like below in laravel route.php file.

  1. Below code is used to provide access to GUEST user.
  2.  // Guest Access
    Route::group(array('middleware' => 'Sentry_isGuest'), function()
    {
        Route::get('/', function(){
            return redirect('user/login');
        });
    
      Route::match(array('get', 'post'), 'user/register', 'UserController@register'); 
      Route::match(array('get', 'post'), 'user/login', 'UserController@login');
      Route::match(array('get', 'post'), 'user/forgot_password', 'UserController@forgotPassword');  
      Route::match(array('get', 'post'), 'user/reset_password', 'UserController@resetPassword');
      Route::get('user/register_thanks', 'UserController@registerThanks');
    });
     
  3. Below code is use to provide access to Loggedin user.
  4.  // Authenticated 
    Route::group(array('middleware' => 'Sentry_isLoggedIn'), function()
    {
        Route::get('user/logout', 'UserController@logout');
        Route::get('user/dashboard', 'UserController@dashboard');
      Route::match(array('get', 'post'), 'product/add_product', 'ProductController@addProduct');
    
        Route::get('/', function(){
            return redirect('user/dashboard');
        }); 
    });
     

Step 1: Need to add sentry as dependency module in composer.json file.

"require": {
        "php": ">=5.5.9",
        "laravel/framework": "5.1.*",
        "cartalyst/sentry": "dev-feature/laravel-5",
        "illuminate/html": "5.*"
    },

Step 2: Now update and install composer.
Step 3: Add below line into config/app.php file.

'providers' => [
   Cartalyst\Sentry\SentryServiceProvider::class,
]

Step 4: Create new UserController.php file in controller folder.
Step 5:You can use user sentry class as a namespace using below code.
use Sentry;

Step 6: Now you can define register,login and logout method here.

class UserController extends Controller
{
    public function register(Request $Request){
    }
    public function login(Request $Request){
      $credentials = array(
                'email'    => $input['email'],
                'password' => $input['password'],
            );

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

              return redirect('user/dashboard');
            }
    }
    public function logout(Request $Request){

      Sentry::logout();
    }
}

Conclusion:

Here i have given basic introduction of sentry and integration with laravel 5.This tutorial help to create basic login and logout features using Sentry in laravel 5.