Laravel Admin Authentication System via MultiAuth
Sep 23, 2016Installing and setting up basic authentication system for a user in laravel is so easy. However, that is not the same case with admin user. So, I decided to create a simple laravel package that handles admin authentication system. Wouldn't it be great if we have something like
php artisan make:auth
that builds us necessary files for admin? Here, we are going to set this up by using multiple authentication in laravel 5.2
NOTE: This package will only work for Laravel 5.2. I'm yet to release support for Laravel 5.3.
Installation
You can install the sarav/admin-auth package by running the following command in your terminal
composer require sarav/admin-auth
Configuration
After installation, you have to register the service provider in your config/app.php
file under providers array.
Sarav\AdminAuth\Providers\AdminAuthServiceProvider::class
Then, publish the configuration file for admin
php artisan vendor:publish
Admin Panel Setup
Once we are done with configuration, let's set up the admin panel by running the following command.
php artisan admin:auth
This will install all necessary files and folders for admin authentication system in laravel. Now migrate to set up admin table.
php artisan migrate
NOTE: Before trying out admin authentication, don't forget to seed admins tabe which we just created!
Once that is done, we have to register the newly created middleware files in app/Http/kernel.php.
Finally clear cache by
php artisan config:cache
Now, if you hit http://localhost:8000/admin/login, you will see admin login page.
Helpers
You can access admin user by
However, typing auth()->guard('admin')->user() everytime is such a pain. So, I have created a nice wrapper functions like admin()
and isAdminLoggedIn()
for accessing admin.
That's it! Now you can setup laravel admin authentication system in no time! If you would like to know what is happening behind the screens, consider reading this article Multiple authentication in Laravel 5.2 Happy Coding!