Advertisement
Guest User

Untitled

a guest
Nov 4th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. <section>
  2. <input type="text" id="_username" name="_username" placeholder="Email Address" />
  3. <input type="password" id="_password" name="_password" placeholder="Password" />
  4. <footer>
  5. <button data-iziModal-close>Cancel</button>
  6. <button id="form_login" class="submit">Log in</button>
  7. </footer>
  8. </section>
  9.  
  10. $('#form_login').on('click', function(event) {
  11. event.preventDefault();
  12. $.ajax({
  13. type : 'post',
  14. url: '/login',
  15. data: '_username=abc&_password=temp123',
  16. success : function(response) {
  17. console.log(response);
  18. }
  19. });
  20. });
  21.  
  22. services:
  23. acme.security.authentication_handler:
  24. class: UserBundleHandlerAuthenticationHandler
  25. public: false
  26. arguments: ["@router", '@session']
  27.  
  28. security:
  29. encoders:
  30. UserBundleEntityUser: bcrypt
  31. providers:
  32. in_memory:
  33. memory: ~
  34.  
  35. firewalls:
  36. dev:
  37. pattern: ^/(_(profiler|wdt)|css|images|js)/
  38. security: false
  39. main:
  40. anonymous: ~
  41. form_login:
  42. check_path: login
  43. success_handler: acme.security.authentication_handler
  44. failure_handler: acme.security.authentication_handler
  45. access_control:
  46. - { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY }
  47.  
  48. <?php
  49.  
  50. namespace UserBundleHandler;
  51.  
  52. use SymfonyComponentHttpFoundationResponse;
  53. use SymfonyComponentHttpFoundationRedirectResponse;
  54. use SymfonyComponentRoutingRouterInterface;
  55. use SymfonyComponentHttpFoundationSessionSession;
  56. use SymfonyComponentSecurityCoreAuthenticationTokenTokenInterface;
  57. use SymfonyComponentSecurityCoreExceptionAuthenticationException;
  58. use SymfonyComponentHttpFoundationRequest;
  59. use SymfonyComponentSecurityCoreSecurityContextInterface;
  60. use SymfonyComponentSecurityHttpAuthenticationAuthenticationSuccessHandlerInterface;
  61. use SymfonyComponentSecurityHttpAuthenticationAuthenticationFailureHandlerInterface;
  62.  
  63. class AuthenticationHandler implements AuthenticationSuccessHandlerInterface, AuthenticationFailureHandlerInterface
  64. {
  65. private $router;
  66. private $session;
  67.  
  68. public function __construct(RouterInterface $router, Session $session)
  69. {
  70. $this->router = $router;
  71. $this->session = $session;
  72. }
  73.  
  74. public function onAuthenticationSuccess( Request $request, TokenInterface $token )
  75. {
  76. if ($request->isXmlHttpRequest()) {
  77. $array = array('success' => true);
  78. $response = new Response(json_encode($array));
  79. $response->headers->set('Content-Type', 'application/json');
  80. return $response;
  81. } else {
  82. if ( $this->session->get('_security.main.target_path')) {
  83. $url = $this->session->get('_security.main.target_path');
  84. } else {
  85. $url = $this->router->generate('home');
  86. }
  87. return new RedirectResponse($url);
  88. }
  89. }
  90.  
  91. public function onAuthenticationFailure( Request $request, AuthenticationException $exception )
  92. {
  93. // if AJAX login
  94. if ($request->isXmlHttpRequest()) {
  95. $array = array('success' => false, 'message' => $exception->getMessage()); // data to return via JSON
  96. $response = new Response(json_encode( $array ));
  97. $response->headers->set('Content-Type', 'application/json');
  98. return $response;
  99. // if form login
  100. } else {
  101. // set authentication exception to session
  102. $request->getSession()->set(SecurityContextInterface::AUTHENTICATION_ERROR, $exception);
  103. return new RedirectResponse($this->router->generate('login'));
  104. }
  105. }
  106. }
  107.  
  108. /**
  109. * @Route("/login", name="login")
  110. * @Template()
  111. */
  112. public function loginAction()
  113. {
  114. $authenticationUtils = $this->get('security.authentication_utils');
  115. $error = $authenticationUtils->getLastAuthenticationError();
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement