diff --git a/cvat-core/src/api-implementation.js b/cvat-core/src/api-implementation.js index afb0ad02..708b7a0b 100644 --- a/cvat-core/src/api-implementation.js +++ b/cvat-core/src/api-implementation.js @@ -58,6 +58,11 @@ await serverProxy.server.logout(); }; + cvat.server.authorized.implementation = async () => { + const result = await serverProxy.server.authorized(); + return result; + }; + cvat.users.get.implementation = async (filter) => { checkFilter(filter, { self: isBoolean, diff --git a/cvat-core/src/api.js b/cvat-core/src/api.js index 6bf52a72..0c4fb8a4 100644 --- a/cvat-core/src/api.js +++ b/cvat-core/src/api.js @@ -144,6 +144,20 @@ function build() { .apiWrapper(cvat.server.logout); return result; }, + /** + * Method allows to know whether you are authorized on the server + * @method authorized + * @async + * @memberof module:API.cvat.server + * @returns {boolean} + * @throws {module:API.cvat.exceptions.PluginError} + * @throws {module:API.cvat.exceptions.ServerError} + */ + async authorized() { + const result = await PluginRegistry + .apiWrapper(cvat.server.authorized); + return result; + }, }, /** * Namespace is used for getting tasks diff --git a/cvat-core/src/server-proxy.js b/cvat-core/src/server-proxy.js index dfb8c144..3836168f 100644 --- a/cvat-core/src/server-proxy.js +++ b/cvat-core/src/server-proxy.js @@ -222,6 +222,20 @@ } } + async function authorized() { + try { + await module.exports.users.getSelf(); + } catch (serverError) { + if (serverError.code === 403) { + return false; + } + + throw serverError; + } + + return true; + } + async function getTasks(filter = '') { const { backendAPI } = config; @@ -441,7 +455,7 @@ } catch (errorData) { const code = errorData.response ? errorData.response.status : errorData.code; throw new ServerError( - 'Could not get users from the server', + 'Could not get user data from the server', code, ); } @@ -626,6 +640,7 @@ exception, login, logout, + authorized, }), writable: false, },