martin-markovic / learn-connect

Multi-feature platform designed to enhance the online learning experience.
MIT License
0 stars 0 forks source link

Update user authentication #4

Closed martin-markovic closed 2 months ago

martin-markovic commented 2 months ago

Summary

This pull request implements several key features and improvements, focusing on user authentication, database integration, and code refactoring.

Changes Implemented

  1. User Registration and Login Validation

    • Implemented request and response validation and error handling for registerUser and loginUser.
  2. Database Connection

    • Implemented connectDB by integrating mongoose in db.js.
    • Integrated and instantiated connectDB into server.js.
  3. Server Test Refactoring

    • Refactored server.test.js by adding describe blocks with before to server initialization tests.
    • Added before and after hooks for socket.io connection tests.
  4. User Model Implementation

    • Implemented userModel.js using mongoose.Schema.
  5. Package Installation and Integration

    • Installed jsonwebtoken and bcrypt via npm.
    • Integrated these packages alongside the User schema in userController.js.
  6. Controller Refactoring

    • Refactored registerUser and loginUser functions to utilize try...catch for error handling.
    • Integrated input validation, user schema validation, user creation, bcrypt password hashing, and error handling within the controller.
  7. Environment Variable

    • Added JWT_SECRET to the .env file.
  8. Token Generation

    • Implemented generateToken function to generate valid JSON Web Tokens for authentication.
  9. Response Enhancement

    • Integrated user data into the response using JSON.

Testing

Updated and refactored userAuth.test.js to cover the new functionality and ensure robustness. Tested for integration.

Installation

Run npm i to install the new dependencies (jsonwebtoken and bcrypt).

Notes

Ensure that .env file contains theJWT_SECRET key. Verify that the database connection string is correctly set in the db.js.