Skip to content

A secure and easy-to-use API for creating and authenticating users with JSON Web Tokens (JWTs).

Notifications You must be signed in to change notification settings

aytheotaku/jwt-auth-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

61 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JWT AUTH API

Interacting with this api, you are able to create and authenticate clients using Json Web Tokens which are needed to perform CRUD Operations on Transactions.

The API is live at https://jwtapi.ayomideakinbo.live/

This API has also been documented using Postman, find this documentation here at : https://documenter.getpostman.com/view/11792638/2s93eYUBzR

Please feel free to visit my Notion blog page where I elaborate on the project and share my thought process that informed its development. https://www.notion.so/JWT-Authentication-Api-a6bbb501f0784ce8a7b2461210ddd93d?pvs=4

Getting Started

To get started, clone this repository to your local machine and run npm install to install the required dependencies. You will also need to create a .env file in the root directory of the project following the format in the env_example file located in the root of this repository. You can then start the server by running npm start.


API Authentication

To access certain resources and perform certain operations, a client needs to be authenticated by being registering and logging in. Resources that require authentication will be tagged with 'Authentication-Needed' and will require you to send a request Authorization header with value of Bearer access_token

Register Client

POST /api/v1/register

The request body needs to be in JSON format and must include the following properties

  • firstName - String - Required
  • lastName - String - Required
  • email - String - Required
  • password - String - Required
  • repeatPassword - String -Required - Reference to password

Example Request

POST /api/v1/register
{
  "firstName": "Tobi",
  "lastName": "John",
  "email": "tobyjohn@gmail.com"
  "password": "randompasswordbelongingtotobyjohn",
  "repeatPassword": "randompasswordbelongingtotobyjohn"
}

Example, Successful Response

{
  "success": "true",
  "message": "User Created",
  "status": "201"
  "data" : {
    "firstName": "tobi",
    "lastName": "john",
    "email": "tobyjohn@gmail.com"
  }
}

Example, Failed Response

{
    "success": false,
    "message": "An error has occurred",
    "status": 409,
    "error": [
        {
            "message": "User Already Exists"
        }
    ]
}

Login Client

POST /api/v1/login

The request body needs to be in JSON format and must include the following properties

  • email - String - Required
  • password - String - Required

Example Request

POST /api/v1/login

{
  "email": "tobyjohn@gmail.com"
  "password": "randompasswordbelongingtotobyjohn"
}

Example Success Response

{
  "email": "tobyjohn@gmail.com"
  "access_token": "${jwt_token_provided}"
}

${jwt_token_provided} will be the actual jwt provided by in the response. This jwt will last for 1 day from the time of logging in.


Endpoints


Check Api Status

GET /api
Returns status of api.

Create Transaction

POST /api/v1/transactions
Creates a transaction. 'Authentication-Needed'

The request body needs to be in JSON format and must include the following properties

  • depositorName - String - Required
  • transactionAmount - Number - Required

Example Request

POST /api/v1/transactions
Authorization: Bearer <access_token>

{
  "depositorName": "Thomas Shelby"
  "transactionAmount": 2000
}

Example, Successful Response

{ 
  "success": "true",
  "message": "Transaction Created",
  "status:" 201
}

Example, Failed Response

{
    "success": "false",
    "message": "An error has occurred",
    "status": 401,
    "error": [
        {
            "message": "Unauthorized"
        }
    ]
}

Get Transactions

POST /api/v1/transactions
Returns all transactions. 'Authentication-Needed'

Get Transactions by Query

POST /api/v1/transactions/
Returns all query transactions. 'Authentication-Needed'

The request query parameters are not required but can be added if a client wants to query the transactions to be returned, they are:

  • min-amount - Number. If provided alone returns all transactions with transaction amounts greater than or equal to provided value
  • max-amount - Number. If provided alone returns all transactions with transaction amounts less than or equal to provided value

If both min-amount and max-amount query parameters are provided, the api returns all transactions with transaction amounts between both.

  • name - String. If provided returns transactions by name of depositor.

Get a Transaction

POST /api/v1/transactions/:id
Returns a transaction. 'Authentication-Needed'

Update a Transaction

PATCH /api/v1/transactions/:id
Updates a transaction. 'Authentication-Needed'

The request body needs to be in JSON format and must include the following properties

  • depositorName - String - Required
  • transactionAmount - Number - Required

Example Request

PATCH /api/v1/transactions/:id
Authorization: Bearer <access_token>

{ 
  "depositorName": "charles winkage",
  "transactionAmount": 1500
}

Delete Transaction

DELETE /api/v1/transactions/:id
Deletes a transaction. 'Authentication-Needed'


LOOSE ENDS.

Before proceeding with this documentation, I want to bring to your attention that I have purposely not created a route that enables a client to log out of the system. Please once again visit my Notion blog page where I talk about why and my intentions moving forward.

About

A secure and easy-to-use API for creating and authenticating users with JSON Web Tokens (JWTs).

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published