diff --git a/src/controllers/adminController.ts b/src/controllers/adminController.ts index b169f9e..0ba6044 100644 --- a/src/controllers/adminController.ts +++ b/src/controllers/adminController.ts @@ -1,6 +1,9 @@ import { Request, Response } from "express"; import { User } from "../models/user"; -import { ADMIN_MAX_USERS_PER_PAGE } from "../utils/utils"; +import { + ADMIN_MAX_USERS_PER_PAGE, + MONGODB_IGNORED_FIELDS, +} from "../utils/constants"; export async function GetAllUsers(req: Request, res: Response) { try { @@ -21,7 +24,7 @@ export async function GetAllUsers(req: Request, res: Response) { // Query for the current page with limit and skip const users = await User.find({}) .lean() - .select("-password -_id -__v") // Exclude password and other fields + .select(MONGODB_IGNORED_FIELDS) // Exclude password and other fields .skip(skip) .limit(pageSize); diff --git a/src/controllers/userController.ts b/src/controllers/userController.ts index f17eefe..a71b1d6 100644 --- a/src/controllers/userController.ts +++ b/src/controllers/userController.ts @@ -2,6 +2,7 @@ import { Request, Response } from "express"; import bcrypt from "bcrypt"; import { User } from "../models/user"; import { saveSession } from "../utils/utils"; +import { MONGODB_IGNORED_FIELDS } from "../utils/constants"; export async function SignUp(req: Request, res: Response) { if (!req.body.accountName || !req.body.username || !req.body.password) { @@ -10,7 +11,9 @@ export async function SignUp(req: Request, res: Response) { const { accountName, username, password } = req.body; - const existingUser = await User.findOne({ accountName }).lean(); + const existingUser = await User.findOne({ accountName }) + .select("accountName -_id") + .lean(); if (existingUser) { return res.status(400).json({ status: 1 }); @@ -89,7 +92,9 @@ export async function GetUserProfile(req: Request, res: Response) { try { const user = await User.findOne({ accountName: req.params.accountName, - }).lean(); + }) + .select(MONGODB_IGNORED_FIELDS) + .lean(); if (!user) { return res.status(404).json({ status: "err" }); diff --git a/src/utils/constants.ts b/src/utils/constants.ts index 2461843..dc849c8 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -3,3 +3,5 @@ export const DEFAULT_SESSION_EXPIRATION: number = 7 * 24 * 60 * 60 * 1000; // Maximum number of users to display per page in the admin interface export const ADMIN_MAX_USERS_PER_PAGE: number = 10; + +export const MONGODB_IGNORED_FIELDS: string = "-password -_id -__v";