|
@@ -12,208 +12,15 @@ import { ApiError } from "#exceptions/api-error.js";
|
|
|
// other
|
|
|
import { z } from "zod";
|
|
|
import bcript from "bcrypt";
|
|
|
-import { v7 as uuidv7 } from "uuid";
|
|
|
|
|
|
import { UserAuthService } from "../services/user-auth-service.js";
|
|
|
-import { ConfirmPinsService } from "#modules/client/users/confirm-pins/confirm-pins-service.js";
|
|
|
+
|
|
|
import { RouterUtils } from "#utils/router-utils.js";
|
|
|
import { config } from "#config";
|
|
|
import { Request, Response } from "express";
|
|
|
-import { cUsersService } from "../../c-users-service.js";
|
|
|
-import sessionService from "../services/session-service.js";
|
|
|
import tokenService from "../services/token-service.js";
|
|
|
-import { cCustomFieldsValidateService } from "#modules/client/custom-fields/c-cf-validate-service.js";
|
|
|
|
|
|
class authController {
|
|
|
- // --- Регистрация ---
|
|
|
- async getUserRegData(req: Request, res: Response) {
|
|
|
- const event = await sessionService.getCurrentEventFromReq(req);
|
|
|
-
|
|
|
- const regData = await cUsersService.getUserEventFieldsWithValidators(
|
|
|
- event.eventId,
|
|
|
- );
|
|
|
-
|
|
|
- RouterUtils.validAndSendResponse(api.client.auth.GET_UserRegData.res, res, {
|
|
|
- code: "success",
|
|
|
- fields: [...regData],
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- async register(
|
|
|
- req: Request,
|
|
|
- res: Response,
|
|
|
- // next: NextFunction
|
|
|
- ) {
|
|
|
- // валидация запроса
|
|
|
- const { email } = api.client.auth.POST_Registration.req.parse(req.body);
|
|
|
-
|
|
|
- const isUserExist = await UserAuthService.checkUserExistByEmail(email);
|
|
|
-
|
|
|
- // если пользователь уже зарегистрирован
|
|
|
- if (isUserExist) {
|
|
|
- RouterUtils.validAndSendResponse(
|
|
|
- api.client.auth.POST_Registration.res,
|
|
|
- res,
|
|
|
- { code: "alreadyExists" },
|
|
|
- 400,
|
|
|
- );
|
|
|
-
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- // отправка пина
|
|
|
- const transactionId = uuidv7();
|
|
|
- try {
|
|
|
- await ConfirmPinsService.sendConfirmPin({
|
|
|
- transactionId,
|
|
|
- email,
|
|
|
- actionType: "registration",
|
|
|
- });
|
|
|
- } catch {
|
|
|
- RouterUtils.validAndSendResponse(
|
|
|
- api.client.auth.POST_Registration.res,
|
|
|
- res,
|
|
|
- { code: "pinIsNotSent" },
|
|
|
- 400,
|
|
|
- );
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- RouterUtils.validAndSendResponse(
|
|
|
- api.client.auth.POST_Registration.res,
|
|
|
- res,
|
|
|
- {
|
|
|
- code: "pinIsSent",
|
|
|
- transactionId: transactionId,
|
|
|
- },
|
|
|
- );
|
|
|
- }
|
|
|
-
|
|
|
- async confirmRegistration(req: Request, res: Response) {
|
|
|
- // валидация запроса
|
|
|
- const { password, transactionId, confirmPin, fields } =
|
|
|
- api.client.auth.POST_ConfirmRegistration.req.formData.body.parse(
|
|
|
- JSON.parse(req.body.body),
|
|
|
- );
|
|
|
-
|
|
|
- const event = await sessionService.getCurrentEventFromReq(req);
|
|
|
- const files = req.files;
|
|
|
-
|
|
|
- // проверка пина
|
|
|
- const pinInfo = await ConfirmPinsService.checkConfirmPin(
|
|
|
- transactionId,
|
|
|
- confirmPin,
|
|
|
- );
|
|
|
-
|
|
|
- switch (pinInfo.status) {
|
|
|
- case "rotten": {
|
|
|
- RouterUtils.validAndSendResponse(
|
|
|
- api.client.auth.POST_ConfirmRegistration.res,
|
|
|
- res,
|
|
|
- { code: "pinIsRotten" },
|
|
|
- 400,
|
|
|
- );
|
|
|
- return;
|
|
|
- }
|
|
|
- case "tooManyTries": {
|
|
|
- RouterUtils.validAndSendResponse(
|
|
|
- api.client.auth.POST_ConfirmRegistration.res,
|
|
|
- res,
|
|
|
- { code: "tooManyTries" },
|
|
|
- 400,
|
|
|
- );
|
|
|
- return;
|
|
|
- }
|
|
|
- case "wrong": {
|
|
|
- RouterUtils.validAndSendResponse(
|
|
|
- api.client.auth.POST_ConfirmRegistration.res,
|
|
|
- res,
|
|
|
- {
|
|
|
- code: "pinIsWrong",
|
|
|
- triesRemained: pinInfo.triesRemained,
|
|
|
- },
|
|
|
- 400,
|
|
|
- );
|
|
|
- return;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // пин правильный
|
|
|
- const email = pinInfo.email;
|
|
|
- // регистрация
|
|
|
- const hashPassword = await bcript.hash(password, 3);
|
|
|
-
|
|
|
- // поля пользователя
|
|
|
- const userData = await cUsersService.getUserEventFieldsWithValidators(
|
|
|
- event.eventId,
|
|
|
- );
|
|
|
-
|
|
|
- const refFields = userData.map((f) => ({
|
|
|
- ...f,
|
|
|
- idKey: "userEfId",
|
|
|
- }));
|
|
|
-
|
|
|
- // валидация полей
|
|
|
- const validationResult =
|
|
|
- await cCustomFieldsValidateService.processAndValidateFields({
|
|
|
- inputFields: fields,
|
|
|
- referenceFields: refFields,
|
|
|
- files,
|
|
|
- idKey: "userEfId",
|
|
|
- addOldValue: false,
|
|
|
- });
|
|
|
-
|
|
|
- if (!validationResult.isValid)
|
|
|
- throw ApiError.BadRequest(
|
|
|
- "fieldsValidationFailed",
|
|
|
- JSON.stringify(validationResult.messages),
|
|
|
- );
|
|
|
-
|
|
|
- const validatedFields = validationResult.checkedfields;
|
|
|
- // вставляем в базу и сохраняем файлы
|
|
|
- const userId = uuidv7();
|
|
|
- await updPool.transaction(async (tr) => {
|
|
|
- await tr.query(
|
|
|
- sql.unsafe`
|
|
|
- insert into usr.users
|
|
|
- (user_id, email, password)
|
|
|
- values
|
|
|
- (${userId}, ${email}, ${hashPassword})`,
|
|
|
- );
|
|
|
-
|
|
|
- await cCustomFieldsValidateService.saveCustomFieldValuesInTransaction({
|
|
|
- tr,
|
|
|
- parentId: userId,
|
|
|
- action: "userProfile",
|
|
|
- inputFields: validatedFields,
|
|
|
- files,
|
|
|
- isDeleteBefore: false,
|
|
|
- });
|
|
|
- });
|
|
|
-
|
|
|
- // токены
|
|
|
- const { accessToken, refreshToken } = tokenService.generateTokens({
|
|
|
- email,
|
|
|
- userId,
|
|
|
- });
|
|
|
- await tokenService.insertRefreshToken(userId, refreshToken);
|
|
|
-
|
|
|
- tokenService.setRefreshTokenInCookie(res, refreshToken);
|
|
|
-
|
|
|
- RouterUtils.validAndSendResponse(
|
|
|
- api.client.auth.POST_ConfirmRegistration.res,
|
|
|
- res,
|
|
|
- {
|
|
|
- code: "registered",
|
|
|
- accessToken,
|
|
|
- userData: {
|
|
|
- email,
|
|
|
- userId,
|
|
|
- },
|
|
|
- },
|
|
|
- );
|
|
|
- }
|
|
|
-
|
|
|
async login(req: Request, res: Response) {
|
|
|
// валидация запроса
|
|
|
const { email, password } = api.client.auth.POST_Login.req.parse(req.body);
|
|
@@ -289,7 +96,11 @@ class authController {
|
|
|
email,
|
|
|
userId: user.userId,
|
|
|
});
|
|
|
- await tokenService.insertRefreshToken(user.userId, refreshToken);
|
|
|
+ await tokenService.insertRefreshToken({
|
|
|
+ tr: updPool,
|
|
|
+ userId: user.userId,
|
|
|
+ refreshToken,
|
|
|
+ });
|
|
|
|
|
|
tokenService.setRefreshTokenInCookie(res, refreshToken);
|
|
|
|