Creating Multi-user Login System in Laravel

Mar 15, 2025

Lecture Notes: Creating Multi-user Login System in Laravel Using Breeze

Introduction

  • Presenter: Yamin
  • Objective: Develop a multi-user login system (MULO) using Laravel and Breeze.
  • Concept: MULO allows both admin and users to login, redirecting them to different dashboards.

Initial Setup

Create Laravel Project

  • Open Command Prompt (CMD) and navigate to the desired location.
  • Run the command: composer create-project laravel/laravel project-name (e.g., multi).
  • Verify project creation.

Run Laravel Project

  • Open CMD in the project directory.
  • Run: php artisan serve to start the server.
  • Access the project via browser.

Database Configuration

Set Up Database

  • Open your code editor (e.g., Sublime Text, VS Code).
  • Edit .env file:
    • Set DB_CONNECTION to mysql.
    • Define DB_DATABASE as multi.
  • Use a tool like phpMyAdmin to create the database multi.

Install Laravel Breeze

Installation Steps

  • In CMD, run: composer require laravel/breeze --dev.
  • Install Breeze: php artisan breeze:install.
  • Select options (e.g., Blade for templates, enable PHPUnit).
  • Run: npm install and npm run build.

Modify User Table

Migration Changes

  • In the migrations folder, modify the user table to include a user_type column with default user.
  • Execute migration: php artisan migrate.

User Registration and Role Assignment

Register Users

  • Register users via the Laravel registration form (e.g., user, admin).
  • Manually change user_type in database for roles (e.g., admin).

Create Routes and Controllers

Admin Routing

  • Create HomeController using CMD: php artisan make:controller HomeController.
  • Define routes in web.php for admin dashboard.
  • Add logic to HomeController to return appropriate views.

Login Functionality

Authenticate Users

  • Modify AuthenticatedSessionController.php:
    • Add condition to redirect admin to admin dashboard.
    • Fallback for regular users to user dashboard.

Middleware for Access Control

Protect Admin Dashboard

  • Create middleware via CMD: php artisan make:middleware Admin.
  • Define access logic to restrict users without admin role.
  • Register middleware in bootstrap/app.php.
  • Apply middleware to routes.

Finalize Setup

  • Test login and redirection for both user types.
  • Ensure unauthorized users cannot access restricted pages.

Conclusion

  • Successfully implemented MULO using Laravel Breeze.
  • Extended possibilities for multiple user roles.
  • For additional user types (e.g., manager), create additional middleware.

Tips: Keep practicing and exploring further Laravel features for more robust applications.