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.
Install laravel 12 using the following command.
composer create-project laravel/laravel login-app
Configure your env file:
DB_DATABASE=login_db
DB_USERNAME=root
DB_PASSWORD=
Then run:
php artisan migrate
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');
}
}
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');
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>
Add this to routes/web.php:
Route::get('/dashboard', function () {
return 'Welcome to your dashboard!';
})->middleware('auth');
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.
Custom forms give you more control over design and logic without relying on built-in scaffolding like Jetstream or Breeze.
Yes. Laravel uses Bcrypt for password hashing by default. Ensure you use CSRF tokens, input validation, and HTTPS in production for maximum security.
Use the redirect()->intended('dashboard');
method in your login function to send users to their intended page or a default location.
Yes. Modify your login logic to Auth::attempt($credentials, true);
to remember the user across sessions.
Use Laravel’s built-in validation like this:
$request->validate([
'email' => 'required|email',
'password' => 'required|min:6',
]);
You might also like: