Task Backend is a PHP-based project that provides essential user authentication and authorization functionalities. It includes user registration, login, token refresh, authentication, and user profile retrieval.
The project is designed to serve as a backend for applications requiring user management. It leverages core PHP principles, object-oriented programming, and secure authentication practices. The implementation follows industry-standard security practices to ensure the protection of user data.
-
User Registration: Endpoint for registering new users. Passwords are securely hashed before storage.
-
Login: Authentication endpoint that validates user credentials and issues a JSON Web Token (JWT) upon successful login.
-
Token Refresh: Mechanism for refreshing JWT tokens without re-authentication, addressing token expiration.
-
User Authentication: Endpoint for validating user credentials against the database, ensuring secure access to protected resources.
-
User Profile Retrieval: Endpoint to fetch user profile information using JWT authentication.
To get started, follow the Installation and Database Setup sections in the documentation.
Detailed information about available API endpoints can be found in the API Endpoints section. It includes examples of requests and responses for each endpoint.
Provide a concise overview of your project, its purpose, and any key features. Mention the technologies used, if applicable.
-
Clone the repository:
git clone https://github.com/fathah/task-backend.git
-
Install dependencies using Composer:
composer install
-
Set up your database. (Refer to the Database Setup section)
-
Configure Environment Variables.
-
Configure your API by editing the necessary configuration files.
Secrets are stored in a .env
file. Configure the variables as per the requirements. Following are the required variables for this project.
DB_HOST = "db_host"
DB_USER = "db_user"
DB_PASSWORD = "db_pass"
DB_DATABASE = "task_db"
JWT_SECRET = "jwt_secret_code"
- Setup a MySQL database.
- Import (schema.sql) SQL to the database.
- Configure the credentials
.env
file.
Setup the local/prod environement and access the api through /api
route.
http(s)://{baseurl}/v1/{api_endpoint}
- Register User
- Endpoint:
/api/auth/register.php
- Method:
POST
- Description: Register a new user.
- Request Body:
{ "email": "[email protected]", "password": "userpassword", "role": "user" }
- Endpoint:
- Login
- Endpoint:
/api/auth/login.php
- Method:
POST
- Description: Authenticate and get a JWT token.
- Request Body:
{ "email": "[email protected]", "password": "userpassword" }
- Endpoint:
- Get User Profile
- Endpoint:
/api/user/profile.php
- Method:
GET
- Description: Get user profile information.
- Authentication: Bearer Token
- Endpoint:
- Refresh Token
- Endpoint:
/api/auth/refresh-token.php
- Method:
POST
- Description: Refresh an expired JWT token.
- Authentication: Bearer Token
- Endpoint:
To ensure the correctness of your application, you can run the provided PHPUnit tests. Make sure you have PHPUnit installed. If not, you can install it using Composer:
composer require --dev phpunit/phpunit
- Open a terminal window and navigate to the project root directory.
- Run the PHPUnit tests:
vendor/bin/phpunit
This command will execute all the test cases in the tests directory.
vendor/bin/phpunit v1/tests/EndpointTest.php --filter testAuthenticationEndpoint
vendor/bin/phpunit v1/tests/EndpointTest.php --filter testRegistrationEndpoint
vendor/bin/phpunit v1/tests/EndpointTest.php --filter testTokenRefreshEndpoint
vendor/bin/phpunit v1/tests/EndpointTest.php --filter testUserProfileEndpoint
- /v1/api # API Entry Directory
- /v1/src # Source code files
- /v1/tests # Unit tests
- /vendor # Composer Packages
- .env # Environment variables (gitignored)
- .gitignore # Gitignore file
- README.md # Project documentation
- composer.json # Composer configuration
- packages.php # Packages Inclusion Header File
This project is made for the interview task for Smucci Vapes, UAE