Kritim Yantra
Jun 28, 2025
Ever worried about hackers breaking into your Laravel app? π Whether you're building a simple blog or a full-scale SaaS platform, authentication is your app's first line of defense.
Laravel 12 makes authentication easy, secure, and customizable. In this guide, weβll cover:
β
Built-in Auth Systems (Breeze, Jetstream, Sanctum)
β
Custom Authentication (Manual Login & Registration)
β
Advanced Security (2FA, Rate Limiting, Password Hashing)
Letβs lock things down! π
Before diving into code, let's understand why authentication is crucial:
β Protects User Data β Prevents unauthorized access.
β Prevents Attacks β Blocks brute force & credential stuffing.
β Enhances Trust β Users feel safe using your app.
Laravel offers three powerful tools for authentication:
Perfect for beginners! It includes:
Installation:
composer require laravel/breeze --dev
php artisan breeze:install
npm install && npm run dev
php artisan migrate
Need more? Jetstream adds:
Installation:
composer require laravel/jetstream
php artisan jetstream:install livewire
npm install && npm run dev
php artisan migrate
Building a mobile app or SPA? Sanctum provides token-based auth.
Installation:
composer require laravel/sanctum
php artisan sanctum:install
php artisan migrate
Sometimes, you need full control. Letβs build auth from scratch!
// routes/web.php
Route::get('/register', [AuthController::class, 'showRegister'])->name('register');
Route::post('/register', [AuthController::class, 'register']);
Route::get('/login', [AuthController::class, 'showLogin'])->name('login');
Route::post('/login', [AuthController::class, 'login']);
Route::post('/logout', [AuthController::class, 'logout'])->name('logout');
// app/Http/Controllers/AuthController.php
public function register(Request $request)
{
$validated = $request->validate([
'name' => 'required|string|max:255',
'email' => 'required|email|unique:users',
'password' => 'required|confirmed|min:8',
]);
$user = User::create([
'name' => $validated['name'],
'email' => $validated['email'],
'password' => Hash::make($validated['password']),
]);
Auth::login($user);
return redirect('/dashboard');
}
public function login(Request $request)
{
$credentials = $request->validate([
'email' => 'required|email',
'password' => 'required',
]);
if (Auth::attempt($credentials)) {
return redirect('/dashboard');
}
return back()->withErrors(['email' => 'Invalid credentials!']);
}
Route::middleware('auth')->group(function () {
Route::get('/dashboard', [DashboardController::class, 'index']);
});
Use Laravel Fortify or Jetstream for 2FA.
Example (Fortify):
composer require laravel/fortify
php artisan vendor:publish --provider="Laravel\Fortify\FortifyServiceProvider"
// app/Http/Middleware/ThrottleLogins.php
Route::post('/login', [AuthController::class, 'login'])
->middleware('throttle:5,1'); // 5 attempts per minute
Laravel automatically hashes passwords using bcrypt
:
$user->password = Hash::make('secure123');
β Manual Testing β Try logging in with wrong credentials.
β PHPUnit Testing β Automate security checks.
Example Test:
public function test_login_fails_with_wrong_password()
{
$user = User::factory()->create();
$response = $this->post('/login', [
'email' => $user->email,
'password' => 'wrongpass',
]);
$response->assertSessionHasErrors();
}
β Use Breeze/Jetstream for quick setup
β Build custom auth for full control
β Enable 2FA & rate limiting for security
β Always hash passwords
β Test authentication flows
Laravel 12 makes authentication effortless and secure. Whether you use Breeze, Jetstream, or custom auth, your app will be locked down tight.
Got questions? Drop a comment below! π¬π
Happy coding! π»π₯
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google