This project is a Laravel 12 API for User Registration, Login, and Logout using Sanctum tokens, with unified JSON responses.
- PHP >= 8.1
- Laravel 12
- MySQL or any supported database
- Composer
- Postman (for testing API endpoints)
git clone <your-repo-url>
cd <project-folder>
composer install
cp .env.example .env
php artisan key:generate
php artisan migrate
php artisan servePOST /api/register
Request Body:
{
"name": "John Doe",
"email": "john@example.com",
"password": "password123",
"password_confirmation": "password123"
}Success Response (201):
{
"status": "success",
"message": "User registered successfully.",
"data": {
"user": {
"id": 1,
"name": "John Doe",
"email": "john@example.com"
},
"token": "1|abc123..."
}
}Error Response (Validation failed, 422):
{
"status": "error",
"message": "Validation failed.",
"errors": {
"email": ["This email is already registered."],
"password": ["Password confirmation does not match."]
}
}POST /api/login
Request Body:
{
"email": "john@example.com",
"password": "password123"
}Success Response (200):
{
"status": "success",
"message": "Login successful.",
"data": {
"access_token": "1|abc123..."
}
}Error Response (Invalid credentials, 401):
{
"status": "error",
"message": "Invalid Credentials",
"errors": null
}POST /api/logout
Requires Authentication (auth:sanctum)
Success Response (200):
{
"status": "success",
"message": "Logged out successfully.",
"data": null
}Error Response (Unauthenticated, 401):
{
"status": "error",
"message": "Unauthenticated",
"errors": null
}