Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
2.1 kB
1
Indexable
Never
    #[Route('/api/signin', name: 'signin', methods: ["POST"])]
    public function signin(Request $request, EntityManagerInterface $entityManager, JWTTokenManagerInterface $JWTManager): JsonResponse
    {
        $data = json_decode($request->getContent(), true);

        $email = $data['email'];
        $firstname = $data['firstname'];
        $lastname = $data['lastname'];
        $password = $data['password'];

        // $validator = Validation::createValidator();

        // $violations = $validator->validate($email, [
        //     new Email(),
        //     new Unique(),
        //     new NotBlank([
        //         "message" => "email.not_blank"
        //     ]),
        //     new Required()
        // ]);
        // $violations = $validator->validate($firstname, [
        //     new NotBlank([
        //         "message" => "firstname.not_blank"
        //     ]),
        //     new Required()
        // ]);
        // $violations = $validator->validate($lastname, [
        //     new NotBlank([
        //         "message" => "lastname.not_blank"
        //     ]),
        //     new Required()
        // ]);
        // $violations = $validator->validate($password, [
        //     new NotBlank([
        //         "message" => "password.not_blank"
        //     ]),
        //     new Required()
        // ]);

        // if (0 !== count($violations)) {
        //     foreach ($violations as $violation) {
        //         return new JsonResponse([
        //             "errors" => $violation->getMessage()
        //         ], JsonResponse::HTTP_BAD_REQUEST);
        //     }
        // }

        $user = new User();
        $user->setFirstname($firstname);
        $user->setLastname($lastname);
        $user->setEmail($email);
        $user->setPassword($this->userPasswordHasher->hashPassword($user, $password));
        $entityManager->persist($user);
        $entityManager->flush();

        return new JsonResponse(['token' => $JWTManager->create($user)]);
    }
Leave a Comment