src/Controller/Api/Login/LoginController.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Api\Login;
  3. use App\Controller\Common\ApiController;
  4. use App\Exception\SerializerException;
  5. use App\Exception\ValidationException;
  6. use App\Serializer\Request\Company\User\SendPasswordResetLinkRequestSerializer;
  7. use App\Serializer\Request\Login\ChangePasswordRequestSerializer;
  8. use App\Serializer\Request\Login\LoginUserRequestSerializer;
  9. use App\Serializer\Request\Login\ResetPasswordRequestSerializer;
  10. use App\Service\Login\LoginService;
  11. use Exception;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use Symfony\Component\HttpFoundation\RedirectResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. use Symfony\Component\Validator\Validator\ValidatorInterface;
  18. class LoginController extends ApiController
  19. {
  20.     /**
  21.      * @Route("/api/user/login", methods={"POST"})
  22.      * @param Request $request
  23.      * @param ValidatorInterface $validator
  24.      * @param LoginUserRequestSerializer $loginUserRequestSerializer
  25.      * @param LoginService $companyUserService
  26.      * @return JsonResponse
  27.      */
  28.     public function loginAction(Request $request,
  29.                                 ValidatorInterface $validator,
  30.                                 LoginUserRequestSerializer $loginUserRequestSerializer,
  31.                                 LoginService $companyUserService
  32.     ): JsonResponse
  33.     {
  34.         try {
  35.             $requestJson $request->getContent();
  36.             $requestArray json_decode($requestJsontrue);
  37.             $requestObject $loginUserRequestSerializer->denormalize($requestArray);
  38.             $errors $validator->validate($requestObject);
  39.             if (count($errors) > 0) {
  40.                 throw new SerializerException($errors[0]->getMessage(), $errors[0]->getCode(), 400);
  41.             }
  42.             $response $companyUserService->login($requestObject);
  43.             return $this->setRequest($request)->apiResponse($response);
  44.         } catch (Exception $e) {
  45.             return $this->setRequest($request)->exceptionResponse($e);
  46.         }
  47.     }
  48.     /**
  49.      * @Route("/api/user/logout")
  50.      * @param Request $request
  51.      * @param LoginService $companyUserService
  52.      * @return Response
  53.      */
  54.     public function logout(Request $requestLoginService $companyUserService)
  55.     {
  56.         try {
  57.             $referer $request->headers->get('referer');
  58.             if ($referer != "") {
  59.                 $response = new RedirectResponse($referer);
  60.                 $response->headers->clearCookie('_accessToken');
  61.             } else {
  62.                 $response = ["msg" => "successfully logout"];
  63.             }
  64.             $companyUserService->logout();
  65.             if ($referer != "") {
  66.                 return $response;
  67.             } else {
  68.                 return $this->setRequest($request)->apiResponse($response);
  69.             }
  70.         } catch (Exception $e) {
  71.             return $this->setRequest($request)->exceptionResponse($e);
  72.         }
  73.     }
  74.     /**
  75.      * @Route("/api/user/me", methods={"GET"})
  76.      * @param Request $request
  77.      * @param LoginService $companyUserService
  78.      * @return JsonResponse
  79.      */
  80.     public function meAction(Request $request,
  81.                              LoginService $companyUserService
  82.     ): JsonResponse
  83.     {
  84.         try {
  85.             $response $companyUserService->me();
  86.             return $this
  87.                 ->setRequest($request)
  88.                 ->apiResponse($response);
  89.         } catch (Exception $e) {
  90.             return $this->setRequest($request)->exceptionResponse($e);
  91.         }
  92.     }
  93.     /**
  94.      * @Route("/api/user/change-password", methods={"POST"})
  95.      * @param Request $request
  96.      * @param ValidatorInterface $validator
  97.      * @param ChangePasswordRequestSerializer $changePasswordRequestSerializer
  98.      * @param LoginService $companyUserService
  99.      * @return JsonResponse
  100.      */
  101.     public function changePasswordAction(Request $request,
  102.                                          ValidatorInterface $validator,
  103.                                          ChangePasswordRequestSerializer $changePasswordRequestSerializer,
  104.                                          LoginService $companyUserService): JsonResponse
  105.     {
  106.         try {
  107.             $requestJson $request->getContent();
  108.             $requestArray json_decode($requestJsontrue);
  109.             $requestObject $changePasswordRequestSerializer->denormalize($requestArray);
  110.             $errors $validator->validate($requestObject);
  111.             if (count($errors) > 0) {
  112.                 throw new ValidationException($errors[0]->getMessage(), 400);
  113.             }
  114.             $response $companyUserService->changePasswordService($requestObject);
  115.             return $this
  116.                 ->setRequest($request)
  117.                 ->apiResponse($response);
  118.         } catch (Exception $e) {
  119.             return $this->setRequest($request)->exceptionResponse($e);
  120.         }
  121.     }
  122.     /**
  123.      * @Route("/api/user/reset-password", methods={"POST"})
  124.      * @param Request $request
  125.      * @param ValidatorInterface $validator
  126.      * @param ResetPasswordRequestSerializer $resetPasswordRequestSerializer
  127.      * @param LoginService $userService
  128.      * @return JsonResponse
  129.      * @throws SerializerException
  130.      * @throws ValidationException
  131.      */
  132.     public function resetPasswordAction(Request $request,
  133.                                         ValidatorInterface $validator,
  134.                                         ResetPasswordRequestSerializer $resetPasswordRequestSerializer,
  135.                                         LoginService $userService
  136.     )
  137.     {
  138.         $requestJson $request->getContent();
  139.         $requestArray json_decode($requestJsontrue);
  140.         $requestObject $resetPasswordRequestSerializer->denormalize($requestArray);
  141.         $errors $validator->validate($requestObject);
  142.         if (count($errors) > 0) {
  143.             throw new ValidationException($errors[0]->getMessage(), 400);
  144.         }
  145.         $response $userService->resetPasswordService($requestObject);
  146.         return $this
  147.             ->setRequest($request)
  148.             ->apiResponse($response);
  149.     }
  150. }