Security & APIs · 8 min read · Intermediate
Authentication, Middleware & Authorization Policies
Secure routes with middleware and enforce per-record ownership using Laravel Policies and Gates.
What you'll learn
- Protect route groups using `auth` and `throttle` middleware
- Write clean `Policy` classes to prevent IDOR vulnerabilities
- Hash passwords safely using `Hash::make()` (bcrypt/Argon2)
Overview
Secure routes with middleware and enforce per-record ownership using Laravel Policies and Gates.
Code Example
<?php
namespace App\Policies;
use App\Models\Note;
use App\Models\User;
class NotePolicy {
public function update(User $user, Note $note): bool {
return $note->user_id === $user->id;
}
}
// In NoteController::update
$this->authorize('update', $note);
Key Takeaways
- Protect route groups using
authandthrottlemiddleware - Write clean
Policyclasses to prevent IDOR vulnerabilities - Hash passwords safely using
Hash::make()(bcrypt/Argon2)
Quick Check
Check Your Understanding
Score: 0 / 1
1. Why is checking `auth` middleware alone NOT enough on `DELETE /notes/{note}`?
Study Workspace
Finished this lesson?
Your progress is saved locally in your browser, no account needed.