Browse files

Updated JS lib to 1.1.0.

  • Loading branch information...
1 parent e205f5a commit 242dbbd25b754fe79ea9d94ee59b2c56bcf5dc2b @vseventer vseventer committed Aug 29, 2013
Showing with 115 additions and 141 deletions.
  1. +114 −140 kinvey.js
  2. +1 −1 package.json
View
254 kinvey.js
@@ -104,7 +104,7 @@
* @type {string}
* @default
*/
- Kinvey.SDK_VERSION = '1.0.5';
+ Kinvey.SDK_VERSION = '1.1.0';
// Properties.
// -----------
@@ -155,9 +155,10 @@
/**
* Restores the active user (if any) from disk.
*
+ * @param {Object} options Options.
* @returns {Promise} The active user, or `null` if there is no active user.
*/
- var restoreActiveUser = function() {
+ var restoreActiveUser = function(options) {
// Retrieve the authtoken from storage. If there is an authtoken, restore the
// active user from disk.
var promise = Storage.get('activeUser');
@@ -172,14 +173,19 @@
log('Restoring the active user.');
}
- // Set the active user to a near-empty user with only the authtoken set.
+ // Set the active user to a near-empty user with only id and authtoken set.
var previous = Kinvey.setActiveUser({
_id: user[0],
_kmd: {
authtoken: user[1]
}
});
+ // If not `options.refresh`, return here.
+ if(false === options.refresh) {
+ return Kinvey.getActiveUser();
+ }
+
// Retrieve the user. The `Kinvey.User.me` method will also update the
// active user. If `INVALID_CREDENTIALS`, reset the active user.
return Kinvey.User.me().then(null, function(error) {
@@ -257,12 +263,13 @@
/**
* Initializes the library for use with Kinvey services.
*
- * @param {Options} options Options.
- * @param {string} options.appKey App Key.
- * @param {string} [options.appSecret] App Secret.
- * @param {string} [options.masterSecret] Master Secret. **Never use the
+ * @param {Options} options Options.
+ * @param {string} options.appKey App Key.
+ * @param {string} [options.appSecret] App Secret.
+ * @param {string} [options.masterSecret] Master Secret. **Never use the
* Master Secret in client-side code.**
- * @param {Object} [options.sync] Synchronization options.
+ * @param {boolean} [options.refresh=true] Refresh the active user (if any).
+ * @param {Object} [options.sync] Synchronization options.
* @throws {Kinvey.Error} `options` must contain: `appSecret` or
* `masterSecret`.
* @returns {Promise} The active user.
@@ -291,7 +298,9 @@
Kinvey.masterSecret = null != options.masterSecret ? options.masterSecret : null;
// Initialize the synchronization namespace and restore the active user.
- var promise = Kinvey.Sync.init(options.sync).then(restoreActiveUser);
+ var promise = Kinvey.Sync.init(options.sync).then(function() {
+ return restoreActiveUser(options);
+ });
return wrapCallbacks(promise, options);
};
@@ -638,6 +647,13 @@
// #### Client.
/**
+ * @memberOf Kinvey.Error
+ * @constant
+ * @default
+ */
+ Kinvey.Error.ALREADY_LOGGED_IN = 'AlreadyLoggedIn';
+
+ /**
* @memberof Kinvey.Error
* @constant
* @default
@@ -704,6 +720,19 @@
var ClientError = {};
/**
+ * Already logged in error.
+ *
+ * @constant
+ * @type {Object}
+ * @default
+ */
+ ClientError[Kinvey.Error.ALREADY_LOGGED_IN] = {
+ name: Kinvey.Error.ALREADY_LOGGED_IN,
+ description: 'You are already logged in with another user.',
+ debug: 'If you want to switch users, logout the active user first ' + 'using `Kinvey.User.logout`, then try again.'
+ };
+
+ /**
* Database error.
*
* @constant
@@ -751,8 +780,8 @@
*/
ClientError[Kinvey.Error.NO_ACTIVE_USER] = {
name: Kinvey.Error.NO_ACTIVE_USER,
- description: 'No active user.',
- debug: 'Try creating a user or logging in first.'
+ description: 'You need to be logged in to execute this request.',
+ debug: 'Try creating a user using `Kinvey.User.signup`, or login an ' + 'existing user using `Kinvey.User.login`.'
};
/**
@@ -1195,13 +1224,10 @@
// Authentication.
// ---------------
- // Access to the Kinvey service is authenticated through (implicit) user
- // credentials, Master Secret, or App Secret. A combination of these is often
- // accepted, but not always. Therefore, an extensive set of all possible
- // combinations is gathered here and presented as authentication policies.
-
- // The promise to handle concurrent requests when the active user is `null`.
- var implicitUserPromise = null;
+ // Access to the Kinvey service is authenticated through user credentials,
+ // Master Secret, or App Secret. A combination of these is often (but not
+ // always) accepted. Therefore, an extensive set of all possible combinations
+ // is gathered here and presented as authentication policies.
/**
* @private
@@ -1265,50 +1291,17 @@
},
/**
- * Authenticate through (1) user credentials, or (2) Master Secret. If both
- * fail, an implicit user is created and (1) is attempted.
+ * Authenticate through (1) user credentials, or (2) Master Secret.
*
* @returns {Promise}
*/
Default: function() {
- return Auth.UserDefault().then(null, function() {
- // Debug.
- if(KINVEY_DEBUG) {
- log('Creating an implicit user.');
- }
-
- // A race condition occurs when the active user is `null`, and multiple
- // requests are fired simultaneously. Normally, this would result in the
- // creation of multiple implicit users. The flow below ensures only one
- // implicit user can be created at the time, deferring any concurrent
- // requests until after the implicit user is created and ready for usage.
-
- // Debug.
- if(KINVEY_DEBUG && null !== implicitUserPromise) {
- log('An implicit user is already being created by another process, waiting.');
- }
-
- // If there is no implicit user being created right now, obtain the lock.
- if(null === implicitUserPromise) {
- // Create an implicit user.
- implicitUserPromise = Kinvey.User.create().then(function(user) {
- // Debug.
- if(KINVEY_DEBUG) {
- log('Created the implicit user.', user);
- }
-
- // Release the lock and return the response.
- implicitUserPromise = null;
- return user;
- }, function(error) {
- // Release the lock and forward the error.
- implicitUserPromise = null;
- return Kinvey.Defer.reject(error);
- });
- }
-
- // Authenticate using the implicit user.
- return implicitUserPromise.then(Auth.Session);
+ return Auth.Session().then(null, function(error) {
+ return Auth.Master().then(null, function() {
+ // Most likely, the developer did not create a user. Return a useful
+ // error.
+ return Kinvey.Defer.reject(error);
+ });
});
},
@@ -1379,15 +1372,6 @@
// Return the response.
return promise;
- },
-
- /**
- * Authenticate through (1) user credentials, or (2) Master Secret.
- *
- * @returns {Promise}
- */
- UserDefault: function() {
- return Auth.Session().then(null, Auth.Master);
}
};
@@ -1479,7 +1463,7 @@
}
// Return the device information string.
- var parts = ['js-nodejs/1.0.5'];
+ var parts = ['js-nodejs/1.1.0'];
if(0 !== libraries.length) { // Add external library information.
parts.push('(' + libraries.sort().join(', ') + ')');
}
@@ -3290,6 +3274,12 @@
log('Logging in an existing user.', arguments);
}
+ // Validate preconditions.
+ if(null !== Kinvey.getActiveUser()) {
+ var error = clientError(Kinvey.Error.ALREADY_LOGGED_IN);
+ return Kinvey.Defer.reject(error);
+ }
+
// Cast arguments.
if(isObject(usernameOrData)) {
options = 'undefined' !== typeof options ? options : password;
@@ -3308,42 +3298,35 @@
throw new Kinvey.Error('Argument must contain: username and password, or _socialIdentity.');
}
- // Logout the current active user first, then proceed with logging in
- // with the specified credentials.
- return Kinvey.User.logout({
- force: true,
- silent: true
- }).then(function() {
- // Prepare the response.
- var promise = Kinvey.Persistence.create({
- namespace: USERS,
- collection: options._provider ? null : 'login',
- data: usernameOrData,
- flags: options._provider ? {
- provider: options._provider
- } : {},
- auth: Auth.App,
- local: {
- res: true
- }
- }, options).then(function(user) {
- // Set and return the active user.
- Kinvey.setActiveUser(user);
- return user;
- });
-
- // Debug.
- if(KINVEY_DEBUG) {
- promise.then(function(response) {
- log('Logged in the user.', response);
- }, function(error) {
- log('Failed to login the user.', error);
- });
+ // Login with the specified credentials.
+ var promise = Kinvey.Persistence.create({
+ namespace: USERS,
+ collection: options._provider ? null : 'login',
+ data: usernameOrData,
+ flags: options._provider ? {
+ provider: options._provider
+ } : {},
+ auth: Auth.App,
+ local: {
+ res: true
}
-
- // Return the response.
- return wrapCallbacks(promise, options);
+ }, options).then(function(user) {
+ // Set and return the active user.
+ Kinvey.setActiveUser(user);
+ return user;
});
+
+ // Debug.
+ if(KINVEY_DEBUG) {
+ promise.then(function(response) {
+ log('Logged in the user.', response);
+ }, function(error) {
+ log('Failed to login the user.', error);
+ });
+ }
+
+ // Return the response.
+ return wrapCallbacks(promise, options);
},
/**
@@ -3618,8 +3601,8 @@
*
* @param {Object} [data] User data.
* @param {Options} [options] Options.
- * @param {boolean} [options.state=true] Save created user as the
- * active user.
+ * @param {boolean} [options.state=true] Save the created user as the active
+ * user.
* @returns {Promise} The new user.
*/
create: function(data, options) {
@@ -3631,45 +3614,36 @@
// Cast arguments.
options = options || {};
- // If `options.state`, logout the active user.
- var promise;
- if(false !== options.state) {
- promise = Kinvey.User.logout({
- force: true,
- silent: true
- });
- }
- else {
- promise = Kinvey.Defer.resolve(null);
+ // If `options.state`, validate preconditions.
+ if(false !== options.state && null !== Kinvey.getActiveUser()) {
+ var error = clientError(Kinvey.Error.ALREADY_LOGGED_IN);
+ return Kinvey.Defer.reject(error);
}
// Create the new user.
- return promise.then(function() {
- // Prepare the response.
- var promise = Kinvey.Persistence.create({
- namespace: USERS,
- data: data || {},
- auth: Auth.App
- }, options).then(function(user) {
- // If `options.state`, set the active user.
- if(false !== options.state) {
- Kinvey.setActiveUser(user);
- }
- return user;
- });
-
- // Debug.
- if(KINVEY_DEBUG) {
- promise.then(function(response) {
- log('Created the new user.', response);
- }, function(error) {
- log('Failed to create the new user.', error);
- });
+ var promise = Kinvey.Persistence.create({
+ namespace: USERS,
+ data: data || {},
+ auth: Auth.App
+ }, options).then(function(user) {
+ // If `options.state`, set the active user.
+ if(false !== options.state) {
+ Kinvey.setActiveUser(user);
}
-
- // Return the response.
- return wrapCallbacks(promise, options);
+ return user;
});
+
+ // Debug.
+ if(KINVEY_DEBUG) {
+ promise.then(function(response) {
+ log('Created the new user.', response);
+ }, function(error) {
+ log('Failed to create the new user.', error);
+ });
+ }
+
+ // Return the response.
+ return wrapCallbacks(promise, options);
},
/**
@@ -3721,7 +3695,7 @@
namespace: USERS,
id: data._id,
data: data,
- auth: Auth.UserDefault,
+ auth: Auth.Default,
local: {
res: true
}
@@ -3902,7 +3876,7 @@
flags: options.hard ? {
hard: true
} : {},
- auth: Auth.UserDefault,
+ auth: Auth.Default,
local: {
res: true
}
View
2 package.json
@@ -1,6 +1,6 @@
{
"name" : "kinvey",
- "version" : "1.0.5",
+ "version" : "1.1.0",
"description" : "Kinvey JavaScript Library for Node.js.",
"homepage" : "http://www.kinvey.com",
"bugs" : { "email": "support@kinvey.com" },

0 comments on commit 242dbbd

Please sign in to comment.