diff --git a/package.json b/package.json index b344beca5..4a3aebb84 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@webitel/ui-sdk", - "version": "24.4.12", + "version": "24.4.13", "private": false, "scripts": { "dev": "vite", diff --git a/src/modules/Userinfo/api/BaseAPIService.js b/src/modules/Userinfo/api/BaseAPIService.js deleted file mode 100644 index e43f25598..000000000 --- a/src/modules/Userinfo/api/BaseAPIService.js +++ /dev/null @@ -1,7 +0,0 @@ -export default class BaseAPIService { - _instance = null - - setInstance(instance) { - this._instance = instance; - } -} diff --git a/src/modules/Userinfo/api/auth.js b/src/modules/Userinfo/api/auth.js deleted file mode 100644 index ba2445a78..000000000 --- a/src/modules/Userinfo/api/auth.js +++ /dev/null @@ -1,31 +0,0 @@ -import BaseAPIService from './BaseAPIService'; -import applyTransform, { - notify, -} from '../../../api/transformers'; - -class AuthAPI extends BaseAPIService { - async setToken(token) { - localStorage.setItem('access-token', token); - } - - removeToken() { - localStorage.removeItem('access-token'); - } - - async logout() { - const url = '/logout'; - - try { - await this._instance.post(url, {}); - this.removeToken(); - } catch (err) { - throw applyTransform(err, [ - notify, - ]); - } - } -} - -const auth = new AuthAPI(); - -export default auth; diff --git a/src/modules/Userinfo/api/userinfo.js b/src/modules/Userinfo/api/userinfo.js index a0530e992..84cfcf497 100644 --- a/src/modules/Userinfo/api/userinfo.js +++ b/src/modules/Userinfo/api/userinfo.js @@ -2,17 +2,12 @@ import applyTransform, { notify, snakeToCamel, } from '../../../api/transformers'; -import BaseAPIService from './BaseAPIService'; -class UserinfoAPI extends BaseAPIService { - // gets user by token from localstorage -// stores response username in vuex +const userinfo = (instance) => ({ async getSession() { - // const tokenCheck = localStorage.getItem('access-token'); - // if (typeof tokenCheck === 'string') { // if there is no token, localStorage returns object const url = '/userinfo'; try { - const response = await this._instance.get(url); + const response = await instance.get(url); return applyTransform(response.data, [ snakeToCamel(), ]); @@ -21,13 +16,12 @@ class UserinfoAPI extends BaseAPIService { notify, ]); } - // } - } + }, async getApplicationsAccess() { const url = 'role/metadata/access'; try { - const response = await this._instance.get(url); + const response = await instance.get(url); return applyTransform(response.data, [ snakeToCamel(), ]); @@ -36,9 +30,19 @@ class UserinfoAPI extends BaseAPIService { notify, ]); } - } -} + }, + + async logout() { + const url = '/logout'; -const userinfo = new UserinfoAPI(); + try { + return await instance.post(url, {}); + } catch (err) { + throw applyTransform(err, [ + notify, + ]); + } + } +}); export default userinfo; diff --git a/src/modules/Userinfo/components/__tests__/auth.spec.js b/src/modules/Userinfo/components/__tests__/auth.spec.js deleted file mode 100644 index 8a62fb399..000000000 --- a/src/modules/Userinfo/components/__tests__/auth.spec.js +++ /dev/null @@ -1,54 +0,0 @@ -import { shallowMount } from '@vue/test-utils'; -import { createStore } from 'vuex'; -import { createRouter, createWebHistory } from 'vue-router'; -import authAPI from '../../api/auth'; -import userinfoAPI from '../../api/userinfo'; -import Auth from '../the-auth.vue'; -import UserinfoStoreModule from '../../store/UserinfoStoreModule'; - -import '../../../../../tests/mocks/localStorageMock'; - -localStorage.setItem('access-token', 'jest'); - -authAPI.setToken = vi.fn(); -userinfoAPI.getSession = vi.fn(() => ({})); -userinfoAPI.getApplicationsAccess = vi.fn(() => ({})); - -describe('Auth', () => { - let store; - let wrapper; - const router = createRouter({ - history: createWebHistory(), - routes: [], - }); - router.replace = vi.fn(); - - const userinfo = new UserinfoStoreModule().getModule(); - beforeEach(() => { - store = createStore({ - modules: { userinfo }, - }); - - wrapper = shallowMount(Auth, { - global: { - plugins: [router, store], - }, - }); - }); - - it('renders a component', () => { - expect(wrapper.classes('auth-wrap')).toBe(true); - }); - - it('sets token, gets session and opens app after auth token message emit', async () => { - const accessToken = 'hello there'; - window.postMessage({ accessToken }, '*'); - await new Promise((resolve) => setTimeout(() => { - expect(authAPI.setToken).toHaveBeenCalledWith(accessToken); - expect(userinfoAPI.getSession).toHaveBeenCalled(); - expect(userinfoAPI.getApplicationsAccess).toHaveBeenCalled(); - expect(router.replace).toHaveBeenCalled(); - resolve(); - }, 100)); - }); -}); diff --git a/src/modules/Userinfo/components/the-auth.vue b/src/modules/Userinfo/components/the-auth.vue deleted file mode 100644 index 6ad411679..000000000 --- a/src/modules/Userinfo/components/the-auth.vue +++ /dev/null @@ -1,79 +0,0 @@ - - - - - diff --git a/src/modules/Userinfo/store/UserinfoStoreModule.js b/src/modules/Userinfo/store/UserinfoStoreModule.js index e6ef3663b..2cc909837 100644 --- a/src/modules/Userinfo/store/UserinfoStoreModule.js +++ b/src/modules/Userinfo/store/UserinfoStoreModule.js @@ -1,8 +1,10 @@ -import userinfo from '../api/userinfo'; +import userinfoGenerator from '../api/userinfo'; import ApplicationsAccess from '../classes/ApplicationsAccess'; import BaseStoreModule from '../../../store/BaseStoreModules/BaseStoreModule'; import Permissions from '../enums/Permissions.enum'; +let userinfo = null; + const defaultState = () => ({ isLoading: true, domainId: 0, @@ -87,7 +89,9 @@ export default class UserinfoStoreModule extends BaseStoreModule { return permissions; }, - OPEN_SESSION: async (context) => { + OPEN_SESSION: async (context, { instance }) => { + let userinfo = userinfoGenerator(instance); + await context.dispatch('BEFORE_OPEN_SESSION_HOOK'); // !!! it should be checked in router.js beforeEach hook // if (!localStorage.getItem('access-token')) { @@ -122,6 +126,12 @@ export default class UserinfoStoreModule extends BaseStoreModule { } }, + LOGOUT: async (context, { authUrl = import.meta.env.VITE_AUTH_URL }) => { + if (!authUrl) throw new Error('No authUrl for LOGOUT provided'); + await userinfo.logout(); + window.location.href = authUrl; + }, + SET_APPLICATIONS_ACCESS: (context, access) => context.commit('SET_APPLICATIONS_ACCESS', access), SET_LOADING: (context, isLoading) => {