baasic-sdk-javascript
Version:
JavaScript SDK provides core functionality for building web and mobile applications on [Baasic](http://www.baasic.com/).
134 lines (133 loc) • 6.39 kB
JavaScript
;
/* globals module */
/**
* @module loginSocialClient
* @description Login Social Client provides an easy way to consume Application Registration REST API end-points. In order to obtain needed routes `loginSocialClient` uses `loginSocialRoute`.
*/
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var inversify_1 = require("inversify");
var httpApi_1 = require("../../httpApi");
var contracts_1 = require("../../core/contracts");
var _1 = require("./");
var LoginSocialClient = /** @class */ (function () {
function LoginSocialClient(loginSocialRoute, tokenHandler, apiClient) {
this.loginSocialRoute = loginSocialRoute;
this.tokenHandler = tokenHandler;
this.apiClient = apiClient;
}
Object.defineProperty(LoginSocialClient.prototype, "routeDefinition", {
get: function () {
return this.loginSocialRoute;
},
enumerable: true,
configurable: true
});
/**
* Returns a promise that is resolved once the get action has been performed. Success response returns a resolved social login provider Url.
* @method
* @param provider Provider name or id for which the login URL should be generated.
* @param returnUrl Redirect Uri for the provider which will be used when the user is redirected back to the application.
* @returns A promise that is resolved once the get action has been performed.
* @example loginSocialClient.get('<provider>', '<returnUrl>')
.then(function (collection) {
// perform success action here
},
function (response, status, headers, config) {
// perform error handling here
});
**/
LoginSocialClient.prototype.get = function (provider, returnUrl) {
return this.apiClient.get(this.loginSocialRoute.get(provider, returnUrl));
};
/**
* Returns a promise that is resolved once the post action has been performed. This action logs user into the application and success response returns the token resource.
* @method
* @param provider Provider name or Id which uniquely identifies social login for which access token should be issued.
* @param data Object used to identify social login information.
* @param options Comma separated list of additional options defining token behavior. Supported values are: "session" and "sliding".
* @example let postData = {
email : '<email>',
code:'<code>',
activationUrl : '<activationUrl>',
oAuthToken : '<oAuthToken>',
oAuthVerifier : '<oAuthVerifier>',
password : '<password>',
returnUrl : '<returnUrl>'
};
loginSocialClient.post('<provider>', postData)
.then(function (collection) {
// perform success action here
},
function (response, status, headers, config) {
// perform error handling here
});
**/
LoginSocialClient.prototype.post = function (provider, data, options) {
var params = { provider: provider };
if (options) {
params.options = options;
}
var self = this;
return this.apiClient.createPromise(function (resolve, reject) {
self.apiClient.post(self.loginSocialRoute.post(provider, options), self.loginSocialRoute.createParams(data), { 'Content-Type': 'application/json; charset=UTF-8' })
.then(function (data) {
if (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);
});
});
};
LoginSocialClient.prototype.parseResponse = function (provider, returnUrl) {
var params = this.parseUrlParams();
var result = {};
switch (provider) {
case 'twitter':
/** jshint camelcase: false*/
result.oAuthToken = params.oauth_token;
result.oAuthVerifier = params.oauth_verifier;
break;
default:
result.code = params.code;
result.returnUrl = returnUrl;
break;
}
return result;
};
// Getting query string values in javascript: http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript
LoginSocialClient.prototype.parseUrlParams = function () {
var urlParams;
var match, pl = /\+/g, search = /([^&=]+)=?([^&]*)/g, decode = function (s) { return decodeURIComponent(s.replace(pl, ' ')); }, query = window.location.search.substring(1);
urlParams = {};
/* jshint -W084*/
while (match = search.exec(query)) {
urlParams[decode(match[1])] = decode(match[2]);
}
return urlParams;
};
LoginSocialClient = tslib_1.__decorate([
inversify_1.injectable(),
tslib_1.__param(0, inversify_1.inject(_1.TYPES.LoginSocialRoute)),
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.LoginSocialRoute, Object, httpApi_1.ApiClient])
], LoginSocialClient);
return LoginSocialClient;
}());
exports.LoginSocialClient = LoginSocialClient;
/**
* @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.
*/