Laravel 9 Authentication Tutorial with UI Template
In this guide, we’ll walk you through installing and setting up a Laravel 9 authentication system with Vue UI scaffolding and role-based access.
Prerequisites
Ensure you have the following installed:
-
PHP 8.0.2+
-
MySQL
-
Composer (PHP package manager)
Step 1: Clone Laravel Project
git clone https://github.com/StarCodeKh/Laravel-9-Sign-Up-Sign-In-Sample-with-Template.git
cd Laravel-9-Sign-Up-Sign-In-Sample-with-Template
Step 2: Install UI
composer require laravel/ui
php artisan ui vue --auth
Step 3: Configure .env
File
Update your .env
with database and mail credentials:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_db
DB_USERNAME=root
DB_PASSWORD=your_password
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your_email@gmail.com
MAIL_PASSWORD=your_email_password
MAIL_ENCRYPTION=tls
Step 4: Define Web Routes (routes/web.php
)
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Auth\RegisterController;
use App\Http\Controllers\HomeController;
Route::get('/', function () {
return view('auth.login');
});
Route::middleware(['auth'])->group(function () {
Route::get('/home', [HomeController::class, 'index'])->name('home');
});
Auth::routes();
Route::controller(LoginController::class)->group(function () {
Route::get('/login', 'login')->name('login');
Route::post('/login', 'authenticate');
Route::get('/logout', 'logout')->name('logout');
});
Route::controller(RegisterController::class)->group(function () {
Route::get('/register', 'register')->name('register');
Route::post('/register', 'storeUser')->name('register');
});
Step 5: LoginController (app/Http/Controllers/Auth/LoginController.php
)
Includes custom login handling with email domain append and session storage.
Step 6: RegisterController (app/Http/Controllers/Auth/RegisterController.php
)
Validates and stores user details along with selected role.
Step 7: Public Assets Structure (public/assets/
)
Organize assets:
- css/
- js/
- images/
- vendor/
Step 8: Migration Files
create_users_table.php
Custom user fields: join_date
, phone_number
, status
, role_name
, avatar
, etc.
create_role_type_users_table.php
Includes role types like Admin, Super Admin, Normal User, etc.
Step 9: User Model (app/Models/User.php
)
Defines fillable attributes and default Laravel Auth user functionality.
Step 10: Views
resources/views/auth/login.blade.php
Contains login form using Bootstrap and Toastr for notifications.
resources/views/auth/register.blade.php
Registration form with fields: name, email, role, password, etc.
This setup provides a basic Laravel 9 app with authentication, UI scaffolding, and role-based access. You can customize further based on your application's needs.