1
votes

I have a form on the login page with username and password text fields. Now wen the user registers for an account, I want to add him to the User model. And when he logs in I need to check whether the username/password combination is accurate to allow entry into the Sencha app.

I have no idea how to go about verifying the login/pass combo. Someone help !!! Here's my code.

The Model :

App.models.Users = Ext.regModel('Expense',{

fields : [
    { name : 'id', type : 'integer'},
    { name : 'email', type : 'string'},
    { name : 'pass', type : 'string'}
],

validations : [
    { name : 'email', type : 'presence', message : 'cannot leave field blank'},
    { name : 'pass', type : 'presence', message : 'cannot leave field blank'}
],

proxy : {
    type : 'localstorage',
    id : 'user-creds'
}
});

Store :

App.stores.User = new Ext.data.Store ({
  model : 'Users',
  autoLoad : true,
  autoSave : true
});

The Form is a basic one, with an xtype "textfield" for email, and "passwordfield" for password.

How do I verify that the details entered in the form match those stored in the localstorage???

1

1 Answers

4
votes

When you tap the button to login, you need to try and find the email address entered in the store.

App.stores.User.findRecord('email', < the value entered on the form >) ;

This method returns a record if a match is found. Once you have a record you can check that the password in the record matches the password entered on the form.

Refer to Store in the Sencha Touch docs

Update:

Assuming you're following the MVC pattern...

You need:

  1. The textfields in a FormPanel
  2. A controller

In the view, your login button dispatches to the controller:

    this.loginButton = new Ext.Button({
        text: 'Login',
        ui: 'action',
        handler: function() {
            Ext.dispatch({
                controller: App.controllers.loginController,
                action: 'login'
            });
        },
        scope: this
    });

In your controller, the code retrieves the form values:

'login': function (options) {

    var formDetails = App.views.loginView.getRecord();

    var store = Ext.StoreMgr.get('users');
    var record = store.findRecord('username', formDetails.get('username'), 0, false, true);

    if (record != null) {

      if (formDetails.get('password') == record.get('password')) {
        // valid login
      } else {
        // wrong password
      }          

    } else {
      // username doesn't exist
    }

 }

I hope this helps.