Create Login page admin and User Laravel

Create Login page admin and User Laravel

Creating a Login Page for Admin and User in Laravel

In this guide, we'll build a Laravel authentication system with separate logins for Admin and Users. We'll cover:
Authentication Setup
Database Migration for Users
Middleware for Role-Based Access
Creating Login Views
Redirecting Admin & Users After Login

Step 1: Install Laravel & Authentication

If you haven't already, install Laravel and set up authentication.

1️⃣ Install Laravel

composer create-project laravel/laravel auth_system cd auth_system

2️⃣ Set Up Database Connection

Edit the .env file with your database details:

DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=auth_system DB_USERNAME=root DB_PASSWORD=

3️⃣ Install Laravel Breeze (Authentication Scaffold)

composer require laravel/breeze --dev php artisan breeze:install npm install && npm run dev php artisan migrate

This will create Login & Registration pages for users.

Step 2: Modify Users Table for Roles

By default, Laravel creates a users table. We need to add a role column.

1️⃣ Create a Migration

php artisan make:migration add_role_to_users_table --table=users

2️⃣ Update Migration File (database/migrations/YYYY_MM_DD_add_role_to_users_table.php)

public function up() { Schema::table('users', function (Blueprint $table) { $table->string('role')->default('user'); // Default role: user }); } public function down() { Schema::table('users', function (Blueprint $table) { $table->dropColumn('role'); }); }

3️⃣ Run Migration

php artisan migrate

Step 3: Modify User Model

Edit app/Models/User.php to include role in the fillable array.

protected $fillable = [ 'name', 'email', 'password', 'role', ];

Step 4: Register Users with Roles

Modify app/Http/Controllers/Auth/RegisterController.php to assign roles dynamically.

use App\Models\User; use Illuminate\Http\Request; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Validator; class RegisterController extends Controller { public function create(array $data) { return User::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => Hash::make($data['password']), 'role' => isset($data['role']) ? $data['role'] : 'user', // Assign role ]); } }

Step 5: Create Middleware for Admin Access

We need middleware to restrict pages to admins only.

1️⃣ Create Admin Middleware

php artisan make:middleware AdminMiddleware

2️⃣ Edit Middleware (app/Http/Middleware/AdminMiddleware.php)

use Closure; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; class AdminMiddleware { public function handle(Request $request, Closure $next) { if (Auth::check() && Auth::user()->role == 'admin') { return $next($request); // Allow admin access } return redirect('/'); // Redirect non-admin users } }

3️⃣ Register Middleware (app/Http/Kernel.php)

protected $routeMiddleware = [ 'admin' => \App\Http\Middleware\AdminMiddleware::class, ];

Step 6: Define Routes for Admin & User

Modify routes/web.php to define separate routes.

use App\Http\Controllers\Auth\LoginController; use App\Http\Controllers\AdminController; use App\Http\Controllers\UserController; Route::get('/', function () { return view('welcome'); }); Auth::routes(); // Admin Routes (Protected) Route::middleware(['auth', 'admin'])->group(function () { Route::get('/admin/dashboard', [AdminController::class, 'index'])->name('admin.dashboard'); }); // User Routes (Protected) Route::middleware(['auth'])->group(function () { Route::get('/user/dashboard', [UserController::class, 'index'])->name('user.dashboard'); });

Step 7: Create Admin & User Controllers

Generate controllers for admin and users.

php artisan make:controller AdminController php artisan make:controller UserController

1️⃣ Edit app/Http/Controllers/AdminController.php

namespace App\Http\Controllers; use Illuminate\Http\Request; class AdminController extends Controller { public function index() { return view('admin.dashboard'); } }

2️⃣ Edit app/Http/Controllers/UserController.php

namespace App\Http\Controllers; use Illuminate\Http\Request; class UserController extends Controller { public function index() { return view('user.dashboard'); } }

Step 8: Create Login Views for Admin & User

Create view files for admin and user dashboards.

1️⃣ Admin Dashboard View (resources/views/admin/dashboard.blade.php)

@extends('layouts.app') @section('content') <div class="container"> <h1>Welcome, Admin</h1> <a href="{{ route('logout') }}" onclick="event.preventDefault(); document.getElementById('logout-form').submit();"> Logout </a> <form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;"> @csrf </form> </div> @endsection

2️⃣ User Dashboard View (resources/views/user/dashboard.blade.php)

@extends('layouts.app') @section('content') <div class="container"> <h1>Welcome, User</h1> <a href="{{ route('logout') }}" onclick="event.preventDefault(); document.getElementById('logout-form').submit();"> Logout </a> <form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;"> @csrf </form> </div> @endsection

Step 9: Redirect Users After Login

Modify app/Providers/RouteServiceProvider.php to send admins to /admin/dashboard and users to /user/dashboard.

protected function authenticated(Request $request, $user) { if ($user->role === 'admin') { return redirect('/admin/dashboard'); } else { return redirect('/user/dashboard'); } }

Step 10: Test the Login System

1️⃣ Register a User

  • Go to /register

  • Create an account

  • Change role to 'admin' manually in the database to test the admin login.

2️⃣ Login as an Admin

  • Visit /login

  • If admin → redirected to /admin/dashboard

  • If the user → redirected to /user/dashboard

Summary

Setup Laravel Authentication with Laravel Breeze
Added role column to users table
Created AdminMiddleware for role-based access
Defined separate routes for Admin & Users
Created separate login views for Admin & Users
Redirected users to the correct dashboard after login

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close