@xompass/sdk-cloud-api
Version:
Xompass Client for cloud-api
139 lines • 4.33 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.XompassAuth = void 0;
var models_1 = require("../../models");
var XompassClient_1 = require("../../XompassClient");
var XompassAuth = /** @class */ (function () {
function XompassAuth() {
}
/**
* @method setRememberMe
* @param value Flag to remember credentials
* @description
* This method will set a flag in order to remember the current credentials
*/
XompassAuth.setRememberMe = function (value) {
this.token.rememberMe = value;
};
/**
* @method setUser
* @param user Any type of user model
* @description
* This method will update the user information and persist it if the
* rememberMe flag is set.
*/
XompassAuth.setUser = function (user) {
this.token.user = user;
this.save();
};
/**
* @method setToken
* @param token SDKToken or casted AccessToken instance
* @description
* This method will set a flag in order to remember the current credentials
*/
XompassAuth.setToken = function (token) {
this.token = Object.assign({}, this.token, token);
this.save();
};
/**
* @method getToken
* @description
* This method will set a flag in order to remember the current credentials.
*/
XompassAuth.getToken = function () {
if (!this.token) {
this.load();
}
return this.token;
};
/**
* @method getAccessTokenId
* @description
* This method will return the actual token string, not the object instance.
*/
XompassAuth.getAccessTokenId = function () {
if (!this.token) {
this.load();
}
return this.token.id;
};
/**
* @method getCurrentUserId
* @description
* This method will return the current user id, it can be number or string.
*/
XompassAuth.getCurrentUserId = function () {
if (!this.token) {
this.load();
}
return this.token.userId;
};
/**
* @method getCurrentUserData
* @description
* This method will return the current user instance.
*/
XompassAuth.getCurrentUserData = function () {
if (!this.token) {
this.load();
}
return (typeof this.token.user === 'string') ? JSON.parse(this.token.user) : this.token.user;
};
/**
* @method save
* @description
* This method will save in either local storage or cookies the current credentials.
* But only if rememberMe is enabled.
*/
XompassAuth.save = function () {
var today = new Date();
var expires = new Date(today.getTime() + (this.token.ttl * 1000));
this.persist("token", this.token, expires);
return true;
};
/**
* @method load
* @description
* This method will load either from local storage or cookies the provided property.
*/
XompassAuth.load = function () {
var token = XompassClient_1.XompassClient.storage.get("".concat(this.prefix, "token"));
if (!token) {
this.token = new models_1.SDKToken();
return;
}
var expiresAt = new Date(new Date(token.created || 0).getTime() + (token.ttl || 0) * 1000);
if (expiresAt < new Date()) {
this.token = new models_1.SDKToken();
return;
}
this.token = token;
};
/**
* @method clear
* @description
* This method will clear cookies or the local storage.
*/
XompassAuth.clear = function () {
XompassClient_1.XompassClient.storage.remove("".concat(this.prefix, "token"));
this.token = new models_1.SDKToken();
};
/**
* @method persist
* @description
* This method saves values to storage
*/
XompassAuth.persist = function (prop, value, expires) {
try {
XompassClient_1.XompassClient.storage.set("".concat(this.prefix).concat(prop), value, expires);
}
catch (err) {
console.error('Cannot access local/session storage:', err);
}
};
XompassAuth.prefix = '$XompassSDK$';
return XompassAuth;
}());
exports.XompassAuth = XompassAuth;
//# sourceMappingURL=XompassAuth.js.map