How to make Multiple Login System using auth in Laravel 5.8 (User + Admin) with Middleware
How to make Multiple Login System using auth in Laravel 5.8 (User + Admin) with Middleware
Step 1: Install your Laravel by issuing the Composer with the command called create-project command in your terminal:
Step 2: Setup your database (DB) and set the credentials in your environment file (.env) in your installed laravel application.
Step 3: Go to your application and open the folder by following: app/Providers/AppServiceProvider.php, open this file and add the below code, to set the defaultStringLength to skip the Error called: Specified key was too long error.
Step 4: Now, lets set the login and registration system using laravel auth command by below code:
Step 5: All the setup for login and register is successfully done. Now, let's edit user table which is available in following folder: database / migrations / yoursomedate_create_users_table.php and add the type column for it:
Note: the value which you store in role_as column: 0 = default_user, 1 = admin_user
Step 6: Let's migrate this table in database by following commad:
If you have already migrated the table, then directly you can edit your phpmyadmin database and add the type column named as : role_as TINYINT (4) -> AsDefined (0);
Step 7: Now, Let's Create a custom middleware to check is the Logged_In user is admin or not.! The command for generating middleware as following:
after middleware generated go to your AdminMiddleware as follows: app/Http/Middleware/AdminMiddleware.php and paste the below code:
Step 8: Now Register your Middleware just you created named AdminMiddleware with the following path: app/Http/Kernel.php and goto the routeMiddleware and paste the below code:
Step 9: Set your Redirection when you are logging as user or admin or any other role provided. Goto path - app/Http/Controller/Auth/LoginController.php and paste the below code:
Step 10: Lets Setup the Route for this Process of multiple authentication as give below:
Step 11: Finally, Now let's register. Example: Email_id: admin@gmail.com and Password: Pass*****, but we see that when we register, the role_as default sets as '0', so for the first user you can update its role as '1' by this below command,
then you can create your CRUD Application for Assigning the Roles for this Admin, as in next post i have compled.