@webitel/ui-sdk 24.4.12 → 24.4.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webitel/ui-sdk",
3
- "version": "24.4.12",
3
+ "version": "24.4.13",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "dev": "vite",
@@ -2,17 +2,12 @@ import applyTransform, {
2
2
  notify,
3
3
  snakeToCamel,
4
4
  } from '../../../api/transformers';
5
- import BaseAPIService from './BaseAPIService';
6
5
 
7
- class UserinfoAPI extends BaseAPIService {
8
- // gets user by token from localstorage
9
- // stores response username in vuex
6
+ const userinfo = (instance) => ({
10
7
  async getSession() {
11
- // const tokenCheck = localStorage.getItem('access-token');
12
- // if (typeof tokenCheck === 'string') { // if there is no token, localStorage returns object
13
8
  const url = '/userinfo';
14
9
  try {
15
- const response = await this._instance.get(url);
10
+ const response = await instance.get(url);
16
11
  return applyTransform(response.data, [
17
12
  snakeToCamel(),
18
13
  ]);
@@ -21,13 +16,12 @@ class UserinfoAPI extends BaseAPIService {
21
16
  notify,
22
17
  ]);
23
18
  }
24
- // }
25
- }
19
+ },
26
20
 
27
21
  async getApplicationsAccess() {
28
22
  const url = 'role/metadata/access';
29
23
  try {
30
- const response = await this._instance.get(url);
24
+ const response = await instance.get(url);
31
25
  return applyTransform(response.data, [
32
26
  snakeToCamel(),
33
27
  ]);
@@ -36,9 +30,19 @@ class UserinfoAPI extends BaseAPIService {
36
30
  notify,
37
31
  ]);
38
32
  }
39
- }
40
- }
33
+ },
34
+
35
+ async logout() {
36
+ const url = '/logout';
41
37
 
42
- const userinfo = new UserinfoAPI();
38
+ try {
39
+ return await instance.post(url, {});
40
+ } catch (err) {
41
+ throw applyTransform(err, [
42
+ notify,
43
+ ]);
44
+ }
45
+ }
46
+ });
43
47
 
44
48
  export default userinfo;
@@ -1,8 +1,10 @@
1
- import userinfo from '../api/userinfo';
1
+ import userinfoGenerator from '../api/userinfo';
2
2
  import ApplicationsAccess from '../classes/ApplicationsAccess';
3
3
  import BaseStoreModule from '../../../store/BaseStoreModules/BaseStoreModule';
4
4
  import Permissions from '../enums/Permissions.enum';
5
5
 
6
+ let userinfo = null;
7
+
6
8
  const defaultState = () => ({
7
9
  isLoading: true,
8
10
  domainId: 0,
@@ -87,7 +89,9 @@ export default class UserinfoStoreModule extends BaseStoreModule {
87
89
  return permissions;
88
90
  },
89
91
 
90
- OPEN_SESSION: async (context) => {
92
+ OPEN_SESSION: async (context, { instance }) => {
93
+ let userinfo = userinfoGenerator(instance);
94
+
91
95
  await context.dispatch('BEFORE_OPEN_SESSION_HOOK');
92
96
  // !!! it should be checked in router.js beforeEach hook
93
97
  // if (!localStorage.getItem('access-token')) {
@@ -122,6 +126,12 @@ export default class UserinfoStoreModule extends BaseStoreModule {
122
126
  }
123
127
  },
124
128
 
129
+ LOGOUT: async (context, { authUrl = import.meta.env.VITE_AUTH_URL }) => {
130
+ if (!authUrl) throw new Error('No authUrl for LOGOUT provided');
131
+ await userinfo.logout();
132
+ window.location.href = authUrl;
133
+ },
134
+
125
135
  SET_APPLICATIONS_ACCESS: (context, access) => context.commit('SET_APPLICATIONS_ACCESS', access),
126
136
 
127
137
  SET_LOADING: (context, isLoading) => {
@@ -1,7 +0,0 @@
1
- export default class BaseAPIService {
2
- _instance = null
3
-
4
- setInstance(instance) {
5
- this._instance = instance;
6
- }
7
- }
@@ -1,31 +0,0 @@
1
- import BaseAPIService from './BaseAPIService';
2
- import applyTransform, {
3
- notify,
4
- } from '../../../api/transformers';
5
-
6
- class AuthAPI extends BaseAPIService {
7
- async setToken(token) {
8
- localStorage.setItem('access-token', token);
9
- }
10
-
11
- removeToken() {
12
- localStorage.removeItem('access-token');
13
- }
14
-
15
- async logout() {
16
- const url = '/logout';
17
-
18
- try {
19
- await this._instance.post(url, {});
20
- this.removeToken();
21
- } catch (err) {
22
- throw applyTransform(err, [
23
- notify,
24
- ]);
25
- }
26
- }
27
- }
28
-
29
- const auth = new AuthAPI();
30
-
31
- export default auth;
@@ -1,54 +0,0 @@
1
- import { shallowMount } from '@vue/test-utils';
2
- import { createStore } from 'vuex';
3
- import { createRouter, createWebHistory } from 'vue-router';
4
- import authAPI from '../../api/auth';
5
- import userinfoAPI from '../../api/userinfo';
6
- import Auth from '../the-auth.vue';
7
- import UserinfoStoreModule from '../../store/UserinfoStoreModule';
8
-
9
- import '../../../../../tests/mocks/localStorageMock';
10
-
11
- localStorage.setItem('access-token', 'jest');
12
-
13
- authAPI.setToken = vi.fn();
14
- userinfoAPI.getSession = vi.fn(() => ({}));
15
- userinfoAPI.getApplicationsAccess = vi.fn(() => ({}));
16
-
17
- describe('Auth', () => {
18
- let store;
19
- let wrapper;
20
- const router = createRouter({
21
- history: createWebHistory(),
22
- routes: [],
23
- });
24
- router.replace = vi.fn();
25
-
26
- const userinfo = new UserinfoStoreModule().getModule();
27
- beforeEach(() => {
28
- store = createStore({
29
- modules: { userinfo },
30
- });
31
-
32
- wrapper = shallowMount(Auth, {
33
- global: {
34
- plugins: [router, store],
35
- },
36
- });
37
- });
38
-
39
- it('renders a component', () => {
40
- expect(wrapper.classes('auth-wrap')).toBe(true);
41
- });
42
-
43
- it('sets token, gets session and opens app after auth token message emit', async () => {
44
- const accessToken = 'hello there';
45
- window.postMessage({ accessToken }, '*');
46
- await new Promise((resolve) => setTimeout(() => {
47
- expect(authAPI.setToken).toHaveBeenCalledWith(accessToken);
48
- expect(userinfoAPI.getSession).toHaveBeenCalled();
49
- expect(userinfoAPI.getApplicationsAccess).toHaveBeenCalled();
50
- expect(router.replace).toHaveBeenCalled();
51
- resolve();
52
- }, 100));
53
- });
54
- });
@@ -1,79 +0,0 @@
1
- <template>
2
- <div class="auth-wrap">
3
- <div
4
- v-show="!isLoaded"
5
- class="auth-loader"
6
- >
7
- <wt-loader />
8
- </div>
9
- <iframe
10
- class="auth"
11
- :src="authURL"
12
- @load="isLoaded = true"
13
- >
14
- <p>Your browser does not support iframes.</p>
15
- </iframe>
16
- </div>
17
- </template>
18
-
19
- <script>
20
- import AuthAPI from '../api/auth';
21
-
22
- const authURL = process ? process.env.VUE_APP_AUTH_MODULE_URL : import.meta.env.VITE_AUTH_MODE_URL;
23
-
24
- export default {
25
- name: 'Auth',
26
- props: {
27
- namespace: {
28
- type: String,
29
- default: 'userinfo',
30
- },
31
- },
32
- data: () => ({
33
- authURL,
34
- isLoaded: false,
35
- }),
36
-
37
- mounted() {
38
- window.addEventListener('message', this.authMessageHandler, false);
39
- },
40
- unmounted() {
41
- window.removeEventListener('message', this.authMessageHandler, false);
42
- },
43
-
44
- methods: {
45
- openSession(payload) {
46
- return this.$store.dispatch(`${this.namespace}/OPEN_SESSION`, payload);
47
- },
48
- async authMessageHandler(event) {
49
- if (event.data.accessToken) {
50
- // eslint-disable-next-line import/no-named-as-default-member
51
- await AuthAPI.setToken(event.data.accessToken);
52
- await this.openSession();
53
- await this.$router.replace('/');
54
- }
55
- },
56
- },
57
- };
58
- </script>
59
-
60
- <style lang="scss" scoped>
61
- .auth-wrap {
62
- position: relative;
63
- height: 100vh;
64
- width: 100vw;
65
- overflow: hidden;
66
-
67
- .wt-loader {
68
- position: absolute;
69
- top: 50%;
70
- right: 50%;
71
- transform: translate(-50%, -50%);
72
- }
73
-
74
- .auth {
75
- height: 100%;
76
- width: 100%;
77
- }
78
- }
79
- </style>