Advanced LaravelPro· 35 min read

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.

Laravel Breeze installs full auth
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:

Access the current user
// the logged-in user, anywhere
$user = auth()->user();
echo $user->name;

// in Blade
@auth
  <p>Welcome, {{ auth()->user()->name }}</p>
@endauth

auth()->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:

  1. Install a starter kit (Breeze) and run php artisan migrate to create the users table.
  2. A visitor registers — Laravel securely hashes their password and stores the new user.
  3. They log in — Laravel checks the password and starts a session that remembers them.
  4. Protect private pages with the auth middleware so only logged-in users can reach them.
  5. Anywhere in your app, read the current user with auth()->user() (or @auth in 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?

Answer: auth()->user() returns the authenticated user (or null). Blade has @auth and auth()->user() too.

✍️ Practice

  1. Install a starter kit (Breeze) and get register/login working.
  2. Protect a dashboard page and greet the logged-in user.

🏠 Homework

  1. Add login to one of your CRUD apps and restrict editing to logged-in users.
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →