https://github.com/qaiswardag/php-rest-api-with-token-authentication-for-tasks-management
PHP REST API with token authentication for Tasks Management application.
https://github.com/qaiswardag/php-rest-api-with-token-authentication-for-tasks-management
authentication javascript php rest-api
Last synced: 6 months ago
JSON representation
PHP REST API with token authentication for Tasks Management application.
- Host: GitHub
- URL: https://github.com/qaiswardag/php-rest-api-with-token-authentication-for-tasks-management
- Owner: qaiswardag
- Created: 2022-03-15T19:35:29.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-10-24T18:56:42.000Z (over 1 year ago)
- Last Synced: 2025-05-15T14:14:28.217Z (about 1 year ago)
- Topics: authentication, javascript, php, rest-api
- Language: PHP
- Homepage:
- Size: 47.9 KB
- Stars: 3
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# PHP REST API with Token Authentication for Tasks Management
This repository contains a PHP-based REST API with token authentication, designed to manage tasks. It follows a token-based authentication method to ensure secure access to the API endpoints.
## Features
- **Token-based Authentication:** Secure authentication mechanism using tokens.
- **Task Management:** Create, update, and delete tasks through API requests.
- **Error Handling:** Custom error messages and HTTP status codes for better debugging and client interaction.
- **Preflight Requests (CORS):** Handles preflight `OPTIONS` requests for cross-origin resource sharing (CORS).
## Prerequisites
- **PHP 7.2+**
- **MySQL/MariaDB** for the database
- **Composer** to manage dependencies
- A web server like Apache or Nginx
## Setup
1. Clone the repository:
```bash
git clone https://github.com/your-username/php-rest-api-with-token-authentication-for-tasks-management.git
```
2. Install dependencies:
```bash
composer install
```
3. Set up the database:
Import the provided SQL file to create the necessary tables in your database.
```bash
mysql -u username -p database_name < path/to/sqlfile.sql
```
4. Configure the database connection in `db.php`:
```php
setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
self::$writeDBConnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
return self::$writeDBConnection;
}
}
```
5. Run the API on your local server or configure it on a production server.
## Endpoints
### User Registration
- **URL:** `/register`
- **Method:** `POST`
- **Request Body:** JSON
```json
{
"fullname": "John Doe",
"username": "john123",
"password": "yourPassword"
}
```
- **Response:**
- Success: `201 Created`
- Error: `400 Bad Request` | `409 Conflict` (if username exists)
```php
fullname, $jsonData->username, $jsonData->password)) {
$fullname = trim($jsonData->fullname);
$username = trim($jsonData->username);
$password = password_hash($jsonData->password, PASSWORD_DEFAULT);
try {
$query = $writeDB->prepare('SELECT id FROM tblusers WHERE username = :username');
$query->bindParam(':username', $username, PDO::PARAM_STR);
$query->execute();
if ($query->rowCount() > 0) {
$response = new Response();
$response->setHttpStatusCode(409);
$response->setSuccess(false);
$response->addMessage('Username already exists');
$response->send();
} else {
$query = $writeDB->prepare('INSERT INTO tblusers (fullname, username, password) VALUES (:fullname, :username, :password)');
$query->bindParam(':fullname', $fullname, PDO::PARAM_STR);
$query->bindParam(':username', $username, PDO::PARAM_STR);
$query->bindParam(':password', $password, PDO::PARAM_STR);
$query->execute();
$response = new Response();
$response->setHttpStatusCode(201);
$response->setSuccess(true);
$response->addMessage('User created successfully');
$response->send();
}
} catch (PDOException $ex) {
error_log('Database error: ' . $ex);
$response = new Response();
$response->setHttpStatusCode(500);
$response->setSuccess(false);
$response->addMessage('Internal server error');
$response->send();
}
} else {
$response = new Response();
$response->setHttpStatusCode(400);
$response->setSuccess(false);
$response->addMessage('Incomplete user data');
$response->send();
}
}
}