This project is a secure authentication API built with Node.js and Express. It demonstrates best practices in input validation, JWT-based authentication, and error handling.
- Input validation using express-validator
- JWT token generation for authentication
- Mock user database
- Error handling with appropriate HTTP status codes
- Token verification endpoint (Bonus)
- Node.js
- Express
- jsonwebtoken
- express-validator
- dotenv
Description: Authenticates user and returns a JWT token.
Request Body:
{
"email": "test@example.com",
"password": "password123"
}- Email must exist and be in a valid format.
- Password must exist and be at least 8 characters.
200 OK: Returns JWT token.400 Bad Request: Missing or invalid inputs.401 Unauthorized: Invalid email or password.500 Internal Server Error: Token generation failure.
Description: Verifies if a provided JWT token is valid.
Header:
Authorization: Bearer <token>200 OK: Token is valid.401 Unauthorized: Missing or invalid token.
git clone https://github.com/odogwukelly/secure-auth-api.git
cd secure-auth-apinpm installJWT_SECRET=SECRET_123
PORT=3000node app.js- POST
/api/login - GET
/api/verifywith Bearer Token
Test users mock database:
{
"email": "test@example.com",
"password": "password123"
},
{
"email": "test2@example.com",
"password": "password"
}secure-auth-api/
├── controllers/
│ └── authController.js
├── middleware/
│ └── validate.js
├── routes/
│ └── authRoutes.js
├── utils/
│ └── mockDB.js
├── .env
├── .gitignore
├── app.js
├── package.json
├── README.md
- Passwords are stored in plain text for simplicity. In a real-world application, use
bcryptfor hashing. - This project uses a mock database. Integrate with a real DB (e.g., MongoDB, PostgreSQL) in production.
Backend Developer – SimpliRide Tech Internship Candidate