Fix _authenticate method to use promises only

This commit is contained in:
blagoev 2017-09-12 23:04:20 +03:00
parent eba51066d9
commit ba9321834b
1 changed files with 10 additions and 7 deletions

View File

@ -132,18 +132,19 @@ function _authenticate(userConstructor, server, json, callback) {
headers: postHeaders, headers: postHeaders,
open_timeout: 5000 open_timeout: 5000
}; };
const promise = performFetch(url, options) const promise = performFetch(url, options)
.then((response) => { .then((response) => {
if (response.status !== 200) { if (response.status !== 200) {
return response.json().then((body) => callback(new AuthError(body))); return response.json().then((body) => Promise.reject(new AuthError(body)));
} else { } else {
return response.json().then(function (body) { return response.json().then(function (body) {
// TODO: validate JSON // TODO: validate JSON
const token = body.refresh_token.token; const token = body.refresh_token.token;
const identity = body.refresh_token.token_data.identity; const identity = body.refresh_token.token_data.identity;
const isAdmin = body.refresh_token.token_data.is_admin; const isAdmin = body.refresh_token.token_data.is_admin;
callback(undefined, userConstructor.createUser(server, identity, token, false, isAdmin)); return userConstructor.createUser(server, identity, token, false, isAdmin);
}) });
} }
}); });
@ -193,8 +194,8 @@ const staticMethods = {
const message = "register(..., callback) is now deprecated in favor of register(): Promise<User>. This function argument will be removed in future versions."; const message = "register(..., callback) is now deprecated in favor of register(): Promise<User>. This function argument will be removed in future versions.";
(console.warn || console.log).call(console, message); (console.warn || console.log).call(console, message);
} }
_authenticate(this, server, json, callback);
return _authenticate(this, server, json, callback);
}, },
login(server, username, password, callback) { login(server, username, password, callback) {
@ -209,7 +210,8 @@ const staticMethods = {
const message = "login(..., callback) is now deprecated in favor of login(): Promise<User>. This function argument will be removed in future versions."; const message = "login(..., callback) is now deprecated in favor of login(): Promise<User>. This function argument will be removed in future versions.";
(console.warn || console.log).call(console, message); (console.warn || console.log).call(console, message);
} }
_authenticate(this, server, json, callback);
return _authenticate(this, server, json, callback);
}, },
registerWithProvider(server, options, callback) { registerWithProvider(server, options, callback) {
@ -240,7 +242,8 @@ const staticMethods = {
const message = "registerWithProvider(..., callback) is now deprecated in favor of registerWithProvider(): Promise<User>. This function argument will be removed in future versions."; const message = "registerWithProvider(..., callback) is now deprecated in favor of registerWithProvider(): Promise<User>. This function argument will be removed in future versions.";
(console.warn || console.log).call(console, message); (console.warn || console.log).call(console, message);
} }
_authenticate(this, server, json, callback);
return _authenticate(this, server, json, callback);
}, },
_refreshAccessToken: refreshAccessToken _refreshAccessToken: refreshAccessToken