🔐 Week 17, Day 1: Authentication Introduction & Setup

Learn Laravel Authentication with Breeze/UI

📖 Learning Objectives

🎯 What is Authentication?

Authentication is the process of verifying the identity of a user. It ensures that users are who they claim to be before granting access to protected resources.

Key Authentication Concepts:

Authentication vs Authorization:

Authentication Authorization
Who are you? What can you do?
Verifies identity Verifies permissions
Login/Register Roles/Permissions
Example: User login Example: Admin access

🚀 Laravel Authentication Options

1. Laravel Breeze (Recommended for Beginners)

Laravel Breeze is a minimal, simple implementation of all Laravel's authentication features including login, registration, password reset, email verification, and password confirmation.

Features:

2. Laravel UI (Bootstrap/Vue/React)

Laravel UI provides Bootstrap scaffolding for authentication. Good if you prefer Bootstrap over Tailwind.

3. Laravel Jetstream (Advanced)

Jetstream includes more advanced features like team management, two-factor authentication, and API tokens.

💡 Tip: For this bootcamp, we'll use Laravel Breeze because it's simple, modern, and easy to understand.

📦 Installing Laravel Breeze

Step 1: Create a New Laravel Project

# Create new Laravel project composer create-project laravel/laravel auth-app # Navigate to project cd auth-app

Step 2: Install Laravel Breeze

# Install Breeze via Composer composer require laravel/breeze --dev

Step 3: Install Breeze Scaffolding

# Install Breeze with Blade templates php artisan breeze:install blade # Install NPM dependencies npm install # Compile assets npm run dev

What does breeze:install do?

  • Creates authentication routes in routes/auth.php
  • Generates authentication controllers
  • Creates Blade views for login, register, etc.
  • Installs Tailwind CSS
  • Sets up authentication middleware

Step 4: Configure Database

# Edit .env file DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=auth_app DB_USERNAME=root DB_PASSWORD=
💡 Tip: Make sure your XAMPP MySQL is running and create a database named "auth_app" in phpMyAdmin.

Step 5: Run Migrations

# Create authentication database tables php artisan migrate

Tables Created:

  • users: Stores user accounts
  • password_reset_tokens: For password recovery
  • sessions: Manages user sessions
  • migrations: Tracks migration history

Step 6: Start Development Server

# Start Laravel server php artisan serve # In another terminal, compile assets npm run dev
💡 Tip: Keep both terminals running - one for Laravel, one for Vite (asset compilation).

🔍 Exploring Authentication Files

1. Authentication Routes (routes/auth.php)

use App\Http\Controllers\Auth\AuthenticatedSessionController; use App\Http\Controllers\Auth\RegisteredUserController; use Illuminate\Support\Facades\Route; // Registration routes Route::get('/register', [RegisteredUserController::class, 'create']) ->name('register'); Route::post('/register', [RegisteredUserController::class, 'store']); // Login routes Route::get('/login', [AuthenticatedSessionController::class, 'create']) ->name('login'); Route::post('/login', [AuthenticatedSessionController::class, 'store']); // Logout route Route::post('/logout', [AuthenticatedSessionController::class, 'destroy']) ->name('logout');

2. User Model (app/Models/User.php)

namespace App\Models; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; class User extends Authenticatable { use Notifiable; // Mass assignable attributes protected $fillable = [ 'name', 'email', 'password', ]; // Hidden attributes (not shown in JSON) protected $hidden = [ 'password', 'remember_token', ]; // Attribute casting protected $casts = [ 'email_verified_at' => 'datetime', 'password' => 'hashed', ]; }

3. Users Migration (database/migrations/xxxx_create_users_table.php)

public function up(): void { Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); }

4. Authentication Views (resources/views/auth/)

🎨 Testing the Authentication System

Visit Authentication Pages:

# Register page http://localhost:8000/register # Login page http://localhost:8000/login # Dashboard (requires login) http://localhost:8000/dashboard

Test Registration:

  1. Go to http://localhost:8000/register
  2. Fill in:
    • Name: John Doe
    • Email: john@example.com
    • Password: password123
    • Confirm Password: password123
  3. Click "Register"
  4. You should be redirected to the dashboard

Test Login:

  1. Logout from the dashboard
  2. Go to http://localhost:8000/login
  3. Enter credentials:
    • Email: john@example.com
    • Password: password123
  4. Check "Remember me" (optional)
  5. Click "Log in"
  6. You should be logged in and redirected to dashboard

🔐 How Laravel Handles Passwords

Password Hashing:

Laravel uses bcrypt hashing algorithm to securely store passwords. Passwords are never stored in plain text!

use Illuminate\Support\Facades\Hash; // Hash a password $hashedPassword = Hash::make('password123'); // Verify a password if (Hash::check('password123', $hashedPassword)) { // Password matches }

Password Validation Rules:

use Illuminate\Validation\Rules\Password; // Basic password rules 'password' => ['required', 'confirmed', Password::min(8)], // Strong password rules 'password' => [ 'required', 'confirmed', Password::min(8) ->letters() ->mixedCase() ->numbers() ->symbols() ],

📊 Database Tables Explained

1. Users Table Structure:

Column Type Description
id BIGINT Primary key
name VARCHAR(255) User's full name
email VARCHAR(255) Unique email address
email_verified_at TIMESTAMP Email verification time
password VARCHAR(255) Hashed password
remember_token VARCHAR(100) Remember me token
created_at TIMESTAMP Registration time
updated_at TIMESTAMP Last update time

2. Sessions Table:

Stores user session data for logged-in users.

3. Password Reset Tokens Table:

Stores tokens for password reset requests.

🛠️ Customizing Authentication Views

Login View (resources/views/auth/login.blade.php):

<x-guest-layout> <!-- Session Status --> <x-auth-session-status class="mb-4" :status="session('status')" /> <form method="POST" action="{{ route('login') }}"> @csrf <!-- Email Address --> <div> <x-input-label for="email" :value="__('Email')" /> <x-text-input id="email" class="block mt-1 w-full" type="email" name="email" :value="old('email')" required autofocus /> <x-input-error :messages="$errors->get('email')" class="mt-2" /> </div> <!-- Password --> <div class="mt-4"> <x-input-label for="password" :value="__('Password')" /> <x-text-input id="password" class="block mt-1 w-full" type="password" name="password" required /> <x-input-error :messages="$errors->get('password')" class="mt-2" /> </div> <!-- Remember Me --> <div class="block mt-4"> <label for="remember_me" class="inline-flex items-center"> <input id="remember_me" type="checkbox" name="remember"> <span class="ml-2">{{ __('Remember me') }}</span> </label> </div> <div class="flex items-center justify-end mt-4"> <x-primary-button> {{ __('Log in') }} </x-primary-button> </div> </form> </x-guest-layout>
💡 Tip: Breeze uses Blade components (<x-input-label>) for reusable UI elements.

✍️ Practice Exercises

Exercise 1: Install Laravel Breeze

  1. Create a new Laravel project named "student-auth"
  2. Install Laravel Breeze with Blade templates
  3. Configure database connection
  4. Run migrations
  5. Test registration and login

Exercise 2: Create Test Users

  1. Register 3 different users with different emails
  2. Login and logout with each user
  3. Check the users table in phpMyAdmin
  4. Note the hashed passwords

Exercise 3: Explore Authentication Files

  1. Open and read routes/auth.php
  2. Open app/Http/Controllers/Auth/ folder
  3. Explore RegisteredUserController.php
  4. Open resources/views/auth/ folder
  5. Customize the login page title

Exercise 4: Database Exploration

  1. Open phpMyAdmin
  2. Check the users table structure
  3. View registered users
  4. Check password_reset_tokens table
  5. Check sessions table

🎯 Today's Deliverable

✅ Authentication System Setup

Requirements:

  • Laravel project with Breeze installed
  • Database configured and migrated
  • Working registration system
  • Working login system
  • At least 2 test users created
  • Screenshot of dashboard after login

📚 Summary

🔜 What's Next?

In Day 2, we'll dive deeper into: