Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. class LoginController extends Controller
  2. {
  3. /**
  4. * Matches /login exactly
  5. *
  6. * @Route("/login", name="login")
  7. *
  8. * @param Request $request
  9. *
  10. * @return Response
  11. */
  12. public function loginAction(Request $request)
  13. {
  14. $email = $request->query->get('email');
  15.  
  16. $password = $request->query->get('password');
  17.  
  18. if (empty($email) || empty($password)) {
  19. return new Response(
  20. 'Email or password can not be blank',
  21. Response::HTTP_UNAUTHORIZED,
  22. ['Content-type' => 'application/json',]
  23. );
  24. }
  25.  
  26. /** @var EncoderFactory $factory */
  27. $factory = $this->get('security.encoder_factory');
  28.  
  29. /** @var UserManager $user_manager */
  30. $user_manager = $this->get('fos_user.user_manager');
  31.  
  32. /** @var User $user */
  33. $user = $user_manager->findUserByEmail($email);
  34.  
  35. if (!$user) {
  36. return new Response(
  37. 'Email doesnt exists',
  38. Response::HTTP_UNAUTHORIZED,
  39. ['Content-type' => 'application/json',]
  40. );
  41. }
  42.  
  43. $encoder = $factory->getEncoder($user);
  44.  
  45. $salt = $user->getSalt();
  46.  
  47. if (!$encoder->isPasswordValid($user->getPassword(), $password, $salt)) {
  48. return new Response(
  49. 'Email or Password not valid.',
  50. Response::HTTP_UNAUTHORIZED,
  51. ['Content-type' => 'application/json',]
  52. );
  53. }
  54.  
  55. $token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
  56.  
  57. $this->get('security.token_storage')->setToken($token);
  58.  
  59. $this->get('session')->set('_security_main', serialize($token));
  60.  
  61. $event = new InteractiveLoginEvent($request, $token);
  62.  
  63. $this->get("event_dispatcher")->dispatch("security.interactive_login", $event);
  64.  
  65. return new Response(
  66. 'Welcome '. $user->getUsername(),
  67. Response::HTTP_OK,
  68. ['Content-type' => 'application/json']
  69. );
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement