feat(controllers): update registerUser function in auth controller

This commit includes changes to improve the `registerUser` function in auth.controller.ts.
- Switched registration from displayName to email and added email validation
- Adjusted types and logging for better consistency and readability

Signed-off-by: Mathis <yidhra@tuta.io>
This commit is contained in:
Mathis H (Avnyr) 2024-05-02 12:23:20 +02:00
parent 3fe6453b0c
commit 1723a8588a
Signed by: Mathis
GPG Key ID: DD9E0666A747D126

View File

@ -1,7 +1,9 @@
import JwtService from "@services/jwt.service"; import JwtService from "@services/jwt.service";
import type { IReqEditUserData } from "@interfaces/IReqEditUserData"; import type { IReqEditUserData } from "@interfaces/IReqEditUserData";
import type { IReqRegister } from "@interfaces/requests/IReqRegister";
import UserService from "@services/user.service"; import UserService from "@services/user.service";
import { isEmail } from "@utils/validators/email";
import type { Request, Response } from "express"; import type { Request, Response } from "express";
import { Logger } from "tslog"; import { Logger } from "tslog";
@ -9,21 +11,8 @@ const logger = new Logger({
name: "AuthController", 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<unknown> { async function registerUser(req: Request, res: Response): Promise<unknown> {
const body = req.body; const body: IReqRegister = req.body;
if (!body) { if (!body) {
logger.warn(`Invalid input data (${req.ip})`); logger.warn(`Invalid input data (${req.ip})`);
return res.type("application/json").status(400).json({ return res.type("application/json").status(400).json({
@ -35,7 +24,7 @@ async function registerUser(req: Request, res: Response): Promise<unknown> {
!body.username || !body.username ||
!body.firstName || !body.firstName ||
!body.lastName || !body.lastName ||
!body.displayName !body.email
) { ) {
logger.warn(`Field(s) missing (${req.ip})`); logger.warn(`Field(s) missing (${req.ip})`);
return res.type("application/json").status(400).json({ return res.type("application/json").status(400).json({
@ -43,13 +32,20 @@ async function registerUser(req: Request, res: Response): Promise<unknown> {
}); });
} }
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; let gdpr = false;
if (body.gdpr === true) { if (body.gdpr === true) {
gdpr = true; gdpr = true;
} }
const sanitizeData = { const sanitizeData: IReqRegister = {
username: `${body.username}`, username: `${body.username}`,
displayName: `${body.displayName}`, email: `${body.email.toLowerCase()}`,
gdpr: gdpr, gdpr: gdpr,
password: `${body.password}`, password: `${body.password}`,
firstName: `${body.firstName}`, firstName: `${body.firstName}`,
@ -74,7 +70,7 @@ async function registerUser(req: Request, res: Response): Promise<unknown> {
} }
// SUCCESS // SUCCESS
logger.info(`User registered successfully (${req.ip})`); logger.info(`User registered successfully (${sanitizeData.username})`);
return res.type("application/json").status(201).json(RegisterServiceResult); return res.type("application/json").status(201).json(RegisterServiceResult);
} }