Authentication
Add full login, registration and password handling — often in minutes with a starter kit.
What you will learn
- Add authentication to a Laravel app
- Protect pages for logged-in users
- Access the current user
Login, the easy way
Building login by hand in raw PHP was a lot of work (sessions, hashing). Laravel gives you complete, secure authentication via a starter kit — registration, login, password reset, all built.
composer require laravel/breeze --dev
php artisan breeze:install
php artisan migrate
# You now have full register/login/logout pages!Those three commands give you a complete login system. composer require laravel/breeze --dev downloads the Breeze starter kit. php artisan breeze:install generates the register, login, logout and password-reset pages plus their routes and controllers. php artisan migrate creates the users table those pages need. After this you genuinely have working sign-up and sign-in screens — without writing the auth code yourself.
Once users can log in, you often need the current user. Laravel gives you a helper for that, both in PHP and in Blade:
// the logged-in user, anywhere
$user = auth()->user();
echo $user->name;
// in Blade
@auth
<p>Welcome, {{ auth()->user()->name }}</p>
@endauthauth()->user() returns the person who is currently logged in (or null if nobody is), so $user->name prints their name. In Blade, the @auth ... @endauth block shows its contents only when someone is logged in — handy for a "Welcome, Asha" greeting that hides itself for guests.
How authentication works once a starter kit is installed:
- Install a starter kit (Breeze) and run
php artisan migrateto create theuserstable. - A visitor registers — Laravel securely hashes their password and stores the new user.
- They log in — Laravel checks the password and starts a session that remembers them.
- Protect private pages with the
authmiddleware so only logged-in users can reach them. - Anywhere in your app, read the current user with
auth()->user()(or@authin Blade).
Tip: Compare this to the manual sessions + password hashing you would write in raw PHP. Laravel handles secure password hashing, sessions and protection for you — safely.
Q. How do you access the currently logged-in user in Laravel?
✍️ Practice
- Install a starter kit (Breeze) and get register/login working.
- Protect a dashboard page and greet the logged-in user.
🏠 Homework
- Add login to one of your CRUD apps and restrict editing to logged-in users.