import BaseEntityService from '../../common/services/BaseEntityService/BaseEntityService'; import Authenticator from '../entity/Authenticator'; import AuthenticatorServiceInterface from './AuthenticatorServiceInterface'; import AuthenticatorClientInterface from '../clients/AuthenticatorClientInterface'; import AuthenticatorAdapterInterface from '../adapter/AuthenticatorAdapterInterface'; import RequestPromise from '../../common/DTO/RequestPromise'; import HTTPRequest from '../../Request/entity/HTTPRequest'; import RoleScopeType from '../enums/RoleScopeType'; import MethodNotImplementedYetException from '../../common/exceptions/MethodNotImplementedYetException'; export default class AuthenticatorService extends BaseEntityService implements AuthenticatorServiceInterface { protected client: AuthenticatorClientInterface; constructor(client: AuthenticatorClientInterface, transformer: AuthenticatorAdapterInterface) { super(client, transformer); } login(username: string, password: string, scope: Array): Promise { let httpRequest = new HTTPRequest(); let auth = Authenticator.getNew(username, password, scope); httpRequest.data = { ...this.transformer.transformToPayload(auth), scope: auth.scope.join(','), }; return new Promise(async (resolve, reject) => { try { const { data } = await this.client.login(httpRequest).promise; this.saveIntoLocalStorage(data); resolve(data); } catch (e) { reject(e); } }); } refreshToken(refresh_token: string): RequestPromise { throw new MethodNotImplementedYetException('\'refreshToken\' at service level'); } private saveIntoLocalStorage(data) { localStorage.setItem('zlo_user_session', JSON.stringify(data)); } private clearLocalStorage() { localStorage.removeItem('zlo_user_session'); } logout() { this.clearLocalStorage(); } }