This project is a user authentication service implemented using ExpressJS, TypeScript, Mongoose (with MongoDB), JWT (JSON Web Tokens) for authentication, bcrypt for password hashing, and Joi for request validation.
- User registration and login.
- Password hashing with bcrypt.
- JWT authentication for protected routes.
- Request data validation with Joi.
- Node.js
- MongoDB
- npm
-
Install dependencies:
Using npm:
npm install
-
Set up your environment variables:
Create a
.env
file in the root of your project and add the following:JWT_SECRET=123456789 PORT=3000
Replace the
JWT_SECRET
with your own secret key. -
Set up MongoDB connection
Replace with your DB url
MONGO_URI
variable in/src/db/config.ts
Start the server with:
npm run dev
The server will run on http://localhost:{{PORT}}
by default.
POST /users
: Register a new user.POST /users/login
: Authenticate a user and return a JWT.GET /users/:id
: Gets a user profile (Requires Bearer token)
- ExpressJS: Web application framework for Node.js.
- TypeScript: A superset of JavaScript that adds static types.
- Mongoose: MongoDB object modeling tool.
- JWT: For securely transmitting information between parties as a JSON object.
- bcrypt: For hashing passwords.
- Joi: For data validation.
src/controllers
: Contains user controller with authentication logic.src/models
: Mongoose models for user data.src/routes
: Express routes defining the API endpoints.src/validator
: Middleware for validation and other purposes.src/db
: MongoDB conection details.