Skip to content

Latest commit

 

History

History
executable file
·
245 lines (202 loc) · 7.95 KB

File metadata and controls

executable file
·
245 lines (202 loc) · 7.95 KB

User Profile Management Implementation

Overview

This implementation adds comprehensive user profile management functionality to the Laravel social network application using Livewire components.

Features Implemented

1. Profile Viewing

  • Users can view their own profile at /my-profile
  • Users can view other users' profiles at /users/{userId}/profile
  • Profile displays:
    • Profile photo
    • Name and email
    • Bio
    • Location, website, gender, and birth date (if available)
    • Friend/follower statistics

2. Profile Editing

  • Users can edit their profile at /my-profile/edit
  • Editable fields:
    • Name (required)
    • Email (required, must be valid email)
    • Bio (optional, max 1000 characters)
    • Location (optional)
    • Website (optional, must be valid URL)
    • Gender (optional: male, female, other, prefer not to say)
    • Birth date (optional, must be in the past)
    • Profile photo (optional, image only, max 2MB)

3. Profile Photo Management

  • Upload new profile photo
  • Delete existing profile photo
  • Preview profile photo before saving
  • Automatic photo validation (image type, max size 2MB)

Technical Implementation

Database Changes

  1. Existing Migration: 2026_02_15_210000_add_bio_to_users_table.php

    • Adds bio column to users table (already exists)
    • Bio is stored in the users table, not the profiles table
  2. Models Updated:

    • User.php: Added bio to fillable array, added profile() relationship
    • Profile.php: No changes needed (bio not stored here)

Livewire Components

  1. ShowProfile (app/Http/Livewire/ShowProfile.php)

    • Displays user profile information
    • Automatically creates profile if it doesn't exist
    • Shows "Edit Profile" button for own profile
  2. EditProfile (app/Http/Livewire/EditProfile.php)

    • Handles profile editing with validation
    • Supports profile photo upload and deletion
    • Real-time validation with error messages
    • Redirects to profile view after successful save

Views

  1. Livewire Views:

    • resources/views/livewire/show-profile.blade.php: Profile display
    • resources/views/livewire/edit-profile.blade.php: Profile editing form
  2. Wrapper Views:

    • resources/views/user-profile/show.blade.php: Profile page wrapper
    • resources/views/profile/edit.blade.php: Edit page wrapper

Routes

Added to routes/web.php (protected by auth middleware):

Route::get('/my-profile', ...)->name('user-profile.show');
Route::get('/my-profile/edit', ...)->name('user-profile.edit');
Route::get('/users/{userId}/profile', ...)->name('user-profile.view');

Tests

Created comprehensive test suite in tests/Feature/ProfileTest.php with 25 test cases covering:

  • Profile viewing functionality
  • Profile editing functionality
  • Validation rules
  • Photo upload and deletion
  • Access control
  • Data persistence

Manual Testing Instructions

Prerequisites

  1. Install dependencies: composer install
  2. Set up database:
    php artisan migrate:fresh --seed
  3. Generate application key: php artisan key:generate
  4. Link storage: php artisan storage:link
  5. Start the development server: php artisan serve

Test Cases

1. View Own Profile

  1. Log in to the application
  2. Navigate to /my-profile
  3. Verify you see:
    • Your profile photo
    • Your name and email
    • Edit Profile button
    • Bio (if set)
    • Profile details (location, website, etc.)
    • Friend/follower statistics

2. Edit Profile Information

  1. From your profile, click "Edit Profile"
  2. Update the following fields:
    • Name: "Updated Name"
    • Bio: "This is my new bio"
    • Location: "San Francisco, CA"
    • Website: "https://example.com"
    • Gender: Select an option
    • Birth date: Select a past date
  3. Click "Save Changes"
  4. Verify you're redirected to your profile
  5. Verify all changes are displayed correctly

3. Upload Profile Photo

  1. Go to profile edit page
  2. Click "Select New Photo"
  3. Choose an image file (JPG/PNG, under 2MB)
  4. Verify preview shows the new photo
  5. Click "Save Changes"
  6. Verify the new photo appears on your profile

4. Delete Profile Photo

  1. Go to profile edit page (with a photo uploaded)
  2. Click "Remove Photo" button
  3. Verify the photo reverts to default avatar
  4. Verify the change is saved

5. Validation Testing

Test each validation rule:

  • Try to save with empty name (should show error)
  • Try to save with invalid email (should show error)
  • Try to save bio with >1000 characters (should show error)
  • Try to upload non-image file as profile photo (should show error)
  • Try to upload very large image >2MB (should show error)
  • Try to set future birth date (should show error)

6. View Other User's Profile

  1. Get another user's ID from the database
  2. Navigate to /users/{userId}/profile
  3. Verify you see their profile information
  4. Verify you do NOT see the "Edit Profile" button

7. Access Control

  1. Log out of the application
  2. Try to access /my-profile (should redirect to login)
  3. Try to access /my-profile/edit (should redirect to login)

Automated Tests

Run the test suite:

php artisan test --filter=ProfileTest

This will execute 25 test cases covering all functionality.

Acceptance Criteria Verification

Users can view their profile information

  • Implemented in ShowProfile component
  • Route: /my-profile

Users can edit their name, email, bio, and other details

  • Implemented in EditProfile component
  • Route: /my-profile/edit
  • All fields editable with proper validation

Users can upload and change their profile picture

  • Photo upload functionality in EditProfile
  • Photo deletion functionality included
  • Validation for image type and size

Profile changes are reflected immediately across the platform

  • Livewire provides real-time updates
  • Changes persist to database
  • Profile relationship ensures data consistency

Additional Features

Beyond the requirements, this implementation includes:

  • Comprehensive validation for all fields
  • User-friendly error messages
  • Profile auto-creation on first view
  • Responsive design using Tailwind CSS
  • SVG icons for profile details
  • Friend/follower statistics display
  • Support for viewing other users' profiles

Security Considerations

  1. Authentication: All routes protected by auth:sanctum middleware
  2. Validation: All inputs validated server-side
  3. File Upload Security:
    • Only image files allowed
    • File size limited to 2MB
    • Uses Laravel's built-in file upload handling
  4. XSS Protection: Blade templating escapes output by default
  5. CSRF Protection: Forms protected by Laravel's CSRF middleware

Future Enhancements

Potential improvements for future iterations:

  1. Profile privacy settings (who can view profile)
  2. Cover photo upload
  3. Profile completion percentage indicator
  4. Social media links section
  5. Profile activity timeline
  6. Email change verification
  7. Profile field visibility controls
  8. Profile badge/verification system

Files Changed

Created Files

  • app/Http/Livewire/ShowProfile.php
  • app/Http/Livewire/EditProfile.php
  • resources/views/livewire/show-profile.blade.php
  • resources/views/livewire/edit-profile.blade.php
  • resources/views/user-profile/show.blade.php
  • resources/views/profile/edit.blade.php
  • tests/Feature/ProfileTest.php
  • PROFILE_IMPLEMENTATION.md (this file)

Modified Files

  • app/Models/User.php (added bio to fillable, added profile relationship)
  • routes/web.php (added profile routes)

Notes

  1. The implementation uses Laravel Jetstream's existing profile photo functionality
  2. Bio field is stored in the users table (added by migration 2026_02_15_210000_add_bio_to_users_table.php)
  3. Profile is automatically created when first accessed if it doesn't exist
  4. All routes are protected by authentication middleware
  5. The implementation follows Laravel and Livewire best practices
  6. Code review completed with all issues addressed
  7. Security scan passed with no vulnerabilities detected