This is a user authentication API built with Node.js that can be used in any application that uses HTTP.
It provides endpoints for user registration, login, and token generation.
The API uses JWT for authentication and supports middleware for handling authentication and errors.
- Installation
- Configuration
- Database
- Models
- Controllers
- Middleware
- Routes
- Utils
- Server
- Environment Variables
- API Endpoints
- License
-
Clone the repository:
git clone https://github.com/yourusername/user-authentication-api.git
-
Navigate to the project directory:
cd authentication-api
-
Install dependencies:
npm install
Alternatively, you can install dependencies listed in
requirements.txt
:npm install $(cat requirements.txt | xargs)
config/db.js
: Contains database connection logic.
Ensure you have MongoDB installed and running. Configure the database connection in config/db.js
.
models/userModel.js
: Defines the User schema and model.
controllers/userController.js
: Contains logic for user registration and login.
middleware/authMiddleware.js
: Middleware to protect routes that require authentication.middleware/errorMiddleware.js
: Middleware for error handling.
routes/userRoutes.js
: Defines the user-related routes (registration, login).
utils/generateTokens.js
: Contains functions for generating JWT tokens.
server.js
: Entry point for the API. Configures and starts the server.
Create a .env
file in the root of your project with the following environment variables:
PORT=5000
MONGO_URI=your_mongo_db_uri
JWT_SECRET=your_jwt_secret