Git Product home page Git Product logo

task-backend's Introduction

Task Backend

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.

Overview

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.

Features

  • 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.

Installation

To get started, follow the Installation and Database Setup sections in the documentation.

API Endpoints

Detailed information about available API endpoints can be found in the API Endpoints section. It includes examples of requests and responses for each endpoint.

Table of Contents

Overview

Provide a concise overview of your project, its purpose, and any key features. Mention the technologies used, if applicable.

Installation

  1. Clone the repository:

    git clone https://github.com/fathah/task-backend.git
  2. Install dependencies using Composer:

    composer install
  3. Set up your database. (Refer to the Database Setup section)

  4. Configure Environment Variables.

  5. Configure your API by editing the necessary configuration files.

Environment Config

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"

Database Setup

  • Setup a MySQL database.
  • Import (schema.sql) SQL to the database.
  • Configure the credentials .env file.

API Endpoints

Setup the local/prod environement and access the api through /api route.

http(s)://{baseurl}/v1/{api_endpoint}

Authentication

  • Register User
    • Endpoint: /api/auth/register.php
    • Method: POST
    • Description: Register a new user.
    • Request Body:
      {
        "email": "[email protected]",
        "password": "userpassword",
        "role": "user"
      }
  • Login
    • Endpoint: /api/auth/login.php
    • Method: POST
    • Description: Authenticate and get a JWT token.
    • Request Body:
      {
        "email": "[email protected]",
        "password": "userpassword"
      }

User Profile

  • Get User Profile
    • Endpoint: /api/user/profile.php
    • Method: GET
    • Description: Get user profile information.
    • Authentication: Bearer Token

Token Refresh

  • Refresh Token
    • Endpoint: /api/auth/refresh-token.php
    • Method: POST
    • Description: Refresh an expired JWT token.
    • Authentication: Bearer Token

Testing

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

Running Tests

  1. Open a terminal window and navigate to the project root directory.
  2. Run the PHPUnit tests:
vendor/bin/phpunit

This command will execute all the test cases in the tests directory.

Test Cases

Authentication Endpoint

vendor/bin/phpunit v1/tests/EndpointTest.php --filter testAuthenticationEndpoint

Registration Endpoint

vendor/bin/phpunit v1/tests/EndpointTest.php --filter testRegistrationEndpoint

Token Refresh Endpoint

vendor/bin/phpunit v1/tests/EndpointTest.php --filter testTokenRefreshEndpoint

User Profile Endpoint

vendor/bin/phpunit v1/tests/EndpointTest.php --filter testUserProfileEndpoint

Folder Structure

- /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

Disclaimer

This project is made for the interview task for Smucci Vapes, UAE

task-backend's People

Contributors

fathah avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.