This repository contains a simple authentication server implemented using Express.js, JWT (JSON Web Tokens), and bcrypt for password hashing. The server provides endpoints for user sign up and sign in, generating a token upon successful authentication.
- User registration: Users can sign up by providing a unique username and password. The server checks if the username already exists and returns an error if it does.
- User authentication: Users can sign in by providing their username and password. The server verifies the credentials and returns an authentication token upon successful login.
- Token-based authentication: JWT (JSON Web Tokens) are used to generate and validate authentication tokens. The tokens are signed using a secret key, ensuring their authenticity.
- Basic password hashing: Passwords are hashed using bcrypt, a secure password-hashing algorithm. This ensures that even if the server is compromised, user passwords remain protected.
- CORS support: Cross-Origin Resource Sharing (CORS) is enabled, allowing the server to handle requests from different origins.
- Environment variable configuration: The server supports environment variables for configuration, allowing for easy customization of settings such as the secret key.
Before running the server, ensure you have the following dependencies installed:
- Node.js
- npm (Node Package Manager)
-
Clone this repository to your local machine.
-
Navigate to the project directory in your terminal.
-
Run
npm installto install the required dependencies. -
Create a
.envfile in the project root and define the following environment variable:SECRET_KEY=<your_secret_key>Replace
<your_secret_key>with your desired secret key for JWT token signing.
- Start the server by running
npm startin the project directory. - The server will be running on
http://localhost:3000.
-
POST /api/signup - Register a new user.
- Request body:
{ "username": "<username>", "password": "<password>" } - Response:
{ "message": "User created successfully", "token": "<generated_token>" }
- Request body:
-
POST /api/signin - Authenticate a user.
- Request body:
{ "username": "<username>", "password": "<password>" } - Response:
{ "message": "Sign in successful", "token": "<generated_token>", "user": "<username>" }
- Request body:
- It is recommended to use HTTPS when deploying this server to ensure secure communication between clients and the server.
- In a production environment, consider using a secure session management system to handle user sessions and protect against session hijacking attacks.
This project is licensed under the MIT License.