This repository contains a middleware for handling user authentication. It provides functionality to register users and login with JWT (JSON Web Tokens) for secure authentication.
- User registration with email and password.
- User login with email and password.
- Secure authentication using JWT tokens.
- MongoDB integration for user data storage.
Before installing and running this middleware, ensure that you have the following prerequisites:
- Node.js (v12 or higher)
- MongoDB
To get started with the authentication middleware, follow these steps:
- Clone this repository to your local machine.
git clone https://github.com/rahuldahal/auth.git
- Install the dependencies by navigating to the cloned repository directory and running the following command:
cd auth
npm install
- Set up the environment variables. Create a
.env
file in the root directory and provide the necessary values for the following variables:
MONGODB_URI=<your-mongodb-connection-uri>
JWT_SECRET=<your-secret-key>
- Start the middleware by running the following command:
npm start
The middleware should now be up and running on the specified port (default: 3000) and connected to the MongoDB database.
The authentication middleware provides registration and login endpoints for user authentication using JWT tokens. See the API Endpoints section for more details.
The following API endpoints are available for user authentication:
- Endpoint:
/register
- Method: POST
- Request Body:
{
"email": "[email protected]",
"password": "secretpassword",
"passwordConfirmation": "secretpassword"
}
- Response:
HTTP Status Code: 201 Created
JWT Access Token: your-generated-jwt-token-with-payload
- Endpoint:
/login
- Method: POST
- Request Body:
{
"email": "[email protected]",
"password": "secretpassword"
}
- Response:
HTTP Status Code: 200 OK
JWT Access Token: your-generated-jwt-token-with-payload
Contributions are welcome! If you find any issues or have suggestions for improvements, please feel free to open an issue or submit a pull request.
This project is licensed under the MIT License.