/* eslint-disable @typescript-eslint/no-var-requires */ import keytar from 'keytar'; import SevenStorageName from '../enums/SevenStorageName'; import SevenNotification from '../enums/SevenNotification'; import SevenAuthorizedUser from './SevenAuthorizedUser'; import { showNotification } from '../helpers/notificationHandler'; import { deleteCurrentTenant } from '../localStorage/tenantLSHandler'; /** * Saves new authorized user into list of autorized users. * If there is already same user with same identity, it will do update. * @param {string} identity * @param {SevenAuthorizedUser} userInfo */ const saveAuthorizedUser = (identity: string, userInfo: SevenAuthorizedUser): void => { keytar.setPassword(SevenStorageName.AUTHORIZED_USERS, identity, JSON.stringify(userInfo)).then(() => { showNotification(SevenNotification.SUCCESS, `Session successfully saved for ${userInfo.firstName} ${userInfo.lastName}`); }).catch((err) => { showNotification(SevenNotification.ERROR, err.message); }); }; /** * Gets all saved authorized users * @returns {Promise} */ const getAuthorizedUsers = (): Promise => { const sessions = keytar.findCredentials(SevenStorageName.AUTHORIZED_USERS); const authorizedUsers: SevenAuthorizedUser[] = []; return sessions.then((result) => { result.forEach((item) => { const userInfo = JSON.parse(item.password); authorizedUsers.push(userInfo); }); return authorizedUsers; }).catch((err) => { showNotification(SevenNotification.ERROR, err.message); return []; }); }; /** * Gets current user * @returns {Promise} */ const getCurrentUser = (): Promise => { const user = keytar.findCredentials(SevenStorageName.CURRENT_USER); return user.then((result) => { if (result?.length) { return JSON.parse(result[0].password); } return null; }).catch((err) => { showNotification(SevenNotification.ERROR, err.message); return null; }); }; /** * Removes current user from keytar */ const deleteCurrentUser = (): Promise => { return getCurrentUser().then((user: SevenAuthorizedUser | null) => { if (user) { deleteCurrentTenant(); keytar.deletePassword(SevenStorageName.CURRENT_USER, user?.identity) .catch((error) => { showNotification(SevenNotification.ERROR, error.message); }); } }).catch(); }; /** * Saves current user * @param {SevenAuthorizedUser} user */ const saveCurrentUser = (user: SevenAuthorizedUser | null): void => { deleteCurrentUser().then(() => { keytar.setPassword(SevenStorageName.CURRENT_USER, user?.identity || '', JSON.stringify(user || {})).then(() => { showNotification(SevenNotification.SUCCESS, `Selected user is ${user?.firstName} ${user?.lastName}`); }).catch((error) => { showNotification(SevenNotification.ERROR, error.message); }); }); }; /** * Removes user from list of authorized users * @param {SevenAuthorizedUser} user */ const removeUserFromAuthorizedUsers = (user: SevenAuthorizedUser | null): void => { keytar.deletePassword(SevenStorageName.AUTHORIZED_USERS, user?.identity || '').then(() => { getCurrentUser().then((currentUser: SevenAuthorizedUser | null) => { if (currentUser?.identity === user?.identity) { deleteCurrentUser(); } }).catch(); showNotification(SevenNotification.SUCCESS, `Successfully removed user ${user?.username}`); }).catch((err) => { showNotification(SevenNotification.ERROR, err.message); }); }; export { getAuthorizedUsers, saveAuthorizedUser, removeUserFromAuthorizedUsers, saveCurrentUser, getCurrentUser };