Skip to content

Repository files navigation

HybridgeCommunityBackend - JWT Authentication API

REST API developed in PHP with JWT authentication for user and board management.

Features

  • JWT Authentication (Access + Refresh Tokens)
  • Password hashing with bcrypt
  • User CRUD operations
  • Board CRUD operations
  • Protected routes with middleware
  • Input validation
  • MariaDB database
  • Docker for development
  • Comprehensive documentation

Requirements

  • PHP 8.x
  • Composer
  • Docker Desktop (for database)
  • MariaDB 11.5

Installation

1. Clone Repository

git clone https://github.com/Ariff-dev/HybridgeCommunityBackend.git
cd HybridgeCommunityBackend

2. Install Dependencies

composer install

3. Configure Environment Variables

cp .env.example .env

Edit .env with your credentials:

DB_USER=your_user
DB_PASS=your_password
DB_NAME=hybridge_community
DB_HOST=localhost
DB_ROOT_PASSWORD=your_root_password

JWT_SECRET=generate-a-secure-random-secret
JWT_ACCESS_TOKEN_EXPIRY=900
JWT_REFRESH_TOKEN_EXPIRY=604800

API_DEBUG=true

Generate JWT Secret:

php -r "echo bin2hex(random_bytes(32));"

4. Start Database

docker-compose up -d

5. Import Database Schema

Connect to database (port 2025) and import database_schema.sql:

mysql -h 127.0.0.1 -P 2025 -u your_user -p your_database < database_schema.sql

Or use DBeaver/phpMyAdmin.

6. Start PHP Server

php -S localhost:8000 -t public

API will be available at: http://localhost:8000/api


Documentation


Main Endpoints

Authentication (Public)

POST /api/auth/register  # Register user
POST /api/auth/login     # Login
POST /api/auth/refresh   # Refresh access token

Protected (Require JWT)

GET  /api/auth/me        # Current user
POST /api/auth/logout    # Logout

GET  /api/users          # List users
GET  /api/board          # List board items
POST /api/board          # Create item
PUT  /api/board          # Update item

Quick Testing

1. Register User

curl -X POST http://localhost:8000/api/auth/register \
  -H "Content-Type: application/json" \
  -d '{
    "name": "John Doe",
    "email": "john@example.com",
    "password": "password123"
  }'

2. Login

curl -X POST http://localhost:8000/api/auth/login \
  -H "Content-Type: application/json" \
  -d '{
    "email": "john@example.com",
    "password": "password123"
  }'

Save the access_token from response.

3. Access Protected Route

curl -X GET http://localhost:8000/api/auth/me \
  -H "Authorization: Bearer YOUR_ACCESS_TOKEN"

Project Structure

HybridgeCommunityBackend/
├── public/
│   └── index.php              # Entry point
├── src/
│   ├── Auth/
│   │   └── JwtHandler.php     # JWT management
│   ├── Config/
│   │   └── database.php       # Database connection
│   ├── Controllers/
│   │   ├── AuthController.php # Authentication
│   │   ├── UserController.php # Users
│   │   └── BoardController.php # Boards
│   ├── Models/
│   │   ├── UserModel.php
│   │   ├── BoardItem.php
│   │   └── RefreshTokenModel.php
│   ├── Middlewares/
│   │   ├── JwtAuth.php        # JWT middleware
│   │   └── ApiKeyAuth.php     # API Keys (legacy)
│   ├── Helpers/
│   │   └── Uuid.php
│   └── Routes/
│       └── api.php            # Route definitions
├── vendor/                    # Composer dependencies
├── .env                       # Environment variables
├── composer.json
├── docker-compose.yml
├── database_schema.sql        # Database schema
├── API_DOCUMENTATION.md
├── REACT_INTEGRATION.md
└── DEPLOYMENT.md

Security

  • Passwords hashed with bcrypt
  • JWT tokens with expiration
  • Revocable refresh tokens
  • Prepared statements (SQL injection prevention)
  • Input validation
  • Configurable CORS headers

Deployment

See DEPLOYMENT.md for detailed deployment instructions on shared hosting.

Summary:

  1. composer install --no-dev
  2. Configure production .env
  3. Import SQL schema
  4. Upload files to hosting
  5. Configure .htaccess

Technology Stack

  • PHP 8.x - Backend language
  • MariaDB 11.5 - Database
  • Firebase PHP-JWT - JWT token management
  • vlucas/phpdotenv - Environment variables
  • Docker - Database container
  • PDO - Database abstraction layer

Notes

  • Access tokens expire in 15 minutes
  • Refresh tokens expire in 7 days
  • Database port: 2025 (development)
  • Set API_DEBUG=false in production

React Integration

See REACT_INTEGRATION.md for:

  • Complete AuthContext
  • Automatic token refresh handling
  • Example components (Login, Register, Dashboard)
  • Protected Routes

Support

For issues or questions:

  1. Review documentation
  2. Check PHP logs
  3. Verify dependencies are installed
  4. Confirm .env is configured correctly

License

This project is open source.


Next Steps

  • Add unit tests (PHPUnit)
  • Implement rate limiting
  • Add pagination to listings
  • Implement DELETE method for boards
  • Add advanced roles and permissions
  • OpenAPI/Swagger documentation

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages