🔐

Creating Multi-User Login System in Laravel with Breeze

Jul 12, 2024

Tutorial on Creating Multi-User Login System in Laravel with Breeze by Yamin

Introduction

  • Presenter: Yamin
  • Objective: Show how to create Multi-User Login (Mulo) in Laravel using Breeze.

Steps Overview

  1. Create Laravel Project: Initialize a new Laravel project.
  2. Set Up Database: Configure database and create necessary tables.
  3. Install Dependencies: Install required Laravel packages and Breeze for authentication.
  4. Modify Database Schema: Add necessary columns to the user table for differentiating user types.
  5. Create Controllers and Routes: Set up routes and controllers for different user dashboards.
  6. Middleware Setup: Use middleware to restrict access based on user type.

Detailed Steps

1. Create Laravel Project

  • Open command prompt and navigate to desired location.
  • Run: composer create-project --prefer-dist laravel/laravel project-name
  • Example: composer create-project --prefer-dist laravel/laravel multi
  • Navigate to project directory: cd multi
  • Start server: php artisan serve and check in browser.

2. Set Up Database

  • Open project in code editor (e.g., Sublime Text, VS Code).
  • Modify .env file to add database credentials. Example: DB_CONNECTION=mysql DB_DATABASE=multi DB_USERNAME=root DB_PASSWORD=
  • Create database in PHPMyAdmin or command line.

3. Install Dependencies

  • Require Breeze package: composer require laravel/breeze --dev
  • Install Breeze: php artisan breeze:install
    • Choose desired stack (e.g., Blade).
  • Run: npm install and npm run build
  • Run migrations: php artisan migrate

4. Modify Database Schema

  • Add a new column 'user_type' in create_users_table.php migration: $table->string('user_type')->default('user');
  • Run migrations to update database schema.
  • Register and update user types in the database manually.

5. Create Controllers and Routes

  • Create HomeController: php artisan make:controller HomeController
  • Define routes in web.php: Route::get('/admin-dashboard', [HomeController::class, 'adminDashboard'])->name('admin.dashboard'); Route::get('/user-dashboard', [HomeController::class, 'userDashboard'])->name('user.dashboard');
  • Implement the methods in HomeController: public function adminDashboard() { return view('admin.dashboard'); } public function userDashboard() { return view('user.dashboard'); }
  • Create corresponding views in resources/views/admin/ and resources/views/user/

6. Middleware Setup

  • Create middleware: php artisan make:middleware AdminMiddleware
  • Implement middleware logic in AdminMiddleware.php: if (auth()->user()->user_type !== 'admin') { return redirect('/'); } return $next($request);
  • Register middleware in Kernel.php: protected $routeMiddleware = [ ... 'admin' =>](streamdown:incomplete-link)

\App\Http\Middleware\AdminMiddleware::class, ];

- Apply middleware to routes:

Route::middleware(['auth', 'admin'])->group(function () { Route::get('/admin-dashboard', [HomeController::class, 'adminDashboard'])->name('admin.dashboard'); }); Route::middleware(['auth'])->group(function () { Route::get('/user-dashboard', [HomeController::class, 'userDashboard'])->name('user.dashboard'); });

## Conclusion - Successfully set up multi-user login with admin and user dashboards. - Can further extend functionality for additional roles like manager. - Recommended to watch additional tutorials for more complex setups. ## Further Learning - Check linked videos for setting up three types of user logins (admin, user, manager).