diff --git a/.env b/.env index 23679d0c..424129fe 100644 --- a/.env +++ b/.env @@ -27,4 +27,4 @@ JWT_SECRET_KEY= # This setting can influence the application's behavior, enabling or disabling certain features based on the environment. # For example, in 'development' mode, more verbose logging might be enabled, whereas 'production' might focus on performance optimizations and error handling. # Default: development (if not specified) -NODE_ENVIRONMENT= +NODE_ENVIRONMENT= \ No newline at end of file diff --git a/src/controllers/authController.js b/src/controllers/authController.js index 0efb284a..69a21861 100644 --- a/src/controllers/authController.js +++ b/src/controllers/authController.js @@ -14,9 +14,9 @@ export async function registerUser(req, res) { return res.status(400).json({ message: "User already exists" }); } // Hash the password before saving it - const hashed = await hashPassword(password); - // Create a new user instance with the hashed password and save it to the database - user = new User({ email, password: hashed }); + const hashedPassword = await hashPassword(password); + // Create a new user instance and save it to the database + user = new User({ email, password: hashedPassword }); await user.save(); // Respond with the generated token res.status(201).json({ message: "User created successfully" }); diff --git a/src/middlewares/authMiddleware.js b/src/middlewares/authMiddleware.js index a5acb786..b1211069 100644 --- a/src/middlewares/authMiddleware.js +++ b/src/middlewares/authMiddleware.js @@ -1,4 +1,5 @@ import jwt from "jsonwebtoken"; +import { jwtSecret } from "../config/initialConfig.js"; // Middleware to validate JWT tokens export default function auth(req, res, next) { @@ -12,7 +13,7 @@ export default function auth(req, res, next) { try { // Verify the token using the secret key - const decoded = jwt.verify(token, process.env.JWT_SECRET); + const decoded = jwt.verify(token, jwtSecret); req.user = decoded.userId; // Attach the user ID to the request object next(); // Proceed to the next middleware or route handler } catch (error) { diff --git a/src/models/User.js b/src/models/User.js index 4addba8a..8275826a 100644 --- a/src/models/User.js +++ b/src/models/User.js @@ -1,6 +1,5 @@ // Import required modules and configuration import mongoose from "mongoose"; -import bcrypt from "bcrypt"; // Define a schema for the user with email and password fields const UserSchema = new mongoose.Schema({