කොහොමද ඔයාලට! 👋

අපේ "Path to Laravel" article series එකේ අද අපි යන්නෙ system එකක foundation එක වගේ කොටසකට. ඒ තමයි Authentication සහ Authorization.

None

වැඩේට බහින්න කලින්, ගොඩක් අය පටලවා ගන්න ප්‍රධාන සංකල්ප දෙකක් අපි මුලින්ම පැහැදිලි කරගමු. මොකද මේක තමයි ඕනෑම ලොකු system එකක logic එක තීරණය කරන්නේ:

  1. Authentication (Log වීම): "මේ එන්නෙ ඇත්තටම කලින් register වෙලා ඉන්න user කෙනෙක්ද?" කියලා පරීක්ෂා කිරීම. (උදා: Username/Password නිවැරදිද?)
  2. Authorization (Permission): "ඔයාට මේ දේ කරන්න අවසර තියෙනවාද?" කියලා බැලීම. (උදා: normal user කෙනෙක්ට Admin Dashboard එකට යන්න බෑ).

අද අපි මේ දෙකම Industry Standards වලට අනුකූලව Laravel වලින් හදන හැටි පියවරෙන් පියවර ඉගෙන ගනිමු.

Laravel Breeze 🚀

Laravel වල තියෙන සුපිරිම feature එකක් තමයි "Starter Kits" කියන්නෙ. සාමාන්‍ය PHP වලින් Login, Register, Password Reset, Email Verification වගේ දේවල් ලියන්න දවස් ගණනක් යනවා. හැබැයි Laravel Breeze පාවිච්චි කළාම මේ හැමදේම ඉක්මනින් කරන්න පුලුවන් වගේම Security Best Practices එහෙමත් ඉබේම හැදෙනවා.

A 3D isometric infographic with a dark blue circuit board background, split vertically. Left side, ‘THE OLD WAY’, features a confused human developer, messy desk, slow clocks, broken MVC brain, and red ‘X’ marks. Chaotic red arrows. Right side, ‘THE PROFESSIONAL WAY’, features a proficient human developer at a sleek console, multiple responsive screens, fast stopwatches, green ‘CHECK’ marks, and pristine MVC brain (with Laravel ‘L’). Organized blue arrows. Shared central shield with a checkmark
The path to professional, secure development with Laravel Breeze and built-in best practices.

Breeze Install කරමු:

ඔයාගේ project එකේ terminal එකේ මේ commands ටික පිළිවෙලට run කරන්න:

# 1. Breeze package එක install කිරීම
composer require laravel/breeze --dev

# 2. Breeze setup එක (මෙහිදී 'Blade' සහ 'Dark mode' වැනි කැමති දේ තෝරන්න)
php artisan breeze:install

# 3. Database එකට table ටික යැවීම (users, password_resets tables මෙතැනදී හැදේ)
php artisan migrate

# 4. Frontend assets ටික build කිරීම
npm install
npm run dev

Expert Insight — මොකක්ද මේ වුණේ? Breeze install කළාම ඔයාගේ project එකේ මේ වෙනස්කම් සිද්ධ වෙනවා:

  • Routes: routes/auth.php කියන අලුත් file එක හැදෙනවා. Login, Register, Logout ඔක්කොම routes තියෙන්නේ මෙතනයි.
  • Controllers: app/Http/Controllers/Auth/ ඇතුළේ logic එක හැදෙනවා.
  • Views: resources/views/auth/ ඇතුළේ ලස්සන Frontend page ටික හැදෙනවා.

Auth Facade🧐

User කෙනෙක් log වුණාම Laravel විසින් ඒ user ව "Session" එකක් හරහා මතක තියාගන්නවා. අපිට ඕනෑම තැනකදී (Controller, Blade, හෝ Middleware) මේ user ගෙ විස්තර ගන්න පුළුවන්. ඒකට පාවිච්චි කරන්නේ Auth facade එකයි.

  • Auth::user() — Log වී සිටින user ගෙ Model Instance එක ලබා දෙයි. මෙතැනින් $user->name වගේ data වගේම, ඔහුට අදාළ relationships (උදා: $user->posts) පවා load කරන්න පුළුවන්.
  • Auth::id() — user ID එක විතරක් ලබා දෙයි. database එකට Foreign Key එකක් save කරද්දී (උදා: user_id ලෙස) මුළු model එකම load කරනවාට වඩා මේක වේගවත්.
  • Auth::check() — user log වෙලාදැයි පරීක්ෂා කිරීම මෙයින් සිදු කරයි. මෙය True හෝ False ලබා දෙයි.
  • Auth::attempt($credentials) — username/Password ගැලපෙනවාදැයි බලා ඔහුව log කරවීමට පාවිච්චි කරයි.
  • Auth::logout() — වර්ථමාන session එක සහ Cookies ඉවත් කර user ව logout කරයි.

Code Example (Controller):

File එක: app/Http/Controllers/DashboardController.php

public function index() {
    // 1. user ව log වෙලාද කියලා බලමු
    if (Auth::check()) {
        // 2. log වූ user ගේ සම්පූර්ණ model එක ගමු
        $user = Auth::user(); 
        
        // 3. user ගේ නම View එකට යවමු
        return view('dashboard', ['userName' => $user->name]);
    }
    
    return redirect('/login');
}

Middleware 🛡️

හැම page එකක්ම හැමෝටම පෙන්වන්න බෑනේ. උදාහරණයකට log නොවී Dashboard එක බලන්න බැරි වෙන්න ඕනේ. ඒ වගේම log වුණ කෙනෙක්ට ආයෙත් register page එක පෙන්වන්නත් බෑ. මේක control කරන්නේ Middleware හරහායි.

  • auth Middleware: "ඔයා මුලින්ම log වෙලා ඉන්න ඕනේ මේ page එක බලන්න."
  • guest Middleware: "ඔයා log වෙලා නැති කෙනෙක් (guest) වෙන්න ඕනේ මේ page එක බලන්න."
A 3D isometric infographic with a dark blue circuit board background, split vertically. Left side, ‘THE OLD WAY’, features a confused human developer, messy desk, slow clocks, broken MVC brain, and red ‘X’ marks. Chaotic red arrows. Right side, ‘THE PROFESSIONAL WAY’, features a proficient human developer at a sleek console, multiple responsive screens, fast stopwatches, green ‘CHECK’ marks, and pristine MVC brain (with Laravel ‘L’). Organized blue arrows. Shared central shield with a checkmark
The gap between manual development and professional speed, with built-in security and best practices using Laravel Breeze.

Middleware Grouping කියන්නේ මොකක්ද?

හිතන්න ඔයාට page 10ක් තියෙනවා. ඒ 10ම බලන්න පුලුවන් log වුණ අයට විතරයි නම්, හැම Route එකකටම ->middleware('auth') කියලා වෙන වෙනම ලියන එක වෙලාව නාස්ති කිරීමක්. ඒ වෙනුවට අපි කරන්නේ ඒ routes ඔක්කොම එක "Group" එකකට දාලා, ඒ මුළු group එකටම එකපාර Middleware එක දාන එකයි. මේක DRY (Don't Repeat Yourself) කියන Professional මූලධර්මය ආරක්ෂා කරනවා.

භාවිතා කරන ආකාරය (Code Examples):

File එක: routes/web.php

// 1. තනි Route එකක් 
Route::get('/profile', [ProfileController::class, 'edit'])->middleware('auth');

// 2. Middleware Grouping (Professional Way)
// මෙතන ඇතුළේ ලියන ඕනෑම Route එකකට 'auth' Middleware එක ඉබේම ඇප්ලයි වෙනවා.
Route::middleware(['auth'])->group(function () {
    
    Route::get('/dashboard', [DashboardController::class, 'index']);
    Route::get('/settings', [SettingsController::class, 'show']);
    Route::post('/settings/update', [SettingsController::class, 'update']);
    
    // අවශ්‍ය නම් ගෲප් එකක් ඇතුළේ තව ගෲප් එකක් වුණත් හදන්න පුළුවන් (Nested Groups)
});

// 3. 'guest' Middleware (ලොග් වූ අයට Register පේජ් එකට යාම වළක්වයි)
Route::get('/register', [RegisteredUserController::class, 'create'])->middleware('guest');

Expert Insight: ඔයාටම කියලා අලුත් Middleware එකක් හදාගන්න ඕනේ නම් php artisan make:middleware CheckAge වැනි command එකක් පාවිච්චි කරලා, යම් logic එකක් (උදා: වයස 18 ට අඩු අයට ඇතුල් වීමට තහනම් කිරීම) Route එකකට කලින් run කරන්න පුළුවන්.

Advanced Logic: Role-Based Authorization 👑

Real world project එකක "Admin" සහ "User" විදිහට roles වෙන් කිරීම අත්‍යවශ්‍යයි.

පියවර 01: Database එක සකස් කිරීම

File එක: database/migrations/xxxx_create_users_table.php

// default 'user' ලෙස දීමෙන් අලුතින් register වන හැමෝම සාමාන්‍ය users ල වෙනවා
$table->string('role')->default('user');

පියවර 02: Gates නිර්මාණය කිරීම (The Architect Way)

Gate එකක් කියන්නේ යම් ක්‍රියාවක් කරන්න අවසර තියෙනවාද කියලා බලන සරල logic එකක්.

File එක: app/Providers/AppServiceProvider.php

use Illuminate\Support\Facades\Gate;
use App\Models\User;

public function boot(): void {
    // 'manage-users' කියන ක්‍රියාව කරන්න පුළුවන් admin ට විතරයි කියලා නියම කරමු
    Gate::define('manage-users', function (User $user) {
        // මේ logic එක true වුණොත් විතරයි අවසර ලැබෙන්නේ
        return $user->role === 'admin';
    });
}

පියවර 03: Blade View එකේදී පාලනය කිරීම

File එක: resources/views/layouts/navigation.blade.php

@can('manage-users')
    <a href="/admin/users">User Management</a>
@else
    <p>You are logged in as a Student.</p>
@endcan

Security Architecture 💡

A. Password Hashing (The Safe Vault)

Hacker කෙනෙක් ඔයාගේ database එක හොරකම් කළත්, එයාට user ගෙ password එක කියවන්න බැහැ. මොකද ඒක save වෙන්නේ Bcrypt කියන algorithm එකෙන් Hash වෙලයි.

File එක: app/Models/User.php

protected function casts(): array
{
    return [
        'email_verified_at' => 'datetime',
        'password' => 'hashed', // මේ පේළියෙන් Laravelට කියනවා password එක save වෙද්දී ඉබේම Hash කරන්න කියලා.
    ];
}

Example: user ටයිප් කරන්නේ 123456 වුණාට database එකේ save වෙන්නේ $2y$10$92IXUNpkjO0rOQ5byMi.Ye... වගේ කියවන්න බැරි දිග code එකක්.

B. Session Regeneration (Anti-Hijacking)

"Session Hijacking" කියන්නේ Hacker කෙනෙක් user ගේ Session ID එක හොරකම් කරලා එයා log වෙලා ඉන්නවා වගේ රඟපාන එකයි. Laravel මේක වළක්වන්න user log වුණ සැනින් Session ID එක අලුත් කරනවා.

File එක: app/Http/Controllers/Auth/AuthenticatedSessionController.php (Breeze)

public function store(LoginRequest $request)
{
    $request->authenticate();

    // log වුණ ගමන් පරණ session එක අයින් කරලා අලුත් එකක් හදනවා
    $request->session()->regenerate(); 

    return redirect()->intended(RouteServiceProvider::HOME);
}

C. Rate Limiting / Throttling (Anti-Brute Force)

Hacker කෙනෙක්ට එක දිගට password දහස් ගණනක් ගහලා චෙක් කරන්න (Brute Force) බැරි වෙන්න, Laravel විසින් login උත්සාහයන් සීමා කරනවා.

File එක: app/Http/Requests/Auth/LoginRequest.php

public function ensureIsNotRateLimited(): void
{
    // user වැරදි password 5 වතාවකට වඩා ගැහුවොත්, ඔහුව තත්පර ගණනාවකට බ්ලොක් කරනවා.
    if (! RateLimiter::tooManyAttempts($this->throttleKey(), 5)) {
        return;
    }

    event(new Lockout($this));

    $seconds = RateLimiter::availableIn($this->throttleKey());

    throw ValidationException::withMessages([
        'email' => trans('auth.failed'),   // "Too many login attempts" පණිවිඩය මෙතැනින් ලැබේ.
    ]);
}
A 3D isometric infographic detailing “SECURITY ARCHITECTURE: MULTI-LAYERED DEFENSE” against a dark circuit background. A fanned array details three scenarios: A (Password Hashing via Bcrypt), B (Session ID Regeneration on login), and C (Rate Limiting blocking fanned brute force hacker icons after 5 failed attempts). A professional developer orchestrates the defenses against red chaotic paths and allows blue accepted ones. Minimalist tech-professional style.
Multi-Layered Security Architecture: The guardian of professional application defense.

💡 Expert Tips for Beginners

  • Mass Assignment: user කෙනෙක් තමන්ගේ profile එක update කරද්දී role එකත් වෙනස් කරන්න උත්සාහ කරන්න පුළුවන්. ඒ නිසා model එකේ $fillable එකට role ඇතුළත් කරන්න එපා.
  • Auth::check() inside Blade: Blade views වලදී @auth සහ @guest කියන directives පාවිච්චි කරන්න. ඒක code එක clean කරනවා.
@auth
    Welcome {{ Auth::user()->name }}
@endauth
  • Use ID for Performance: user ව database එකට සම්බන්ධ කරද්දී හැමවිටම Auth::id() පාවිච්චි කරන්න. එය අනවශ්‍ය SQL queries ප්‍රමාණය අඩු කරයි.

🎯 Conclusion & What's Next?

අද අපි ඉගෙන ගත්තා Laravel පද්ධතියක ආරක්ෂාව සහ user management එක පියවරෙන් පියවර world standard අනුව හදන හැටි. මේ සංකල්ප ටික ඔයාට ඕනෑම සංකීර්ණ පද්ධතියක් හදන්න ලොකු පදනමක් වෙයි.

ඊළඟ ලිපියෙන් (Article 14) අපි බලමු RESTful API Development ගැන. Mobile Apps සහ React/Vue වැනි Frontend සඳහා Laravel වලින් API එකක් හදන්නේ කොහොමද කියලා අපි එදාට කතා කරමු!

ඔයා Breeze install කරද්දී මොකක් හරි Error එකක් ආවද? නැත්නම් Gate එකක් වැඩ කරන්නේ නැද්ද? පහළින් Comment කරන්න! 👇

✍️ Dinushka Tharidu

#PHP #Laravel #Authentication #Authorization #LaravelBreeze #CyberSecurity #WebDevelopment #SoftwareEngineering #PathToLaravel #DTech #ExpertGuide #MasteringLaravel