Building a Custom Login Form in Laravel 12

Hi there! In this article, I’ll show you how to create a custom login form in Laravel 12 from scratch. Sometimes, Laravel’s built-in Jetstream or Breeze may feel like overkill for simple needs. That’s why I prefer building my own login system using Laravel's core features.

It's clean, secure, and gives full control over the design and logic. Whether you're a beginner or someone looking to improve Laravel authentication, follow along—I'll make it easy and beginner-friendly with code examples and clear steps.

Step-by-Step Guide to Build a Custom Login Form in Laravel 12

Step-by-Step Guide to Build a Custom Login Form in Laravel 12

 

Step 1: Install Laravel 12

Install laravel 12 using the following command.

composer create-project laravel/laravel login-app

 

Step 2: Set Up Database

Configure your env file:

DB_DATABASE=login_db
DB_USERNAME=root
DB_PASSWORD=

Then run:

php artisan migrate

 

Step 3: Create Auth Controller

Next, create a controller using the following command.

php artisan make:controller Auth/LoginController

Inside LoginController.php:

use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;

class LoginController extends Controller
{
    public function showLoginForm() {
        return view('auth.login');
    }

    public function login(Request $request) {
        $credentials = $request->only('email', 'password');

        if (Auth::attempt($credentials)) {
            return redirect()->intended('dashboard');
        }

        return back()->withErrors([
            'email' => 'Invalid credentials.',
        ]);
    }

    public function logout() {
        Auth::logout();
        return redirect('/login');
    }
}

 

Step 4: Create Login Route

In routes/web.php:

Route::get('/login', [LoginController::class, 'showLoginForm'])->name('login');
Route::post('/login', [LoginController::class, 'login']);
Route::post('/logout', [LoginController::class, 'logout'])->name('logout');

 

Step 5: Create Login Blade View

Create resources/views/auth/login.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Login - Laravel 12</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-light d-flex align-items-center justify-content-center" style="height:100vh;">
    <div class="card shadow p-4" style="width: 22rem;">
        <h4 class="text-center mb-4">Login</h4>
        <form method="POST" action="{{ url('/login') }}">
            @csrf
            <div class="mb-3">
                <label for="email" class="form-label">Email address</label>
                <input type="email" class="form-control" name="email" required>
            </div>
            <div class="mb-3">
                <label for="password" class="form-label">Password</label>
                <input type="password" class="form-control" name="password" required>
            </div>
            <button type="submit" class="btn btn-primary w-100">Login</button>
        </form>

        @if ($errors->any())
            <div class="alert alert-danger mt-3">
                {{ $errors->first() }}
            </div>
        @endif
    </div>
</body>
</html>

 

Step 6: Protect Dashboard Route

Add this to routes/web.php:

Route::get('/dashboard', function () {
    return 'Welcome to your dashboard!';
})->middleware('auth');

 

Conclusion

You’ve just built a custom login form in Laravel 12 without using Jetstream or Breeze. This approach gives you complete control over the logic, design, and validation. Whether you're building a secure PHP login form for clients or want to customize your Laravel authentication flow, this method is lightweight and powerful. Make sure to add validation and rate limiting in production for better security.

 

Frequently Asked Questions (FAQs)

  1. Why create a custom login form in Laravel 12?

    Custom forms give you more control over design and logic without relying on built-in scaffolding like Jetstream or Breeze.

  2. Is this login method secure?

    Yes. Laravel uses Bcrypt for password hashing by default. Ensure you use CSRF tokens, input validation, and HTTPS in production for maximum security.

  3. How do I redirect users after login?

    Use the redirect()->intended('dashboard'); method in your login function to send users to their intended page or a default location.

  4. Can I add "Remember Me" functionality?

    Yes. Modify your login logic to Auth::attempt($credentials, true); to remember the user across sessions.

  5. How to add validation to the login form?

    Use Laravel’s built-in validation like this:

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

 


You might also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS