diff --git a/src/controllers/auth.controller.ts b/src/controllers/auth.controller.ts index 9ae845f..5b2be59 100644 --- a/src/controllers/auth.controller.ts +++ b/src/controllers/auth.controller.ts @@ -1,7 +1,9 @@ import JwtService from "@services/jwt.service"; import type { IReqEditUserData } from "@interfaces/IReqEditUserData"; +import type { IReqRegister } from "@interfaces/requests/IReqRegister"; import UserService from "@services/user.service"; +import { isEmail } from "@utils/validators/email"; import type { Request, Response } from "express"; import { Logger } from "tslog"; @@ -9,21 +11,8 @@ const logger = new Logger({ name: "AuthController", }); -//FIX Better return object interface -/** - * Registers a user with the given request data. - * - * @param {Request} req - The request object containing user data. - * @param {Response} res - The response object to send the registration result. - * - * @return {Promise} A promise that resolves to the registration result. - * It can have the following properties: - * - error: "gdprNotApproved" if GDPR is not approved - * - error: "exist" if the user already exists - * - Otherwise, the registered user data - */ async function registerUser(req: Request, res: Response): Promise { - const body = req.body; + const body: IReqRegister = req.body; if (!body) { logger.warn(`Invalid input data (${req.ip})`); return res.type("application/json").status(400).json({ @@ -35,7 +24,7 @@ async function registerUser(req: Request, res: Response): Promise { !body.username || !body.firstName || !body.lastName || - !body.displayName + !body.email ) { logger.warn(`Field(s) missing (${req.ip})`); return res.type("application/json").status(400).json({ @@ -43,13 +32,20 @@ async function registerUser(req: Request, res: Response): Promise { }); } + if (!isEmail(body.email)) { + logger.warn(`Invalid email format (${req.ip})`); + return res.type("application/json").status(400).json({ + error: "Invalid email format", + }); + } + let gdpr = false; if (body.gdpr === true) { gdpr = true; } - const sanitizeData = { + const sanitizeData: IReqRegister = { username: `${body.username}`, - displayName: `${body.displayName}`, + email: `${body.email.toLowerCase()}`, gdpr: gdpr, password: `${body.password}`, firstName: `${body.firstName}`, @@ -74,7 +70,7 @@ async function registerUser(req: Request, res: Response): Promise { } // SUCCESS - logger.info(`User registered successfully (${req.ip})`); + logger.info(`User registered successfully (${sanitizeData.username})`); return res.type("application/json").status(201).json(RegisterServiceResult); }