Question

I 'm beginner in sencha touch 2 and i need to call webservice.The code of web service is:

public function loginAction()
    {
        $request  = $this->getRequest();
        $username = $request->request->get('username');
        $password= $request->request->get('password');

        $success=false;
        $token="error";
        $error="test";

        $userManager = $this->get('fos_user.user_manager');
        $user = $userManager->findUserBy(array('username' => $username));

        if (! $user)
           {

          $token="username not exists";
          $success=false;
           }
      else
        {

        $password_user=$user->getPassword();

        $factory = $this->get('security.encoder_factory'); 
        $encoder = $factory->getEncoder($user); 
        $password = $encoder->encodePassword($password, $user->getSalt()); 


         if ($password!=$password_user)
         {
           $token="password incorrect"; 
           $success=false;
         }


          else 
          {
            $token="logged";
             $success=true;


          } 


        }


        $info= array("token" => $token);

        $res=array("success"=> $success);

        $res = array("status"=> $res,
                      "resultat"=>$info
                      );

        $serializer = $this->get('jms_serializer');
        $response = $serializer->serialize($res,'json');

        return new Response($response);

    }

and i make in project sencha a view: Login.js and a controller: Login.js. The problem is in calling the web service and the code in controller Login.js:

Ext.define('Sample.controller.Login', {
    extend: 'Ext.app.Controller',
    config: {
        refs: {
            loginView: 'loginview',
            mainMenuView: 'mainmenuview'
        },
        control: {
            loginView: {
                signInCommand: 'onSignInCommand'
            },
            mainMenuView: {
                onSignOffCommand: 'onSignOffCommand'
            }
        }
    },

    // Session token

    sessionToken: null,

    // Transitions
    getSlideLeftTransition: function () {
        return { type: 'slide', direction: 'left' };
    },

    getSlideRightTransition: function () {
        return { type: 'slide', direction: 'right' };
    },

    onSignInCommand: function (view, username, password) {

        console.log('Username: ' + username + '\n' + 'Password: ' + password);

        var me = this,
            loginView = me.getLoginView();

        if (username.length === 0 || password.length === 0) {

            loginView.showSignInFailedMessage('Please enter your username and password.');
            return;
        }

        loginView.setMasked({
            xtype: 'loadmask',
            message: 'Signing In...'
        });

        Ext.Ajax.request({
            url: 'http://localhost/dawinilastversion/web/app_dev.php/api/login',
            method: 'post',
            params: {
                username: username,
                password: password
            },
            success: function (response) {

                var loginResponse = Ext.JSON.decode(response.responseText);

                if (loginResponse.success === "true") {
                    // The server will send a token that can be used throughout the app to confirm that the user is authenticated.
                    me.sessionToken = loginResponse.sessionToken;
                    me.signInSuccess();     //Just simulating success.
                } else {
                  me.signInFailure(loginResponse.message);
                   console.log('erreur.');
                }
            },
            failure: function (response) {
                me.sessionToken = null;
                me.signInFailure('Login failed. Please try again later.');
            }
        });
    },

    signInSuccess: function () {
        console.log('Signed in.');
        var loginView = this.getLoginView();
        mainMenuView = this.getMainMenuView();
        loginView.setMasked(false);

        Ext.Viewport.animateActiveItem(mainMenuView, this.getSlideLeftTransition());
    },

    singInFailure: function (message) {
        var loginView = this.getLoginView();
        loginView.showSignInFailedMessage(message);
        loginView.setMasked(false);
    },

    onSignOffCommand: function () {

        var me = this;

        Ext.Ajax.request({
            url: 'http://localhost/dawinilastversion/web/app_dev.php/api/login',
            method: 'post',
            params: {
                sessionToken: me.sessionToken
            },
            success: function (response) {

                // TODO: You need to handle this condition.
            },
            failure: function (response) {

                // TODO: You need to handle this condition.
            }
        });

        Ext.Viewport.animateActiveItem(this.getLoginView(), this.getSlideRightTransition());
    }
});

And the web service login work with success:enter image description hereenter image description here

Was it helpful?

Solution

you need to go one step deeper into the JSON response:

if (loginResponse.status.success === true)

and check against the bool true not the string true

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top