Start to use accessor functions when working with session information.

* Add get/set/clear functionality for Session.
This commit is contained in:
Shane Tomlinson 2014-01-23 12:21:18 +00:00
Родитель 95c2f5816b
Коммит 0eb843ad11
4 изменённых файлов: 122 добавлений и 14 удалений

Просмотреть файл

@ -42,12 +42,14 @@ function (FxaClient, $, p, Session) {
.then(function (client) {
return client.signIn(email, password, { keys: true });
})
.then(function (accountData) {
Session.email = email;
Session.sessionToken = accountData.sessionToken;
Session.keyFetchToken = accountData.keyFetchToken;
Session.unwrapBKey = accountData.unwrapBKey;
Session.uid = accountData.uid;
.then(function (accountData) {
Session.set({
email: email,
uid: accountData.uid,
unwrapBKey: accountData.unwrapBKey,
keyFetchToken: accountData.keyFetchToken,
sessionToken: accountData.sessionToken
});
return accountData;
});
@ -71,11 +73,11 @@ function (FxaClient, $, p, Session) {
return client.sessionDestroy(Session.sessionToken);
})
.then(function () {
Session.email = null;
Session.uid = null;
Session.unwrapBKey = null;
Session.keyFetchToken = null;
Session.sessionToken = null;
Session.clear('email');
Session.clear('uid');
Session.clear('unwrapBKey');
Session.clear('keyFetchToken');
Session.clear('sessionToken');
});
},

Просмотреть файл

@ -4,8 +4,46 @@
'use strict';
define([], function() {
// Singleton
return {};
define([
'underscore'
], function(_) {
var RESERVED = [
'get',
'set',
'clear'
];
return {
set: function(key, value) {
if (typeof value === 'undefined' && typeof key === 'object') {
return _.each(key, function(value, key) {
this.set(key, value);
}, this);
}
if (RESERVED.indexOf(key) === -1) {
this[key] = value;
}
},
get: function(key) {
return this[key];
},
clear: function(key) {
// no key specified, clear everything.
if (typeof key === 'undefined') {
for (var key in this) {
this.clear(key);
}
return;
}
if (this.hasOwnProperty(key) && RESERVED.indexOf(key) === -1) {
this[key] = null;
delete this[key];
}
}
};
}
);

Просмотреть файл

@ -53,6 +53,7 @@ require([
'../tests/spec/lib/channels/fx-desktop',
'../tests/spec/lib/xss',
'../tests/spec/lib/url',
'../tests/spec/lib/session',
'../tests/spec/lib/fxa-client',
'../tests/spec/views/base',
'../tests/spec/views/sign_up',

Просмотреть файл

@ -0,0 +1,67 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict';
define([
'mocha',
'chai',
'underscore',
'lib/session'
],
function (mocha, chai, _, Session) {
var assert = chai.assert;
describe('lib/session', function () {
beforeEach(function() {
Session.clear();
});
afterEach(function() {
Session.clear();
});
describe('set', function() {
it('can take a key value pair', function() {
Session.set('key', 'value');
assert.equal(Session.key, 'value');
});
it('can take an object', function() {
Session.set({
key2: 'value2',
key3: 'value3'
});
assert.equal(Session.key2, 'value2');
assert.equal(Session.key3, 'value3');
});
});
describe('clear', function() {
it('with a key clears item', function() {
Session.set({
key4: 'value4'
});
Session.clear('key4');
assert.isUndefined(Session.key4);
});
it('with no key clears all items', function() {
Session.set({
key5: 'value5',
key6: 'value6'
});
Session.clear();
assert.isUndefined(Session.key5);
assert.isUndefined(Session.key6);
});
});
});
});