<?php
namespace App\Controller\Api\Login;
use App\Controller\Common\ApiController;
use App\Exception\SerializerException;
use App\Exception\ValidationException;
use App\Serializer\Request\Company\User\SendPasswordResetLinkRequestSerializer;
use App\Serializer\Request\Login\ChangePasswordRequestSerializer;
use App\Serializer\Request\Login\LoginUserRequestSerializer;
use App\Serializer\Request\Login\ResetPasswordRequestSerializer;
use App\Service\Login\LoginService;
use Exception;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Validator\ValidatorInterface;
class LoginController extends ApiController
{
/**
* @Route("/api/user/login", methods={"POST"})
* @param Request $request
* @param ValidatorInterface $validator
* @param LoginUserRequestSerializer $loginUserRequestSerializer
* @param LoginService $companyUserService
* @return JsonResponse
*/
public function loginAction(Request $request,
ValidatorInterface $validator,
LoginUserRequestSerializer $loginUserRequestSerializer,
LoginService $companyUserService
): JsonResponse
{
try {
$requestJson = $request->getContent();
$requestArray = json_decode($requestJson, true);
$requestObject = $loginUserRequestSerializer->denormalize($requestArray);
$errors = $validator->validate($requestObject);
if (count($errors) > 0) {
throw new SerializerException($errors[0]->getMessage(), $errors[0]->getCode(), 400);
}
$response = $companyUserService->login($requestObject);
return $this->setRequest($request)->apiResponse($response);
} catch (Exception $e) {
return $this->setRequest($request)->exceptionResponse($e);
}
}
/**
* @Route("/api/user/logout")
* @param Request $request
* @param LoginService $companyUserService
* @return Response
*/
public function logout(Request $request, LoginService $companyUserService)
{
try {
$referer = $request->headers->get('referer');
if ($referer != "") {
$response = new RedirectResponse($referer);
$response->headers->clearCookie('_accessToken');
} else {
$response = ["msg" => "successfully logout"];
}
$companyUserService->logout();
if ($referer != "") {
return $response;
} else {
return $this->setRequest($request)->apiResponse($response);
}
} catch (Exception $e) {
return $this->setRequest($request)->exceptionResponse($e);
}
}
/**
* @Route("/api/user/me", methods={"GET"})
* @param Request $request
* @param LoginService $companyUserService
* @return JsonResponse
*/
public function meAction(Request $request,
LoginService $companyUserService
): JsonResponse
{
try {
$response = $companyUserService->me();
return $this
->setRequest($request)
->apiResponse($response);
} catch (Exception $e) {
return $this->setRequest($request)->exceptionResponse($e);
}
}
/**
* @Route("/api/user/change-password", methods={"POST"})
* @param Request $request
* @param ValidatorInterface $validator
* @param ChangePasswordRequestSerializer $changePasswordRequestSerializer
* @param LoginService $companyUserService
* @return JsonResponse
*/
public function changePasswordAction(Request $request,
ValidatorInterface $validator,
ChangePasswordRequestSerializer $changePasswordRequestSerializer,
LoginService $companyUserService): JsonResponse
{
try {
$requestJson = $request->getContent();
$requestArray = json_decode($requestJson, true);
$requestObject = $changePasswordRequestSerializer->denormalize($requestArray);
$errors = $validator->validate($requestObject);
if (count($errors) > 0) {
throw new ValidationException($errors[0]->getMessage(), 400);
}
$response = $companyUserService->changePasswordService($requestObject);
return $this
->setRequest($request)
->apiResponse($response);
} catch (Exception $e) {
return $this->setRequest($request)->exceptionResponse($e);
}
}
/**
* @Route("/api/user/reset-password", methods={"POST"})
* @param Request $request
* @param ValidatorInterface $validator
* @param ResetPasswordRequestSerializer $resetPasswordRequestSerializer
* @param LoginService $userService
* @return JsonResponse
* @throws SerializerException
* @throws ValidationException
*/
public function resetPasswordAction(Request $request,
ValidatorInterface $validator,
ResetPasswordRequestSerializer $resetPasswordRequestSerializer,
LoginService $userService
)
{
$requestJson = $request->getContent();
$requestArray = json_decode($requestJson, true);
$requestObject = $resetPasswordRequestSerializer->denormalize($requestArray);
$errors = $validator->validate($requestObject);
if (count($errors) > 0) {
throw new ValidationException($errors[0]->getMessage(), 400);
}
$response = $userService->resetPasswordService($requestObject);
return $this
->setRequest($request)
->apiResponse($response);
}
}