UNPKG

@xompass/sdk-cloud-api

Version:

Xompass Client for cloud-api

151 lines (136 loc) 3.73 kB
declare const Object: any; import { SDKToken } from '../../models'; import { XompassClient } from '../../XompassClient'; export class XompassAuth { private static token: SDKToken; protected static prefix = '$XompassSDK$'; /** * @method setRememberMe * @param value Flag to remember credentials * @description * This method will set a flag in order to remember the current credentials */ public static setRememberMe(value: boolean): void { 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. */ public static setUser(user: any): void { 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 */ public static setToken(token: SDKToken): void { 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. */ public static getToken(): SDKToken { if (!this.token) { this.load(); } return this.token as SDKToken; } /** * @method getAccessTokenId * @description * This method will return the actual token string, not the object instance. */ public static getAccessTokenId(): string { 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. */ public static getCurrentUserId(): any { if (!this.token) { this.load(); } return this.token.userId; } /** * @method getCurrentUserData * @description * This method will return the current user instance. */ public static getCurrentUserData(): any { 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. */ public static save(): boolean { const today = new Date(); const 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. */ public static load(): void { const token = XompassClient.storage.get(`${ this.prefix }token`); if (!token) { this.token = new SDKToken(); return; } const expiresAt = new Date(new Date(token.created || 0).getTime() + (token.ttl || 0) * 1000); if (expiresAt < new Date()) { this.token = new SDKToken(); return; } this.token = token; } /** * @method clear * @description * This method will clear cookies or the local storage. */ public static clear(): void { XompassClient.storage.remove(`${ this.prefix }token`); this.token = new SDKToken(); } /** * @method persist * @description * This method saves values to storage */ public static persist(prop: string, value: any, expires?: Date): void { try { XompassClient.storage.set( `${ this.prefix }${ prop }`, value, expires, ); } catch (err) { console.error('Cannot access local/session storage:', err); } } }