We use cookies to enhance your experience. By continuing to visit this site you agree to our use of cookies. Learn More
contact@techquestworld.com
+919547614783
Premium Article
Published 3 months ago

Master Laravel Auth With Custom Roles in Just 15 Minutes

5 min read
600 views
Master Laravel Auth With Custom Roles in Just 15 Minutes
Photo by Techquestworld

Build a secure Laravel authentication system with custom roles in just 15 minutes. No third-party packages, only clean and modern Laravel code.

Master Laravel Auth With Custom Roles in Just 15 Minutes

Laravel 11 is cleaner and faster and so should be your authentication system. Instead of using Spatie or other packages, let’s manually implement custom roles within Laravel's built-in auth in just 15 minutes.


We'll cover:

• User registration

• Role assignment

• Middleware for access control

• Blade checks

• Dashboard access based on role

🔹 Step 1: Create Project and Auth

laravel new laravel-auth-roles
cd laravel-auth-roles
php artisan make:auth
php artisan migrate

Note: If you're using Laravel Breeze or Laravel UI, auth scaffolding will differ slightly.

🔹 Step 2: Add role to Users Table

Edit the migration:

// database/migrations/xxxx_create_users_table.php
$table->string('role')->default('user'); // or 'admin', 'editor'

Run migration again:

php artisan migrate:fresh

🔹 Step 3: Update Registration to Include Role

// app/Http/Controllers/Auth/RegisterController.php
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'role' => $data['role'] ?? 'user',
]);
}

And in your registration form:

<select name="role" class="form-control">
<option value="user">User</option>
<option value="admin">Admin</option>
</select>

🔹 Step 4: Middleware for Role-Based Access

Create middleware:

php artisan make:middleware RoleMiddleware

Now edit it:

// app/Http/Middleware/RoleMiddleware.php
public function handle($request, Closure $next, $role)
{
if (auth()->check() && auth()->user()->role === $role) {
return $next($request);
}

abort(403, 'Unauthorized.');
}

Register it in app/Http/Kernel.php:

'role' => \App\Http\Middleware\RoleMiddleware::class,

🔹 Step 5: Create Routes with Role Middleware

// routes/web.php
Route::middleware(['auth', 'role:admin'])->group(function () {
Route::get('/admin/dashboard', fn () => view('admin.dashboard'));
});
Route::middleware(['auth', 'role:user'])->group(function () {
Route::get('/user/dashboard', fn () => view('user.dashboard'));
});

🔹 Blade Role Checks

@if(auth()->user()->role === 'admin')
<a href="/admin/dashboard">Admin Panel</a>
@endif

🔹 Optional: Seeder for Quick Testing

// database/seeders/DatabaseSeeder.php
User::factory()->create([
'name' => 'Admin',
'email' => 'admin@example.com',
'password' => bcrypt('password'),
'role' => 'admin',
]);
User::factory()->create([
'name' => 'User',
'email' => 'user@example.com',
'password' => bcrypt('password'),
'role' => 'user',
]);

Run seeder:

php artisan db:seed

🔹 Security Tip

Always validate the role assignment to prevent users from registering as admins through frontend manipulation.

'role' => in_array($data['role'], ['user', 'admin']) ? $data['role'] : 'user',

🧠 Conclusion

With these simple steps, you've built a full custom Laravel role-based auth system in less than 15 minutes, without relying on external packages. This method is:


• Lightweight

• Fully customizable

• Easy to test and extend


Want to add permissions next? That's your next challenge. 😉

Author
TAPAS SAHOO

Developer by Profession, Techie by Heart

A curious mind with a love for writing and technology, dedicated to simplifying web development and programming topics while keeping up with the ever-changing tech landscape.

Discussion (0)

Replying to
Previous Article
Laravel 11 Features That Will Replace Old Cod...

Laravel 11 is here with features that will revolutionize your workflow. Learn how it replaces old pr...

Next Article
15 Common Node.js Mistakes That Are Slowing D...

Node.js is powerful, but even a small mistake can significantly impact performance. Learn the 15 mos...

Related Articles

Create SEO-Friendly Dynamic Routes Using Slugs in Laravel
Create SEO-Friendly Dynamic Routes Using Slugs in...

Learn how to create dynamic routes in Laravel using slugs instead of IDs for clean, SEO-optimized UR...

Laravel vs NodeJS in 2025: Which One Should You Learn First?
Laravel vs NodeJS in 2025: Which One Should You Le...

Laravel or NodeJS – which one should you learn in 2025? This guide helps you choose the right backen...

Pessimistic vs Optimistic Locking in Laravel: A Developers Deep‑Dive
Pessimistic vs Optimistic Locking in Laravel: A De...

Learn how to safeguard your Laravel applications from race conditions by applying the right concurre...

Top 10 Laravel Tricks Every Developer Regrets Not Knowing
Top 10 Laravel Tricks Every Developer Regrets Not...

Boost your Laravel development speed and efficiency with these 10 must know hacks. Perfect for Larav...

Table of Contents