Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <section>
- <input type="text" id="_username" name="_username" placeholder="Email Address" />
- <input type="password" id="_password" name="_password" placeholder="Password" />
- <footer>
- <button data-iziModal-close>Cancel</button>
- <button id="form_login" class="submit">Log in</button>
- </footer>
- </section>
- $('#form_login').on('click', function(event) {
- event.preventDefault();
- $.ajax({
- type : 'post',
- url: '/login',
- data: '_username=abc&_password=temp123',
- success : function(response) {
- console.log(response);
- }
- });
- });
- services:
- acme.security.authentication_handler:
- class: UserBundleHandlerAuthenticationHandler
- public: false
- arguments: ["@router", '@session']
- security:
- encoders:
- UserBundleEntityUser: bcrypt
- providers:
- in_memory:
- memory: ~
- firewalls:
- dev:
- pattern: ^/(_(profiler|wdt)|css|images|js)/
- security: false
- main:
- anonymous: ~
- form_login:
- check_path: login
- success_handler: acme.security.authentication_handler
- failure_handler: acme.security.authentication_handler
- access_control:
- - { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- <?php
- namespace UserBundleHandler;
- use SymfonyComponentHttpFoundationResponse;
- use SymfonyComponentHttpFoundationRedirectResponse;
- use SymfonyComponentRoutingRouterInterface;
- use SymfonyComponentHttpFoundationSessionSession;
- use SymfonyComponentSecurityCoreAuthenticationTokenTokenInterface;
- use SymfonyComponentSecurityCoreExceptionAuthenticationException;
- use SymfonyComponentHttpFoundationRequest;
- use SymfonyComponentSecurityCoreSecurityContextInterface;
- use SymfonyComponentSecurityHttpAuthenticationAuthenticationSuccessHandlerInterface;
- use SymfonyComponentSecurityHttpAuthenticationAuthenticationFailureHandlerInterface;
- class AuthenticationHandler implements AuthenticationSuccessHandlerInterface, AuthenticationFailureHandlerInterface
- {
- private $router;
- private $session;
- public function __construct(RouterInterface $router, Session $session)
- {
- $this->router = $router;
- $this->session = $session;
- }
- public function onAuthenticationSuccess( Request $request, TokenInterface $token )
- {
- if ($request->isXmlHttpRequest()) {
- $array = array('success' => true);
- $response = new Response(json_encode($array));
- $response->headers->set('Content-Type', 'application/json');
- return $response;
- } else {
- if ( $this->session->get('_security.main.target_path')) {
- $url = $this->session->get('_security.main.target_path');
- } else {
- $url = $this->router->generate('home');
- }
- return new RedirectResponse($url);
- }
- }
- public function onAuthenticationFailure( Request $request, AuthenticationException $exception )
- {
- // if AJAX login
- if ($request->isXmlHttpRequest()) {
- $array = array('success' => false, 'message' => $exception->getMessage()); // data to return via JSON
- $response = new Response(json_encode( $array ));
- $response->headers->set('Content-Type', 'application/json');
- return $response;
- // if form login
- } else {
- // set authentication exception to session
- $request->getSession()->set(SecurityContextInterface::AUTHENTICATION_ERROR, $exception);
- return new RedirectResponse($this->router->generate('login'));
- }
- }
- }
- /**
- * @Route("/login", name="login")
- * @Template()
- */
- public function loginAction()
- {
- $authenticationUtils = $this->get('security.authentication_utils');
- $error = $authenticationUtils->getLastAuthenticationError();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement