Explicitely handle sign in/out.

This commit is contained in:
Julien Fontanet 2015-02-23 12:10:45 +01:00
parent d0b37d0f9a
commit 68e863723a
2 changed files with 124 additions and 43 deletions

View File

@ -0,0 +1,34 @@
var xoLib = require('./');
var xo = new xoLib.Xo({
url: 'localhost:9000',
});
xo.call('acl.get', {}).then(function (result) {
console.log('baz', result);
}).catch(function (error) {
console.log('error', error)
});
xo.signIn({
email: 'admin@admin.net',
password: 'admin',
}).then(function () {
console.log('foo', xo.user);
}).catch(function (error) {
console.log('error', error)
});
xo.signIn({
email: 'tom',
password: 'tom',
}).then(function () {
console.log('bar', xo.user);
}).catch(function (error) {
console.log('error', error)
});
xo.call('acl.get', {}).then(function (result) {
console.log('plop', result);
}).catch(function (error) {
console.log('error', error)
})

View File

@ -3,6 +3,7 @@
//==================================================================== //====================================================================
var Bluebird = require('bluebird'); var Bluebird = require('bluebird');
Bluebird.longStackTraces();
var EventEmitter = require('events').EventEmitter; var EventEmitter = require('events').EventEmitter;
var inherits = require('util').inherits; var inherits = require('util').inherits;
var jsonRpc = require('json-rpc'); var jsonRpc = require('json-rpc');
@ -33,6 +34,19 @@ function makeDeferred() {
}; };
} }
function makeStandaloneDeferred() {
var resolve, reject;
var promise = new Bluebird(function (resolve_, reject_) {
resolve = resolve_;
reject = reject_;
});
promise.resolve = resolve;
promise.reject = reject;
return promise;
}
function startsWith(string, target) { function startsWith(string, target) {
return (string.lastIndexOf(target, 0) === 0); return (string.lastIndexOf(target, 0) === 0);
} }
@ -210,6 +224,7 @@ var objectsOptions = {
}, },
}; };
// Try connecting to Xo-Server.
function tryConnect() { function tryConnect() {
/* jshint validthis: true */ /* jshint validthis: true */
@ -221,48 +236,55 @@ function tryConnect() {
}); });
} }
function onSuccessfulConnection() { function resetSession() {
/* jshint validthis: true */ /* jshint validthis: true */
// Reset back off. // No session has been opened and no credentials has been provided
this._backOff = fibonacci(1e3); // yet: nothing to do.
if (this._credentials && this._credentials.isPending()) {
return;
}
// FIXME: session.signIn() should work with both token and password. // Clear any existing user.
return this._api.call( this.user = null;
this._auth.token ?
'session.signInWithToken' : // Create a promise for the next credentials.
'session.signInWithPassword', this._credentials = makeStandaloneDeferred();
this._auth
).bind(this).then(function (user) { // The promise from the previous session needs to be rejected.
if (this._session && !this._session.isPending()) {
this._session.reject();
}
// Create a promise for the next session.
this._session = makeStandaloneDeferred();
}
function signIn() {
/* jshint validthis: true */
// Capture current session.
var session = this._session;
this._credentials.bind(this).then(function (credentials) {
return this._api.call(
credentials.token ?
'session.signInWithToken' :
'session.signInWithPassword',
credentials
);
}).then(function (user) {
this.user = user; this.user = user;
this.status = 'connected';
this._api.call('xo.getAllObjects').bind(this).then(function (objects) { this._api.call('xo.getAllObjects').bind(this).then(function (objects) {
this.objects.clear(); this.objects.clear();
this.objects.setMultiple(objects); this.objects.setMultiple(objects);
}); });
session.resolve();
}); });
} }
function onFailedConnection() {
/* jshint validthis: true */
this.status = 'disconnected';
}
function connect() {
/* jshint validthis: true */
if (this._connection) {
return this._connection;
}
this._connection = tryConnect.call(this).then(
onSuccessfulConnection, onFailedConnection
);
return this._connection;
}
// High level interface to Xo. // High level interface to Xo.
// //
// Handle auto-reconnect, sign in & objects cache. // Handle auto-reconnect, sign in & objects cache.
@ -270,19 +292,24 @@ function Xo(opts) {
var self = this; var self = this;
this._api = new Api(opts.url); this._api = new Api(opts.url);
this._auth = opts.auth;
this._backOff = fibonacci(1e3); this._backOff = fibonacci(1e3);
this.objects = createCollection(objectsOptions); this.objects = createCollection(objectsOptions);
this.status = 'disconnected'; this.status = 'disconnected';
this.user = null;
// Promise representing the connection status. self._api.on('connected', function () {
this._connection = null; self.status = 'connected';
// Reset back off.
self._backOff = fibonacci(1e3);
signIn.call(self);
});
self._api.on('disconnected', function () { self._api.on('disconnected', function () {
// Automatically reconnect. self.status = 'disconnected';
self._connection = null;
connect.call(self); resetSession.call(self);
tryConnect.call(self);
}); });
self._api.on('notification', function (notification) { self._api.on('notification', function (notification) {
@ -299,8 +326,8 @@ function Xo(opts) {
self.objects[method](notification.params.items); self.objects[method](notification.params.items);
}); });
// Bootstrap the connection. resetSession.call(this);
connect.call(this); tryConnect.call(this);
} }
Xo.prototype.call = function (method, params) { Xo.prototype.call = function (method, params) {
@ -310,13 +337,33 @@ Xo.prototype.call = function (method, params) {
throw new Error('session.*() methods are disabled from this interface'); throw new Error('session.*() methods are disabled from this interface');
} }
return connect.call(this).then(function () { return this._session.bind(this).then(function () {
var self = this; return this._api.call(method, params).bind(this).catch(ConnectionLost, function () {
return this._api.call(method, params).catch(ConnectionLost, function () {
// Retry automatically. // Retry automatically.
return self.call(method, params); return this.call(method, params);
}); });
}); });
}; };
Xo.prototype.signIn = function (credentials) {
// Ignore the returned promise as it can cause concurrency issues.
this.signOut();
this._credentials.resolve(credentials);
return this._session;
};
Xo.prototype.signOut = function () {
// Already signed in?
var promise;
if (!this._session.isPending()) {
promise = this._api.call('session.signOut');
}
resetSession.call(this);
return promise || Bluebird.resolve();
};
exports.Xo = Xo; exports.Xo = Xo;