Skip to content

history-coding-account-ehab-elshimi/my-helper-code

Repository files navigation

Laravel Auth API

This project is a Laravel 12 API for User Registration, Login, and Logout using Sanctum tokens, with unified JSON responses.


Requirements

  • PHP >= 8.1
  • Laravel 12
  • MySQL or any supported database
  • Composer
  • Postman (for testing API endpoints)

Installation

git clone <your-repo-url>
cd <project-folder>
composer install
cp .env.example .env
php artisan key:generate
php artisan migrate
php artisan serve

API Endpoints

1️⃣ Register

POST /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."]
  }
}

2️⃣ Login

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
}

3️⃣ Logout

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
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages