Skip to content

FearSmoker/GFG_Backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

72 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

User Authentication System

A robust user authentication system built using Node.js and Express.js. This system supports registration, login, logout, token refreshing, password changes, and profile updates. It utilizes JWT for secure authentication and Cloudinary for managing avatar uploads.


Features

  • User Registration

    • Register with full name, email, username, and password.
    • Avatar upload using Cloudinary.
  • User Login

    • Login with email or username and password.
    • JWT-based access and refresh token generation.
  • Logout

    • Clears refresh and access tokens.
  • Token Refresh

    • Refreshes the access token using the refresh token.
  • Password Management

    • Change the current password securely.
  • Profile Management

    • Update full name, email, or avatar.
  • Avatar Resizing

    • The system automatically resizes uploaded avatars to 256x256 pixels before uploading them to Cloudinary.
  • Email Confirmation

    • The system automatically send confirmation emails through a specified email service.
  • Get Current User

    • Fetch details of the authenticated user.
  • Get Current Profile

    • Fetch profile of the authenticated user.

Project Structure

project-root/
├── uploads/
├── src/
│   ├── app.js
│   ├── constants.js
│   ├── index.js
│   ├── controllers/
│   │   ├── events.controller.js
│   │   ├── member.controller.js
│   │   ├── problem.controller.js
│   │   ├── user.controller.js
│   ├── db/
│   │   └── index.js
│   ├── middlewares/
│   │   ├── auth.middleware.js
│   │   ├── authorize.middleware.js
│   │   ├── error.middleware.js
│   │   └── multer.middleware.js
│   ├── models/
│   │   ├── events.model.js
│   │   ├── member.model.js
│   │   ├── user.model.js
│   │   └── visitor.model.js
│   ├── routes/
│   │   ├── events.routes.js
│   │   ├── member.routes.js
│   │   ├── problem.routes.js
│   │   ├── user.routes.js
│   │   └── visitor.routes.js
│   └── utils/
│       ├── ApiError.js
│       ├── ApiResponse.js
│       ├── asyncHandler.js
│       ├── cloudinary.js
│       ├── ConfirmationEmail.js
│       ├── GoogleConfig.js
│       ├── ResizeImage.js
│       ├── scraper.js
│       └── UploadExternalImage.js


Installation

  1. Clone the repository:

    git clone https://github.com/your-repository/user-authentication-system.git
  2. Navigate to the project directory:

    cd user-authentication-system
  3. Install dependencies:

    npm install
  4. Create a .env file in the root directory and configure the following variables:

    MONGO_URI=your_mongodb_connection_string
    JWT_SECRET=your_jwt_secret
    REFRESH_TOKEN_SECRET=your_refresh_token_secret
    CLOUDINARY_CLOUD_NAME=your_cloudinary_cloud_name
    CLOUDINARY_API_KEY=your_cloudinary_api_key
    CLOUDINARY_API_SECRET=your_cloudinary_api_secret
    POTD_URL=your_potd_api_endpoint
    PROBLEM_DETAILS_URL=your_problem_details_api_endpoint
    EMAIL=your_email_details
    EMAILPORT=your_email_port_details
    HOST=your_email_host_details
    PASS=your_email_password_details
  5. Start the server in development mode:

    npm run dev

API Endpoints

User Routes

POST /api/v1/users/register

  • Description: Register a new user.
  • Middleware: multerMiddleware
  • Request Body:
    {
      "fullName": "John Doe",
      "email": "john.doe@example.com",
      "username": "johndoe",
      "password": "password123",
      "avatar": "test.jpg"
    }
  • File Upload: avatar (image file)

POST /api/v1/users/login

  • Description: Login a user.
  • Request Body:
    {
      "email": "john.doe@example.com",
      "password": "password123"
    }

POST /api/v1/users/logout

  • Description: Logout a user.
  • Middleware: authMiddleware

POST /api/v1/users/refresh

  • Description: Refresh access token using the refresh token.
  • Request Body:
    {
      "refreshToken": "your_refresh_token"
    }

PATCH /api/v1/users/change-password

  • Description: Change the current password.
  • Middleware: authMiddleware
  • Request Body:
    {
      "oldPassword": "old_password",
      "newPassword": "new_password",
      "confirmPassword": "new_password"
    }

GET /api/v1/users/current-user

  • Description: Get details of the authenticated user.
  • Middleware: authMiddleware

GET /api/v1/users/get-profile

  • Description: Retrieve the current user's profile information (full name, email, username, avatar, mobile number).
  • Middleware: authMiddleware
  • Request Body:
    {
    "fullName": "Johnathan Doe",
    "avatar": "url_to_avatar_image",
    "email": "johnathan.doe@example.com",
    "username": "johnathandoe",
    "mobileNo": "+1234567890"
    }

PATCH /api/v1/users/update-account

  • Description: Update account details (full name and email).
  • Middleware: authMiddleware
  • Request Body:
    {
      "fullName": "Johnathan Doe",
      "email": "johnathan.doe@example.com"
    }

PATCH /api/v1/users/update-avatar

  • Description: Update the user's avatar.
  • Middleware: authMiddleware, multerMiddleware
  • File Upload: avatar (image file)
  • Additional Functionality: The uploaded avatar image is resized to 256x256 pixels before being uploaded to Cloudinary.

Technologies Used

  • Backend: Node.js, Express.js
  • Database: MongoDB
  • Authentication: JWT
  • File Upload: Cloudinary
  • Utilities: Multer, AsyncHandler, Custom Error Handling, Image Resizing with Sharp, Confirmation mail using nodemailer

License

This project is licensed under the MIT License.


Contributing

Feel free to contribute by submitting a pull request. For major changes, please open an issue first to discuss what you would like to change.

Problem of the Day API

This project is a Node.js-based backend application designed to fetch the "Problem of the Day" from GeeksforGeeks using their public APIs. The project extracts and cleans up the data to provide the problem's name, difficulty, description, and a link to the original source.


Features

  • Fetches the Problem of the Day (POTD) using GeeksforGeeks API.
  • Provides cleaned and user-friendly problem descriptions by removing unnecessary HTML elements.
  • Exposes a REST API endpoint to serve the processed problem details.
  • Error handling for seamless API requests.

Prerequisites

Before you begin, ensure you have met the following requirements:

  • Node.js and npm installed (>= 16.x).
  • Postman or any other tool for API testing.

Installation

  1. Clone the repository:

    git clone <repository-url>
    cd <repository-folder>
  2. Install dependencies:

    npm install
  3. Create a .env file in the root directory and configure the necessary environment variables:

    PORT=8000
  4. Start the server:

    npm run dev

Folder Structure

src/
|-- controllers/
|   |-- problem.controller.js       # Logic to handle problem fetching and processing
|-- middlewares/
|   |-- error.middleware.js         # Centralized error handling middleware
|-- routes/
|   |-- problem.routes.js           # Route definitions for the Problem of the Day API
|-- utils/
|   |-- scraper.js                  # Utility to fetch and clean problem details
|-- app.js                          # Express application setup
|-- index.js                        # Entry point of the application
|-- constants.js                    # Holds constants like API URLs
.env                                 # Environment variables

Usage

Endpoint

GET /api/v1/problems/potd

Fetches the Problem of the Day details.

Response Format:

{
    "problemId": 701297,
    "problemName": "Strings Rotations of Each Other",
    "difficulty": "Easy",
    "problemDescription": "You are given two strings of equal lengths...",
    "problemLink": "https://www.geeksforgeeks.org/problems/..."
}

Testing with Postman

  1. Open Postman.
  2. Create a new GET request.
  3. Enter the URL:
    http://localhost:8000/api/v1/problems/potd
    
  4. Hit Send and verify the response.

Technologies Used

  • Node.js: JavaScript runtime.
  • Express.js: Web framework for Node.js.
  • Cheerio.js: Parses and cleans HTML content.
  • Axios: HTTP client for API requests.
  • Dotenv: Environment variable management.

Contributing

Contributions are welcome! To contribute:

  1. Fork the repository.
  2. Create a new branch:
    git checkout -b feature-name
  3. Make your changes and commit them:
    git commit -m 'Add some feature'
  4. Push to the branch:
    git push origin feature-name
  5. Open a Pull Request.

License

This project is licensed under the MIT License. See the LICENSE file for details.


Acknowledgments

  • GeeksforGeeks for providing the APIs.
  • Community contributors for helping with improvements.

About

User authentication system for our GFG student chapter website

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors