我对使用Sencha Touch 2库进行开发完全陌生。我遵循本教程来帮助创建一个简单的登录脚本(http://miamicoder.com/2012/adding-a-login-screen-to-a-sencha-touch-application-part-2/)。我不确定的一件事是如何检查客户端上是否存在会话令牌,这样当他们返回应用程序时,登录屏幕就不会显示他们是否通过了身份验证。
app.js
Ext.application({
name: 'RecruitTalkTouch',
views: ['Login', 'MainMenu'],
controllers: ['Login'],
launch: function () {
Ext.Viewport.add([
{ xtype: 'loginview' },
{ xtype: 'mainmenuview' }
]);
}
});Login.js控制器:
Ext.define('RecruitTalkTouch.controller.Login', {
extend: 'Ext.app.Controller',
config: {
refs: {
loginView: 'loginview',
mainMenuView: 'mainmenuview'
},
control: {
loginView: {
signInCommand: 'onSignInCommand'
},
mainMenuView: {
signOffCommand: 'onSignOffCommand'
}
}
},
onSignInCommand: function(view, username, 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:3000/api/v1/sessions.json',
method: 'POST',
useDefaultXhrHeader: false,
params: {
login: username,
password: password
},
success: function(resp) {
var json = Ext.JSON.decode(resp.responseText);
if(json.success === "true") {
me.sessionToken = json.auth_token;
me.signInSuccess();
} else {
me.signInFailure(json.message)
}
},
failure: function(resp) {
me.sessionToken = null;
me.signInFailure('Login failed. Please try again');
}
});
},
signInSuccess: function() {
console.log("Signed In");
var loginView = this.getLoginView(),
mainMenuView = this.getMainMenuView();
loginView.setMasked(false);
Ext.Viewport.animateActiveItem(mainMenuView, this.transition('slide', 'left'));
},
signInFailure: function(message) {
var loginView = this.getLoginView();
loginView.showSignInFailedMessage(message);
loginView.setMasked(false);
},
transition: function(type, direction) {
return { type: type, direction: direction };
},
onSignOffCommand: function() {
var me = this;
me.sessionToken = null;
Ext.Viewport.animateActiveItem(this.getLoginView(), this.transition('slide', 'right'));
}
});Login.js视图:
Ext.define('RecruitTalkTouch.view.Login', {
extend: 'Ext.form.Panel',
alias: "widget.loginview",
requires: ['Ext.form.FieldSet', 'Ext.form.Password', 'Ext.Label', 'Ext.Button'],
config: {
title: 'Login',
items: [
{
xtype: 'label',
html: 'Login failed. Please enter the correct credentials.',
itemId: 'signInFailed',
hidden: true,
hideAnimation: 'fadeOut',
showAnimation: 'fadeIn'
},
{
xtype: 'fieldset',
title: 'Login',
items: [
{
xtype: 'textfield',
placeHolder: 'Username',
itemId: 'userNameTextField',
name: 'userNameTextField',
required: true
},
{
xtype: 'passwordfield',
placeHolder: 'Password',
itemId: 'passwordTextField',
name: 'passwordTextField',
required: true
}
]
},
{
xtype: 'button',
itemId: 'logInButton',
ui: 'action',
padding: '10px',
text: 'Log In'
}
],
listeners: [{
delegate: '#logInButton',
event: 'tap',
fn: 'onLogInButtonTap'
}]
},
onLogInButtonTap: function() {
var me = this,
usernameField = me.down('#userNameTextField'),
passwordField = me.down('#passwordTextField'),
label = me.down('#signInFailed'),
username = usernameField.getValue(),
password = passwordField.getValue();
label.hide();
var task = Ext.create('Ext.util.DelayedTask', function(){
label.setHtml('');
me.fireEvent('signInCommand', me, username, password);
usernameField.setValue('');
passwordField.setValue('');
});
task.delay(500);
},
showSignInFailedMessage: function(message) {
var label = this.down('#signInFailed');
label.setHtml(message);
label.show();
}
});发布于 2013-11-04 10:28:55
您可能会发现,只需在服务器上添加一个检查函数来检查有效会话会更容易。即使cookie在那里,也并不意味着他们还没有注销,会话cookie会随着每个请求来回发送。
也就是说,如果您想从客户端访问cookie,它们将作为单个字符串值附加到文档。你可以在document.cookies上访问它们。我将查看ExtJS cookie实用程序的文档和源代码,以获得有关如何在该字符串中查找cookie的一些灵感。
Ext.util.Cookies Documentation
Ext.util.Cookies Source
本质上,这个过程是遍历cookie字符串中的每个字母,提取会话密钥名称长度的子字符串,查看是否匹配,然后提取下一个等号和分号之间的数据。然后,取消对值的百分比编码,就可以开始了。
编辑要ping服务器,您只需在显示第一个视图之前,在应用程序加载过程中的某个位置发出一个Ajax请求。启动功能是一个非常自然的地方,我认为:
Ext.application({
name: 'App',
requires: [],
models: [],
stores: [],
views: [],
controllers: [],
....
launch: function () {
//Check with the server
Ext.Ajax.request({
//Proxy Settings
url: 'path/to/check/script.php',
//Callbacks
success: function (response, opts) {
// process server response here
response = Ext.JSON.decode(response.responseText);
if(response && response.success === true) {
//Load the normal first view
Ext.Viewport.add(Ext.create('App.view.Main');
} else {
//Load the login view
Ext.Viewport.add(Ext.create('App.view.LoginForm');
}
},
failure: function (response, opts) {
//Notify of network failure
}
});
}
});至于在服务器端的脚本中做什么,这取决于您正在使用哪种语言进行开发,以及您在进行身份验证时使用的是什么。如果它只是一个用户名和密码的数据库,那么当用户第一次登录时,启动一个会话,并为他们的userID和密码存储一个会话变量。在check功能中,首先检查当前会话是否存在,然后根据您的数据库重新进行身份验证,以确保它们仍然有效(以防您或其他人在会话仍然有效时注销了他们的帐户)。然后,只需返回一个json响应,表明他们是否已登录,并包含使应用程序正常工作所需的任何相关用户信息。
发布于 2015-02-06 18:40:45
在Sencha Touch中使用:
var cookiestr = document.cookie.split('; ');
var cookieObjArr = new Array();
for (var i=0; i<cookiestr.length; ++i)
{
if (i in cookiestr)
{
tmp = cookiestr[i].split('=');
cookieObjArr.push({"key": tmp[0], "value": tmp[1]});
}
}https://stackoverflow.com/questions/19749596
复制相似问题