From 4cf61306807e0db63db4dab61bbb0db786eab499 Mon Sep 17 00:00:00 2001 From: Ilya Batuev Date: Mon, 4 Nov 2024 19:27:16 +0000 Subject: [PATCH] new api support --- src/api/auth/UserSessionApi.ts | 122 ---------------------------- src/views/apps/ApplicationFrame.vue | 18 ++-- 2 files changed, 9 insertions(+), 131 deletions(-) delete mode 100644 src/api/auth/UserSessionApi.ts diff --git a/src/api/auth/UserSessionApi.ts b/src/api/auth/UserSessionApi.ts deleted file mode 100644 index 81e8acb..0000000 --- a/src/api/auth/UserSessionApi.ts +++ /dev/null @@ -1,122 +0,0 @@ -import { DefaultResponse } from '../BaseApi'; -import { SessionScope, UserScope, Session } from './../models/index'; -import { AuthBaseApi } from './AuthBaseApi'; - -interface CreateBody { - scopes?: string[]; - expires?: string; -} - -export enum MySessionInfo { - Groups = 'groups', - IndirectGroups = 'indirect_groups', - SessionScopes = 'session_scopes', - UserScopes = 'user_scopes', - Scopes = 'scopes', - AuthMethods = 'auth_methods', -} - -export enum AuthMethod { - Email = 'email', - Yandex = 'yandex_auth', - Github = 'github_auth', - Google = 'google_auth', - LkMsu = 'lkmsu_auth', - MyMsu = 'my_msu_auth', - Physics = 'physics_auth', - VK = 'vk_auth', -} - -interface SessionResponse { - id: number; - user_id: number; - session_name: string; - last_activity: string; -} - -interface SessionCreateResponse { - id: number; - user_id: number; - session_name: string; - last_activity: string; - token: string; - expires: string; -} - -export enum SessionInfo { - SessionScopes = 'session_scopes', - Token = 'token', - Expires = 'expires', -} - -class UserSessionApi extends AuthBaseApi { - constructor() { - super(''); - } - - public async logout() { - return this.post('/logout'); - } - - public async getMe(info?: Info[]) { - return this.get< - Session & { - [MySessionInfo.Groups]: MySessionInfo.Groups extends Info ? number[] : never; - [MySessionInfo.IndirectGroups]: MySessionInfo.IndirectGroups extends Info - ? number[] - : never; - [MySessionInfo.SessionScopes]: MySessionInfo.SessionScopes extends Info - ? SessionScope[] - : never; - [MySessionInfo.UserScopes]: MySessionInfo.UserScopes extends Info ? UserScope[] : never; - [MySessionInfo.AuthMethods]: MySessionInfo.AuthMethods extends Info ? AuthMethod[] : never; - }, - { info?: Info[] } - >('/me', { info }); - } - - public async getById(id: number, info?: Info[]) { - return this.get< - Session & { - [MySessionInfo.Groups]: MySessionInfo.Groups extends Info ? number[] : never; - [MySessionInfo.IndirectGroups]: MySessionInfo.IndirectGroups extends Info - ? number[] - : never; - [MySessionInfo.SessionScopes]: MySessionInfo.SessionScopes extends Info - ? SessionScope[] - : never; - [MySessionInfo.UserScopes]: MySessionInfo.UserScopes extends Info ? UserScope[] : never; - [MySessionInfo.AuthMethods]: MySessionInfo.AuthMethods extends Info ? AuthMethod[] : never; - }, - { info?: Info[] } - >(`/user/${id}`, { info }); - } - - public async getSessions(info?: Info[]) { - return this.get< - Array< - SessionResponse & { - [SessionInfo.Expires]: SessionInfo.Expires extends Info ? string : never; - [SessionInfo.SessionScopes]: SessionInfo.SessionScopes extends Info ? string[] : never; - [SessionInfo.Token]: SessionInfo.Token extends Info ? string : never; - } - >, - { info?: Info[] } - >('/session', { info }); - } - - public async createSession(body: CreateBody) { - return this.post('/session', body); - } - - public async deleteSessions(delete_current?: boolean) { - return this.delete('/session', { delete_current }); - } - - public async deleteSession(token: string) { - return this.delete(`/session/${token}`); - } -} - -export const userSessionApi = new UserSessionApi(); - diff --git a/src/views/apps/ApplicationFrame.vue b/src/views/apps/ApplicationFrame.vue index 7acdb28..222ff08 100644 --- a/src/views/apps/ApplicationFrame.vue +++ b/src/views/apps/ApplicationFrame.vue @@ -117,16 +117,16 @@ const getToken = async () => { } // 3. Если пользователь разрешает – запрашиваем токен на Auth api и возвращаем его - const session = ( - await userSessionApi.createSession(scopes.value.length == 0 ? {} : { scopes: scopes.value }) - ).data; - if (!session) { + const { data } = await apiClient.POST('/auth/session', { + body: { scopes: scopes.value.length == 0 ? [] : scopes.value }, + }); + if (!data) { appState.value = AppState.Error; return; } - authItem.token = session.token; - authItem.expires = session.expires; - profileStore.id = session.user_id; + authItem.token = data.token; + authItem.expires = data.expires; + profileStore.id = data.user_id; if (authItemIndex != -1) { appsData[authItemIndex] = authItem; } else { @@ -134,7 +134,7 @@ const getToken = async () => { } LocalStorage.set(LocalStorageItem.SuperappAuth, appsData); - return session.token; + return data.token; }; const openApp = async (data: ServiceData) => { @@ -160,7 +160,7 @@ const openApp = async (data: ServiceData) => { } // Пользователь не авторизован => Кнопка разблокирована и не требует авторизации => Показываем приложение - if (data.view == ButtonView.Active && !profileStore.id) { + if (data.view == 'active' && !profileStore.id) { appState.value = AppState.Show; return; }