Hi, Laravel Developers! Are you curious about how authentication works in Laravel 12? Maybe you’re trying to start with coding and want to understand how users are able to log in, register, and keep their accounts safe. Well, you’re in the right place!
Let’s classify it step-by-step and with fun!
What Is Authentication?
Authentication is a big word, but it just means this:
“Who are you? Prove it!”
While logging into a website, they usually type a username or email and a password. The website checks if that person is real and allowed to get in. You can create Database seeding file to create a user for a demo account.
Step 1: Starting a Laravel Project
Let’s build our own little clubhouse! First, you need Laravel.
Run this in your terminal:
composer create-project laravel/laravel my-auth-app
Then, go into your new project folder:
cd my-auth-app Start your local server: php artisan serve
Presently you can observe your app in the browser at http://localhost:8000
Step 2: Adding Authentication (The Easy Way!)
Laravel 12 doesn’t come with login/register pages right away. But Laravel made a special tool named Laravel UI that includes them for us!
Run this in your terminal:
composer require laravel/ui
Then, install the auth UI with Bootstrap:
php artisan ui bootstrap --auth
And finally, install the frontend stuff:
npm install && npm run dev
Now, Laravel adds all the pages you need to Register, Log in, and Log out. Try visiting http://localhost:8000/register
Step 3: What Just Happened?
Calm down and explain what Laravel did.
Created routes (URLs) like
Route::get('/login', [LoginController::class, 'showLoginForm']); Route::post('/login', [LoginController::class, 'login']); Route::post('/logout', [LoginController::class, 'logout']);
Created views (HTML pages):
resources/views/auth/login.blade.php
resources/views/auth/register.blade.php
Used a User model in app/Models/User.php to save your data.
Step 4: Try It Out!
- Go to /register and make a new user.
- You’ll be logged in automatically.
- Go to /home and see the dashboard!
- Try logging out and logging in again at /login.
How Does Laravel Know You’re Logged In?
Laravel uses features called sessions and middleware.
Route::get('/dashboard', function () { return view('dashboard'); })->middleware('auth');
Read More Articles
Step-by-Step Guide to Mastering Laravel Authorization
Laravel 12 Import and Export CSV and Excel File
Laravel Pagination for Beginners
Bonus: How to Check if a User is Logged In?
@if (Auth::check()) <p>Hello, {{ Auth::user()->name }}!</p> @else <p>Please <a href="/login">log in</a></p> @endif
What If You Want to Change the Home Page?
You can change that in app/Providers/RouteServiceProvider.php:
public const HOME = '/dashboard';
Wrap Up: What Did You Learn?
Authentication means checking who someone is
Laravel UI makes auth pages super easy
You can register, log in, and protect pages
You learned about sessions, middleware, and routes
Next Steps
– Add a forgot password link (it’s already built-in!)
– Add email verification
– Create a profile page for users