A robust backend API built using Node.js, Express, PostgreSQL, and Redis, following clean architecture principles. This API provides user authentication and weather data management with secure JWT-based authentication and caching for optimal performance.
- Register: Secure user registration with hashed passwords.
- Login: Authenticate users and generate JWT tokens.
- Get Current User: Fetch details of the currently logged-in user.
- Logout: Invalidate JWT tokens for secure logout.
- Create Weather Data: Fetch and store weather data from the OpenWeatherMap API.
- Retrieve Weather Data: Access weather data stored in the database.
- Update Weather Data: Modify existing weather entries.
- Delete Weather Data: Remove specific weather records.
- Cache Results: Utilizes Redis to cache weather requests for better performance.
- Passwords are hashed securely using bcrypt.
- JWT tokens are signed and verified to authorize users.
- All weather routes are protected by authentication middleware.
Divided into domain, application, infrastructure, and interfaces layers to ensure a scalable and maintainable structure.
├── application
├── domain
│ ├── entities
│ │ └── User.js
│ └── validators
│ ├── auth
│ │ ├── registerValidator.js
│ │ └── loginValidator.js
│ ├── weather
│ │ ├── createWeatherValidator.js
│ │ └── updateWeatherValidator.js
├── application
│ └── services
│ │ └── AuthService.js
│ ├── middlewares
│ │ └── authMiddleware.js
├── infrastructure
│ ├── database
│ ├── repositories
| | ├── WeatherRepository.js
│ │ └── UserRepository.js
├── interfaces
│ ├── controllers
│ │ ├── AuthController.js
│ │ └── WeatherController.js
│ └── routes
│ ├── authRoutes.js
│ └── weatherRoutes.js
└── README.md
-
Clone the repository:
git clone https://github.com/SAMIRKL/Backend-Developer-Challenge/edit/main/README.md
-
Navigate to the project directory:
cd weather-auth-api
-
Install dependencies:
npm install
-
Configure environment variables:
-
Create a
.env
file in the root directory with the following variables:PORT=3000 DATABASE_URL=your_database_url JWT_SECRET=your_jwt_secret REDIS_URL=your_redis_url API_KEY=your_openweathermap_api_key
-
-
Run migrations:
npx sequelize-cli db:migrate
-
Start the application:
npm run dev
POST /api/auth/register
- Register a new user.POST /api/auth/login
- Log in a user and receive a JWT token.GET /api/auth/me
- Retrieve the currently logged-in user's details.POST /api/auth/logout
- Log out and invalidate the user's token.
POST /api/weather
- Create a new weather entry.GET /api/weather
- Retrieve all stored weather entries.GET /api/weather/:id
- Get a weather record by ID.PUT /api/weather/:id
- Update a weather entry.DELETE /api/weather/:id
- Delete a weather record.GET /api/weather/latest/:cityName
- Fetch and cache the latest weather data for a city.
This middleware checks if a valid JWT token is provided in the Authorization header for accessing protected routes.
-
Usage:
const authMiddleware = require("../middleware/authMiddleware"); router.use(authMiddleware);
Handles all errors globally and provides consistent error responses.
-
Usage:
app.use(errorHandler);
Run tests using the following command:
npm test
- Node.js: Backend runtime environment.
- Express: Web framework for building APIs.
- PostgreSQL: Relational database for data storage.
- Redis: In-memory cache for improving performance.
- JWT: Secure token-based authentication.
- Swagger: API documentation and testing.
- TypeORM: ORM for database interactions.
- bcrypt: Secure password hashing.
- Auth System: Developed user registration, login, and token-based authentication.
- Weather Management: Integrated OpenWeatherMap API for fetching weather data. Added CRUD operations for weather entries. Implemented Redis caching for improved efficiency.
- Middleware: Created authentication and error handling middleware.
init: add initial auth controller
fix: password hash comparison issue
chore: setup Swagger API documentation
- Your Name GitHub: SAMIRKL
This project is licensed under the MIT License.