I have been researching for a very long time about role-based authentication in Laravel. But I seem to have no luck. So this time I felt like
Lets do is my way and also help others with this post. Here we will be using a package called the sentinel by Cartalyst. So yea lets start.
Sentinal Offers some very great features. Its is already mentioned on their website but if I want to post more content in this post then surely I have to add here too right.
Sentinel is a complete refactor of our popular Sentry authentication & authorization library. Everything you admired plus a whole lot more.
- Users & Roles Management.
- Driver based permission system.
- Flexible activation scenarios.
- Reminders (password reset).
- Inter-account throttling with DDoS protection.
- Custom hashing strategies.
- Multiple sessions.
- Multiple login columns.
- Integration with Laravel.
- Allow use of multiple ORM implementations.
- Native facade for easy usage outside Laravel.
- Interface driven (your own implementations at will).
Reference link – Sentinel
So why are we waiting for, let us start the integration process.
All the upcoming procedures mentioned below are explained in the way you have already installed Laravel Framework on your development machine and also you have completed setting up database connections too.
As always it starts with the composer installation process.
composer require cartalyst/sentinel "2.0.*"
Next is to update the config file from the root dir. Everyone knows this but this is for learners. The file is located in the config/app.php.
In that file update the following preferences.
$providers array add the following service provider for this package.
$aliases array add the following facades for this package.
'Activation' => Cartalyst\Sentinel\Laravel\Facades\Activation::class, 'Reminder' => Cartalyst\Sentinel\Laravel\Facades\Reminder::class, 'Sentinel' => Cartalyst\Sentinel\Laravel\Facades\Sentinel::class,
Now the final step that with that the basic need of Sentinel Package installation is completed. Here we are creating the config and migrations files for the package
php artisan vendor:publish --provider="Cartalyst\Sentinel\Laravel\SentinelServiceProvider"
One point to be noted next. Laravel actually comes with default user Authentication built-in. But yes it doesn’t have role-based authentication. So we are actually removing Laravel’s default authentication. For that remove all the migrations that came when you have installed the Laravel Framework. If you have questions about migrations and what are their use let me know I will create a separate post on that pretty soon. Once you have run the above command now you can create tables by running those migrations with the command
php artisan migrate
With this, all the required steps needs for installing Sentinel in your Laravel Project gets completed.
PS: Laravel also comes with the controllers for its default authentication. So you are safe to remove that complete directory. It is located in the *app/Http/Controllers/Auth*. Just remove the complete Auth directory.
Now comes the usage part. From here we will be learning how to authenticate, verify user, register user and some more.
Everything starts with account registration.
Here we will be using
Let’s say you have already had a form on registering the user. So arguments to this function is an array.
You need to pass the data from the form to this function as per the following structure. Here were considering that you have only email and password for registration.
$credentials = [ 'email' => 'firstname.lastname@example.org', 'password' => 'password', ]; Sentinel::register($credentials);
Instead of this let us make a form with some validations and then register a user.
Here we will add some validations by Creating a Larvel Validation Request Class using the command php artisan make:request RegisterRequest. After creating navigate to Http/Requests and open the file follow the below image.
Next let’s create the controller with the functions for the register with the command php artisan:make controller Auth\\RegisterController . Navigate to Http/Controllers/Auth and Open LoginController. If it says Controllers already exits then don’t worry just remove all the functions that are already available and follow the image below.
And that’s how we created all the functions for registration of a user. The result of this controller came in the way like below image.
With this, we are completing the registration of a user in Laravel Sentinel Package.
Soon I will update the post on Login, Email Verification, and all others.
Thanks for your time. Seen you soon guys