Snai3i-MarketPlace / backend / src / controllers / auth.controller.ts
auth.controller.ts
Raw
import { Response } from 'express';
import { MyRequest } from '../types/Express';
import { ErrorResponse, SuccessResponse } from '../utils/Response';
import { AuthServices } from '../services/auth/auth.service';
import {
  ErrorResponseC,
  SuccessResponseC,
} from '../services/services.response';

export const Login = async (req: MyRequest<UserI>, res: Response) => {
  const { email, password } = req.body;
  const result = await AuthServices.executeLogin(res, email, password);
  if (result instanceof SuccessResponseC)
    return SuccessResponse(
      res,
      result.code,
      result.data,
      result.message,
      result.status
    );
  if (result instanceof ErrorResponseC)
    return ErrorResponse(res, result.code, result.message, result.error);
};

export const Register = async (req: MyRequest<UserI>, res: Response) => {


  const {
    email,
    password,
    firstName,
    lastName,
    role,
    phone = null,
    ...additionalInfo
  } = req.body;
  const result = await AuthServices.executeRegister(
    res,
    email,
    password,
    firstName,
    lastName,
    role,
    phone,
    additionalInfo
  );
  if (result instanceof SuccessResponseC)
    return SuccessResponse(
      res,
      result.code,
      result.data,
      result.message,
      result.status
    );
  if (result instanceof ErrorResponseC)
    return ErrorResponse(res, result.code, result.message, result.error);
};

export const AuthBack = async (req: MyRequest<UserI>, res: Response) => {
  const result = await AuthServices.executeAuthBack(req.user!.user_id);
  if (result instanceof SuccessResponseC)
    return SuccessResponse(
      res,
      result.code,
      result.data,
      result.message,
      result.status
    );
  if (result instanceof ErrorResponseC)
    return ErrorResponse(res, result.code, result.message, result.error);
};

export const Logout = async (req: MyRequest<UserI>, res: Response) => {
  const result = await AuthServices.executeLogout(res);
  if (result instanceof SuccessResponseC)
    return SuccessResponse(
      res,
      result.code,
      result.data,
      result.message,
      result.status
    );
  if (result instanceof ErrorResponseC)
    return ErrorResponse(res, result.code, result.message, result.error);
};

export const ResetPassword = async (req: MyRequest<UserI>, res: Response) => {
  const { userId, newPassword } = req.body;
  const result = await AuthServices.executeResetPassword(userId, newPassword);
  if (result instanceof SuccessResponseC)
    return SuccessResponse(
      res,
      result.code,
      result.data,
      result.message,
      result.status
    );
  if (result instanceof ErrorResponseC)
    return ErrorResponse(res, result.code, result.message, result.error);
};

export const ResetTeacherPassword = async (
  req: MyRequest<UserI>,
  res: Response
) => {
  const { teacherId, newPassword } = req.body;
  const result = await AuthServices.executeResetTeacherPassword(
    teacherId,
    newPassword
  );
  if (result instanceof SuccessResponseC)
    return SuccessResponse(
      res,
      result.code,
      result.data,
      result.message,
      result.status
    );
  if (result instanceof ErrorResponseC)
    return ErrorResponse(res, result.code, result.message, result.error);
};