php - Use of ajax in Silex Framework -


i'm pretty new silex framework , wondering how make simple login (using securityserviceprovider) ajax request. works in code (see below) how can change html page returned boolean giving true or false wether login worked or not.

app.php

use symfony\component\debug\errorhandler; use symfony\component\debug\exceptionhandler;  // register global error , exception handlers errorhandler::register(); exceptionhandler::register();  // register service providers $app->register(new silex\provider\doctrineserviceprovider()); $app->register(new silex\provider\twigserviceprovider(), array(     'twig.path' => __dir__ . '/../views', )); $app->register(new silex\provider\urlgeneratorserviceprovider()); $app->register(new silex\provider\sessionserviceprovider()); $app->register(new silex\provider\securityserviceprovider(), array(     'security.firewalls' => array(         'secured' => array(             'pattern' => '^/',             'anonymous' => true,             'logout' => array('logout_path' => '/admin/logout', 'invalidate_session' => true),             'form' => array('login_path' => 'login', 'check_path' => '/login_check'),             'users' => $app->share(function () use ($app) {                 return new ski\dao\memberdao($app['db']);             }),         ),     ), ));  // register services $app['dao.member'] = $app->share(function ($app) {     return new ski\dao\memberdao($app['db']); }); 

routes.php

use symfony\component\httpfoundation\request;  // home page $app->get('/', function () use ($app) {     return $app['twig']->render('index.html.twig'); })->bind('home');  // todo : never called $app->post('/ajax/login/', function (request $request) use ($app) {     // here : how return if login performed ?     return $app['security.last_error']($request); })->bind('ajax_login');  $app->get('/login/', function (request $request) use ($app) {     return $app['twig']->render('login.html.twig', array(         'error' => $app['security.last_error']($request),         'last_username' => $app['session']->get('_security.last_username'),     )); })->bind('login');  $app->get('/includes/header/', function () use ($app) {     return $app['twig']->render('header.html.twig'); })->bind('header'); 

and login.js

// connexion $(document).on('click', '#connexion_submit_button', function () {     // connexion ajax     var username = $('#connexion_pseudo').val();     var password = $('#connexion_password').val();      $.ajax({         type: 'post',         url: '/ski/web/login_check',         data: '_username=' + username + '&_password=' + password,         beforesend: function () {             $('#connexion_submit_button').html('patientez...');         },         success: function (data) {              // todo : generate custom animations if user logged or not             console.log(data);             $('#connexion_submit_button').html('connexion');          }     });     return false; }); 

any suggestions appreciated !

and way, there manners ajax in such frameworks ?

authentication success , failure handlers create , manage response. need custom authentication success , failure handlers if want make custom response.

add handlers

myauthenticationfailurehandler

use symfony\component\security\http\authentication\defaultauthenticationfailurehandler basedefaultauthenticationfailurehandler; use symfony\component\httpfoundation\request; use symfony\component\httpfoundation\jsonresponse; use symfony\component\security\core\exception\authenticationexception;  class myauthenticationfailurehandler extends basedefaultauthenticationfailurehandler {     public function onauthenticationfailure(request $request, authenticationexception $exception)     {         if ($request->isxmlhttprequest()) return new jsonresponse(['login' => false]);         return parent::onauthenticationfailure($request, $exception);     } } 

myauthenticationsuccesshandler

use symfony\component\security\http\authentication\defaultauthenticationsuccesshandler basedefaultauthenticationsuccesshandler; use symfony\component\httpfoundation\request; use symfony\component\httpfoundation\jsonresponse; use symfony\component\security\core\authentication\token\tokeninterface;  class myauthenticationsuccesshandler extends basedefaultauthenticationsuccesshandler {     public function onauthenticationsuccess(request $request, tokeninterface $token)     {         if ($request->isxmlhttprequest()) return new jsonresponse(['login' => true]);         return parent::onauthenticationsuccess($request, $token);     } } 

and register them in security service provider

$app['security.authentication.success_handler.secured'] = $app->share(function () use ($app) {     $handler = new myauthenticationsuccesshandler(         $app['security.http_utils'],         $app['security.firewalls']['secured']['form'] //$options,     );     $handler->setproviderkey('secured');      return $handler; });  $app['security.authentication.failure_handler.secured'] = $app->share(function () use ($app) {     return new myauthenticationfailurehandler(         $app,         $app['security.http_utils'],         $app['security.firewalls']['secured']['form'],         $app['logger']     ); }); 

Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -