Php

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.

Related Post
  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.

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More

3 months ago

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

9 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

9 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

9 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

10 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

10 months ago

Categories