Skip to content

A robust and scalable backend API built with Node.js, Express, PostgreSQL, and Redis, adhering to clean architecture principles. This API provides secure user authentication using JWT and efficient weather data management with caching for optimal performance.

Notifications You must be signed in to change notification settings

SAMIRKL/Backend-Developer-Challenge

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

53 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Weather & Auth API

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.

Features

User Authentication

  • 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.

Weather Management

  • 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.

Security

  • Passwords are hashed securely using bcrypt.
  • JWT tokens are signed and verified to authorize users.
  • All weather routes are protected by authentication middleware.

Clean Architecture

Divided into domain, application, infrastructure, and interfaces layers to ensure a scalable and maintainable structure.

Project 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

Installation

  1. Clone the repository:

    git clone https://github.com/SAMIRKL/Backend-Developer-Challenge/edit/main/README.md
  2. Navigate to the project directory:

    cd weather-auth-api
  3. Install dependencies:

    npm install
  4. 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
      
  5. Run migrations:

    npx sequelize-cli db:migrate
  6. Start the application:

    npm run dev

Usage

API Endpoints

Authentication

  • 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.

Weather

  • 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.

Middleware

Authentication Middleware

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);

Error Handler Middleware

Handles all errors globally and provides consistent error responses.

  • Usage:

    app.use(errorHandler);

Testing

Run tests using the following command:

npm test

Tools & Technologies

  • 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.

Commit History

Major Changes

  • 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.

Example Commit Messages

  • init: add initial auth controller
  • fix: password hash comparison issue
  • chore: setup Swagger API documentation

Contributors

License

This project is licensed under the MIT License.

About

A robust and scalable backend API built with Node.js, Express, PostgreSQL, and Redis, adhering to clean architecture principles. This API provides secure user authentication using JWT and efficient weather data management with caching for optimal performance.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published