UNPKG

baasic-sdk-javascript

Version:

JavaScript SDK provides core functionality for building web and mobile applications on [Baasic](http://www.baasic.com/).

148 lines (147 loc) 6.38 kB
"use strict"; /* globals module */ /** * @module loginClient * @description Login Client provides an easy way to consume Application Registration REST API end-points. In order to obtain needed routes `loginClient` uses `loginRoute`. */ Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var inversify_1 = require("inversify"); var common_1 = require("../../common"); var contracts_1 = require("../../core/contracts"); var httpApi_1 = require("../../httpApi"); var _1 = require("./"); var LoginClient = /** @class */ (function () { function LoginClient(loginRoute, tokenHandler, apiClient) { this.loginRoute = loginRoute; this.tokenHandler = tokenHandler; this.apiClient = apiClient; this.utility = new common_1.Utility(); } Object.defineProperty(LoginClient.prototype, "routeDefinition", { get: function () { return this.loginRoute; }, enumerable: true, configurable: true }); /** * Returns a promise that is resolved once the login action has been performed. This action logs user into the application and success response returns the token resource. * @method * @example loginClient.login({ username : '<username>', password : '<password>', options : ['session', 'sliding'] }) .then(function (data) { // perform success actions here }, function (data, status) { // perform error handling here }) .finally (function () {}); **/ LoginClient.prototype.login = function (data) { var settings = this.utility.extend({}, data); if (settings.options) { var options = settings.options; if (options instanceof Array) { settings.options = options.join(); } } var loginData = this.transformData({ grant_type: 'password', username: settings.username, password: settings.password }); var self = this; return this.apiClient.createPromise(function (resolve, reject) { self.apiClient.post(self.loginRoute.login(settings), loginData, { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' }) .then(function (data) { var token = { token: data.data.access_token, expires_in: data.data.expires_in, sliding_window: data.data.sliding_window, tokenUrl: data.data.access_url_token, type: data.data.token_type }; self.tokenHandler.store(token); resolve(data); }, function (data) { reject(data); }); }); }; /** * Returns a promise that is resolved once the loadUserData action has been performed. This action retrieves the account information of the currently logged in user. Retrieved account information will contain permission collection which identifies access policies assigned to the user and application sections. * @method * @example loginClient.loadUserData() .then(function (data) { // perform success actions here }, function (data) { // perform error handling here }) .finally (function () {}); */ LoginClient.prototype.loadUserData = function (data) { data = data || {}; return this.apiClient.get(this.loginRoute.login(data), { 'Accept': 'application/json; charset=UTF-8' }); }; /** * Returns a promise that is resolved once the logout action has been performed. This action invalidates user token logging the user out of the system. * @method * @param token Authentication token which uniquely identifies user that needs to be logged out from the system. * @param type Token type. * @returns A promise that is resolved once the logout action has been performed. * @example let token = baasicAuthorizationService.getAccessToken(); loginClient.logout(token.access_token, token.token_type) .then(function (data) { // perform success handling here }, function() { // perform error handling here }) .finally (function () {}); */ LoginClient.prototype.logout = function (token, type) { var data = { token: token, type: type }; var self = this; return this.apiClient.createPromise(function (resolve, reject) { self.apiClient.delete(self.loginRoute.login({}), null, data) .then(function (result) { self.tokenHandler.store(null); resolve(result); }, function (result) { reject(result); }); }); }; /** * Returns url encoded form data. */ LoginClient.prototype.transformData = function (data) { var items = []; for (var key in data) { items.push([encodeURIComponent(key), encodeURIComponent(data[key])].join('=')); } return items.join('&'); }; LoginClient = tslib_1.__decorate([ inversify_1.injectable(), tslib_1.__param(0, inversify_1.inject(_1.TYPES.LoginRoute)), tslib_1.__param(1, inversify_1.inject(contracts_1.TYPES.ITokenHandler)), tslib_1.__param(2, inversify_1.inject(httpApi_1.httpTYPES.ApiClient)), tslib_1.__metadata("design:paramtypes", [_1.LoginRoute, Object, httpApi_1.ApiClient]) ], LoginClient); return LoginClient; }()); exports.LoginClient = LoginClient; /** * @overview ***Notes:** - Refer to the [Baasic REST API](http://dev.baasic.com/api/reference/home) for detailed information about available Baasic REST API end-points. - All end-point objects are transformed by the associated route definition. */