This project is a Node.js API developed in Typescript that provides JSON Web Token (JWT) authentication. The API enables user registration, login and access control to protected routes.
This is a Node.js API developed with Typescript that implements JWT authentication for user registration, login, and access control to protected routes. It also has routes for creating users and editing usernames that require authentication via JWT token. The API is designed to provide a secure and scalable solution for authenticating users in web applications. Its well-documented code can serve as a solid foundation for similar projects.
To run the API, just clone or download the repository and follow the steps below
Endpoint: /api/register
Method: POST
Description: Creates a new user account.
Request Body:
name(string, required): The user's name.password(string, required): The user's password.
Responses:
201Created: If the user is created successfully.400Bad Request: If the name, email, or password fields are missing or empty.401Bad Request: If the email is already in use.500Internal Server Error: If an unexpected error occurs.
Endpoint: /api/login
Method: POST
Description: Authenticates a user and returns a JWT token.
Request Body:
password(string, required): The user's password.
Responses:
200OK:If the email and password are correct, returns a token that is needed for authentication in all other requests. The token is sent as a bearer token.400Unauthorized: If the user does not exist.401Bad Request: If the email or password is incorrect.500Internal Server Error: If an unexpected error occurs.
Endpoint: /api/edit/:id
Method: PATCH
Description: Edit the name of an existing user.
Request Parameters:
id(string, required): The ID of the user to edit.
Request Body:
name(string, required): The new name of the user.
Request Headers:
Authorization(string, required): The JWT token received from the login endpoint. Sent as a bearer token.
Responses:
400Bad Request: If the user does not exist.2011Created: If the user is edited successfully.500Internal Server Error: If an unexpected error occurs.