当我使用钛应用程序进行身份验证过程时,我应该考虑什么?

时间:2015-03-27 22:47:52

标签: titanium titanium-alloy signing

您好,这是我第一次使用Titanium在移动应用中进行登录过程,我想知道应该保存哪些信息以及最佳做法?

我的服务器以这种方式配置:

  • 服务器要求我发送用户和密码,如果信息匹配则提供令牌会话。

这是我用于登录的代码:

 function signIn(e) {

//function to use HTTP to connect to a web server and transfer the data.
var sendit = Ti.Network.createHTTPClient({
    onerror : function(e) {
        Ti.API.debug(e.error);
        alert('There was an error during the connection');
    },
    timeout : 100000,
});
//Here you have to change it for your local ip
sendit.open('POST', 'http://myserver');
var params = {
    user : $.txtUsuario.value,
    password : $.txtPassword.value
};
sendit.send(params);
//Function to be called upon a successful response
sendit.onload = function() {
    var json = this.responseText;  
    var response = JSON.parse(json);  
    if (response.success == "true")  
    {  
        var landing = Alloy.createController("menu").getView();
        $.index.close();
        landing.open();

    }  
    else  
    {  
        alert(response);  
    }  
};
}; 

上面的代码正在运行,但我不知道如何管理退出。我希望我的应用程序像大多数应用程序一样工作,例如:

您已登录一次,之后如果您不关闭应用程序,则可以继续使用该应用程序甚至提出请求。

感谢您的任何解释。

1 个答案:

答案 0 :(得分:1)

这取决于您的应用程序要求。例如,如果您稍后在应用中使用该令牌,则可以将其另存为AppProperty

Ti.App.Properties.setString('token',yourTokenGoHere);

在应用程序启动时,您可以取回它:

var myToken = Ti.App.Properties.getString('token');

然后您可以进行测试,例如,如果令牌仍然有效:

if(myToken === 'invalidtoken')
   youSholdLogin();
else 
   youCanGoFurther();

当用户断开连接时,令牌无效:

Ti.App.Properties.setString('token', 'invalidtoken');