我對使用Sencha Touch 2庫進行開發完全陌生。我遵循本教程來幫助創建一個簡單的登錄腳本(http://miamicoder.com/2012/adding-a-login-screen-to-a-sencha-touch-application-part-2/)。我不確定的一件事是如何檢查會話令牌是否存在於客戶端,以便當它們返回到應用程序時,登錄屏幕不會顯示它們是否經過身份驗證。Sencha Touch 2:在應用程序啓動時檢查cookie
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();
}
});
謝謝你的建議!那麼你是否在App啓動時說,我應該ping服務器以確保用戶已登錄並檢查cookie? – dennismonsewicz
是的,ping服務器是我總是這樣做,並會推薦。如果你使用的會話聽起來像你一樣,檢查他們是否登錄到服務器上會使用cookie(它們每次都在http請求中來回發送)。服務器從http請求中的cookie中提取會話ID,檢查它是否仍在跟蹤該ID,以及是否已過期。我敢肯定這有點語言/環境依賴,但我敢肯定,這是大多數Web開發堆棧中用戶跟蹤背後的一般想法。 –
太棒了!非常感謝!無論如何,你可以提供一個在應用程序啓動時ping服務器的例子嗎? – dennismonsewicz