DEV Community

Ana Elena Ulate Salas
Ana Elena Ulate Salas

Posted on

๐Ÿพ Building a Secure Comment System in Laravel (with ID Authentication and Arithmetic CAPTCHA)

In this post, Iโ€™ll share how I developed a small but complete comment system in Laravel 12, focused on authentication, data validation, and basic security.
The project is called Pet Shop โ€“ Comment System, and it was a great opportunity to practice good coding practices and understand Laravelโ€™s core features.


๐ŸŽฏ Project Goal

The main goal was to build a functional web application that simulates a login process, comment submission, and message confirmation โ€” applying Laravelโ€™s validation and security features.

I wanted to create something minimal yet complete, that demonstrates the foundations of modern Laravel development.


โš™๏ธ Tech Stack

  • Laravel 12 (PHP 8.2+)
  • MySQL / MariaDB
  • Bootstrap 5 (via CDN)
  • Blade Templates
  • Artisan CLI

๐Ÿ” ID-Based Authentication

Instead of using an email address for login, I implemented authentication by ID number, a method often used in local systems.
This allowed me to create custom validation rules and flexible user management.

In the AuthController, the login logic works like this:

$request->validate([
    'cedula' => 'required|numeric',
    'password' => 'required|min:6'
]);

$user = User::where('cedula', $request->cedula)->first();

if ($user && Hash::check($request->password, $user->password)) {
    Auth::login($user);
    return redirect('/home');
} else {
    return back()->withErrors(['login' => 'Invalid credentials']);
}
Enter fullscreen mode Exit fullscreen mode

This manual authentication flow helped me understand Laravelโ€™s internals without relying on Breeze or Jetstream.


๐Ÿงฎ Arithmetic CAPTCHA

To prevent automated logins and spam, I implemented a simple arithmetic CAPTCHA that randomly generates addition, subtraction, or multiplication problems.

Hereโ€™s a simplified version of the logic:

$num1 = rand(0, 9);
$num2 = rand(0, 9);
$operator = ['+', '-', '*'][rand(0, 2)];

switch ($operator) {
    case '+':
        $result = $num1 + $num2;
        break;
    case '-':
        $result = $num1 - $num2;
        break;
    case '*':
        $result = $num1 * $num2;
        break;
}

session(['captcha_result' => $result]);
Enter fullscreen mode Exit fullscreen mode

When the user submits the form, the validation checks if the provided answer matches the stored result:

if ($request->captcha != session('captcha_result')) {
    return back()->withErrors(['captcha' => 'Incorrect CAPTCHA result.']);
}
Enter fullscreen mode Exit fullscreen mode

Itโ€™s a simple, effective solution that doesnโ€™t require external APIs.


๐Ÿ’ฌ Comment System

Once logged in, users can create comments about services such as baths, walks, or grooming.
Each comment requires a minimum length and must belong to one of the predefined services.

$request->validate([
    'content' => 'required|string|min:10',
    'service' => 'required|in:banos,paseos,cortes'
]);

Comment::create([
    'user_id' => Auth::id(),
    'service' => $request->service,
    'content' => $request->content,
]);
Enter fullscreen mode Exit fullscreen mode

After submission, the user is redirected to a simple confirmation view:

<h2>Comment submitted successfully ๐Ÿพ</h2>
<p>Your feedback is very important to us.</p>
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“‚ Project Structure

The system follows Laravelโ€™s standard folder organization, keeping logic separated into controllers and models:

app/
โ”œโ”€โ”€ Http/Controllers/
โ”‚   โ”œโ”€โ”€ AuthController.php
โ”‚   โ”œโ”€โ”€ CommentController.php
โ”‚   โ””โ”€โ”€ HomeController.php
โ””โ”€โ”€ Models/
    โ”œโ”€โ”€ Comment.php
    โ””โ”€โ”€ User.php
resources/views/
โ”œโ”€โ”€ layouts/
โ”œโ”€โ”€ comments/
โ””โ”€โ”€ auth/
routes/web.php
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“š Lessons Learned

  1. Manual authentication โ€“ Building a login flow from scratch helps you understand Laravelโ€™s core security features.
  2. Validation rules โ€“ Laravel Validation makes it easy to express complex rules in a clean way.
  3. Lightweight CAPTCHA โ€“ Sometimes a simple arithmetic CAPTCHA is all you need for basic security.
  4. UX & Accessibility โ€“ Clear validation messages and confirmations improve usability.
  5. Simplicity matters โ€“ Even small projects can teach big lessons about architecture and clean code.

๐Ÿš€ Next Steps

Some planned improvements include:

  • Adding unit tests using Pest or PHPUnit
  • Building an admin dashboard for comment moderation
  • Integrating email notifications for confirmation messages

๐Ÿถ Conclusion

This project helped me strengthen my skills in authentication, validation, and secure data handling using Laravel.
Itโ€™s a reminder that even simple projects can teach a lot about quality, structure, and accessibility.

๐Ÿ‘‰ You can check the full code here:
๐Ÿ”— GitHub โ€“ Animal Shop (Pet Shop - Comment System)


Technical note: The code shown in this article is a simplified version of the real project, with minor variable name and syntax differences, but it preserves the exact same logic and functionality.

Top comments (0)