A Laravel 11 RESTful API for managing tasks with secure user authentication using Laravel Sanctum.
This API provides a complete task management system where users can register, authenticate, and manage their personal tasks. Each user has isolated access to only their own tasks, ensuring data privacy and security.
✅ User - name, email, password
✅ Task - title, description, status (pending/in-progress/completed), user_id
✅ User registration and login with Laravel Sanctum authentication
✅ Complete CRUD operations for tasks
✅ Task access restricted to authenticated users only
✅ Comprehensive input validation
✅ Proper error handling with meaningful messages
✅ Filter tasks by status (pending, in-progress, completed)
✅ Pagination on task listings (10 items per page)
- Framework: Laravel 11
- Authentication: Laravel Sanctum (Token-based)
- Database: MySQL
- PHP: 8.2+
- PHP 8.2 or higher
- Composer
- MySQL database
- Git
- Clone the repository
git clone <your-repository-url>
cd task-api- Install dependencies
composer install- Configure environment
cp .env.example .env- Update database credentials in
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=task_api
DB_USERNAME=root
DB_PASSWORD=- Generate application key
php artisan key:generate-
Create the database
Create a MySQL database named
task_api -
Run database migrations
php artisan migrate- Start the development server
php artisan serveThe API will be accessible at http://127.0.0.1:8000
http://127.0.0.1:8000/api
POST /api/register
Content-Type: application/json
{
"name": "John Doe",
"email": "john@example.com",
"password": "password123",
"password_confirmation": "password123"
}Response (201):
{
"access_token": "4|w5IB1CkX7Bv07RD85rTfEZouz6j3LaRnUaYX7ET853e168f5",
"token_type": "Bearer",
"user": {
"id": 4,
"name": "John Doe",
"email": "john@example.com",
"created_at": "2025-11-08T04:29:53.000000Z",
"updated_at": "2025-11-08T04:29:53.000000Z"
}
}POST /api/login
Content-Type: application/json
{
"email": "john@example.com",
"password": "password123"
}Response (200):
{
"access_token": "5|zZO9BJ0rokUrkUNcmGuZgP7WX4qSFrU2eUoAcnlH7dde06ea",
"token_type": "Bearer",
"user": {
"id": 4,
"name": "John Doe",
"email": "john@example.com"
}
}POST /api/logout
Authorization: Bearer {token}Response (200):
{
"message": "Logged out successfully"
}All task endpoints require authentication. Include the Bearer token in the Authorization header.
GET /api/tasks
Authorization: Bearer {token}Response (200):
{
"current_page": 1,
"data": [
{
"id": 1,
"user_id": 4,
"title": "Complete Laravel Assessment",
"description": "Build task management API",
"status": "in-progress",
"created_at": "2025-11-08T04:12:01.000000Z",
"updated_at": "2025-11-08T04:12:01.000000Z"
}
],
"per_page": 10,
"total": 1
}GET /api/tasks?status=pending
Authorization: Bearer {token}Available status values: pending, in-progress, completed
POST /api/tasks
Authorization: Bearer {token}
Content-Type: application/json
{
"title": "Complete project documentation",
"description": "Write comprehensive API docs",
"status": "pending"
}Response (201):
{
"id": 1,
"user_id": 4,
"title": "Complete project documentation",
"description": "Write comprehensive API docs",
"status": "pending",
"created_at": "2025-11-08T04:12:01.000000Z",
"updated_at": "2025-11-08T04:12:01.000000Z"
}GET /api/tasks/{id}
Authorization: Bearer {token}PUT /api/tasks/{id}
Authorization: Bearer {token}
Content-Type: application/json
{
"status": "completed"
}DELETE /api/tasks/{id}
Authorization: Bearer {token}Response (200):
{
"message": "Task deleted successfully"
}{
"message": "The given data was invalid.",
"errors": {
"email": ["The email field is required."],
"password": ["The password must be at least 8 characters."]
}
}{
"message": "Unauthenticated."
}{
"message": "Unauthorized"
}- id (Primary Key)
- name (varchar)
- email (varchar, unique)
- password (varchar, hashed)
- created_at, updated_at (timestamps)
- id (Primary Key)
- user_id (Foreign Key → users.id, cascade on delete)
- title (varchar, required)
- description (text, nullable)
- status (enum: pending, in-progress, completed)
- created_at, updated_at (timestamps)
- Indexes: user_id, status, composite(user_id, status)
- MVC Pattern: Clear separation between Models, Controllers, and business logic
- RESTful Design: Follows REST principles for intuitive API structure
- Policy-Based Authorization: Users can only access their own tasks
- Password hashing using bcrypt
- Token-based authentication with Laravel Sanctum
- SQL injection protection via Eloquent ORM
- Input validation on all endpoints
- User-specific data isolation
- Followed Laravel coding standards and best practices
- Clean, readable code with proper naming conventions
- Comprehensive error handling
- Proper HTTP status codes for all responses
- Register a new user via
POST /api/register - Copy the access_token from the response
- Add Authorization header to all task requests:
Authorization: Bearer {your_access_token}
- Test all CRUD operations on tasks
Register:
curl -X POST http://127.0.0.1:8000/api/register \
-H "Content-Type: application/json" \
-d '{"name":"Test User","email":"test@example.com","password":"password123","password_confirmation":"password123"}'Login:
curl -X POST http://127.0.0.1:8000/api/login \
-H "Content-Type: application/json" \
-d '{"email":"test@example.com","password":"password123"}'Get Tasks:
curl -X GET http://127.0.0.1:8000/api/tasks \
-H "Authorization: Bearer {your_token}"task-api/
├── app/
│ ├── Http/Controllers/Api/
│ │ ├── AuthController.php
│ │ └── TaskController.php
│ ├── Models/
│ │ ├── User.php
│ │ └── Task.php
│ └── Policies/
│ └── TaskPolicy.php
├── database/migrations/
├── routes/api.php
└── README.md
✅ User registration with email validation
✅ Secure login with credential verification
✅ Token-based authentication (stateless)
✅ Complete task CRUD operations
✅ Task filtering by status (Bonus)
✅ Pagination support (Bonus)
✅ User-specific task access
✅ Comprehensive validation
✅ Proper error handling
✅ Clean, maintainable code
This API demonstrates proficiency in:
- Laravel framework fundamentals
- RESTful API design principles
- Authentication and authorization
- Database design and relationships
- Input validation and error handling
- Security best practices
The implementation is production-ready with proper validation, error handling, and security measures in place.
MIT License
Developed by holyprof for Afrifounders Startup Studio - Laravel Developer Assessment