Skip to content

Commit

Permalink
removed api and rebased apiClient init
Browse files Browse the repository at this point in the history
  • Loading branch information
BatuevIO committed Sep 8, 2024
1 parent a7271df commit b965565
Show file tree
Hide file tree
Showing 32 changed files with 64 additions and 116 deletions.
2 changes: 1 addition & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import IrdomToastList from './components/IrdomToastList.vue';
import { useToolbar } from './store/toolbar';
import IrdomToolbar from './components/IrdomToolbar.vue';
import CalendarDropdown from './views/timetable/CalendarDropdown.vue';
import apiClient from './api/BaseApi';
import apiClient from '@/api/';
const profileStore = useProfileStore();
const toolbar = useToolbar();
Expand Down
4 changes: 0 additions & 4 deletions src/api/BaseApi.ts

This file was deleted.

64 changes: 0 additions & 64 deletions src/api/auth/AuthOauth2BaseApi.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/api/auth/index.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/api/controllers/ServicesApi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useAppsStore } from './../../store/apps';
import apiClient from '../BaseApi';
import apiClient from '@/api/';
import { components } from '@profcomff/api-uilib/src/openapi/services';

export type ServiceData = components['schemas']['ButtonGet'];
Expand Down
2 changes: 1 addition & 1 deletion src/api/controllers/TimetableApi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { stringifyDate, getDateWithDayOffset } from './../../utils/date';
import { useTimetableStore } from './../../store/timetable';
import apiClient from '@/api/BaseApi';
import apiClient from '@/api/';

interface GetLecturersParams {
query?: string;
Expand Down
2 changes: 1 addition & 1 deletion src/api/controllers/UserdataApi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { apply, checkToken, showErrorToast } from './auth/decorators';
import { UserdataUpdateUser } from '../models';
import apiClient from '../BaseApi';
import apiClient from '@/api/';

export class UserdataApi {
static getUser = apply(
Expand Down
4 changes: 2 additions & 2 deletions src/api/controllers/auth/AuthApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { apply, checkToken, scoped, showErrorToast } from './decorators';
import { scopename } from '@/models/ScopeName';
import { useProfileStore } from '@/store/profile';
import { LocalStorage, LocalStorageItem } from '@/models/LocalStorage';
import { UNKNOWN_DEVICE } from '@/api/auth/AuthOauth2BaseApi';
import { UNKNOWN_DEVICE } from '@/api/models';
import router from '@/router';
import apiClient from '@/api/BaseApi';
import apiClient from '@/api/';

export enum UserInfo {
Groups = 'groups',
Expand Down
2 changes: 1 addition & 1 deletion src/api/controllers/auth/decorators.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import apiClient from '@/api/BaseApi';
import apiClient from '@/api/';
import { ToastType } from '@/models';
import router from '@/router';
import { useProfileStore } from '@/store/profile';
Expand Down
4 changes: 4 additions & 0 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
export * from './controllers';
import { createClient } from '@profcomff/api-uilib';

const apiClient = createClient(import.meta.env.VITE_API_URL);
export default apiClient;
10 changes: 10 additions & 0 deletions src/api/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,13 @@ export interface AchievementGet {
owner_user_id: number;
recievers?: Reciever[] | undefined;
}

export const UNKNOWN_DEVICE = 'unknown device';
export type AuthMethods =
| 'github'
| 'google'
| 'vk'
| 'lk-msu'
| 'yandex'
| 'my-msu'
| 'physics-msu';
10 changes: 5 additions & 5 deletions src/components/IrdomAuthButton.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<script setup lang="ts">
import { AuthOauth2BaseApi } from '@/api/auth';
import { onMounted, ref } from 'vue';
import { AuthMethods } from '@/api/auth/AuthOauth2BaseApi';
import { AuthMethods } from '@/api/models';
import apiClient from '@/api/';
export interface AuthButton {
name: string;
api: AuthOauth2BaseApi;
method: AuthMethods;
icon?: string;
color?: string;
Expand All @@ -20,12 +19,13 @@ const props = withDefaults(defineProps<Props>(), { unlink: false });
const authUrl = ref<string | undefined>('');
onMounted(async () => {
authUrl.value = await props.button.api.getAuthUrl(props.button.method);
const { data } = await apiClient.GET(`/auth/${props.button.method}/auth_url`);
authUrl.value = data?.url;
});
async function clickHandler() {
if (props.unlink) {
await props.button.api.unregister(props.button.method);
await apiClient.DELETE(`/auth/${props.button.method}`);
location.reload(); // TODO: придумать нормальное решение
} else if (authUrl.value) {
window.open(authUrl.value, '_self');
Expand Down
2 changes: 1 addition & 1 deletion src/components/IrdomToolbar.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script setup lang="ts">
import { useRoute, useRouter } from 'vue-router';
import { useProfileStore } from '@/store/profile';
import apiClient from '@/api/BaseApi';
import apiClient from '@/api/';
export interface ToolbarMenuItem {
name: string;
Expand Down
8 changes: 0 additions & 8 deletions src/constants/authButtons.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,45 @@
import { oauth2BaseApi } from '@/api/auth/AuthOauth2BaseApi';
import logos from '@/assets/logos.svg';
import { AuthButton } from '@/components/IrdomAuthButton.vue';

export const authButtons: AuthButton[] = [
{
name: 'ЛК МГУ',
api: oauth2BaseApi,
method: 'lk-msu',
icon: `${logos}#msu`,
color: '#58b4470d',
},
{
name: '@physics.msu.ru',
api: oauth2BaseApi,
method: 'physics-msu',
icon: `${logos}#ff`,
color: '#00014c0d',
},
{
name: '@my.msu.ru',
api: oauth2BaseApi,
method: 'my-msu',
icon: `${logos}#msu`,
color: '#2f39500d',
},
{
name: 'Yandex',
api: oauth2BaseApi,
method: 'yandex',
icon: `${logos}#yandex`,
color: '#e94c000d',
},
{
name: 'ВК',
api: oauth2BaseApi,
method: 'vk',
icon: `${logos}#vk`,
color: '#0077ff0d',
},
{
name: 'Github',
api: oauth2BaseApi,
method: 'github',
icon: `${logos}#github`,
color: '#24292f0d',
},
{
name: 'Google',
api: oauth2BaseApi,
method: 'google',
icon: `${logos}#google`,
color: '#58b4470d',
Expand Down
19 changes: 14 additions & 5 deletions src/router/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { NavigationGuard, RouteRecordRaw } from 'vue-router';
import { useProfileStore } from '@/store/profile';
import { useToastStore } from '@/store/toast';
import { AuthApi } from '@/api';
import { AuthMethods, oauth2BaseApi } from '@/api/auth/AuthOauth2BaseApi';
import { AuthMethods } from '@/api/models';
import apiClient from '@/api/';

export const authRoutes: RouteRecordRaw[] = [
{
Expand Down Expand Up @@ -54,17 +55,25 @@ export const authHandler: NavigationGuard = async to => {
};
}

const authMethod = oauth2BaseApi;

if (to.hash === '' && Object.keys(to.query).length === 0) {
return {
path: '/auth/error',
query: { text: 'Отсутствуют параметры входа' },
replace: true,
};
}
const action = profileStore.isUserLogged ? authMethod.linkNewAccount : authMethod.login;
const { response, data, error } = await action.call(authMethod, methodName, to.query);
const action = profileStore.isUserLogged
? apiClient.POST(`/auth/${methodName}/registration`, {
body: { ...to.query },
})
: apiClient.POST(`/auth/${methodName}/registration`, {
body: {
...to.query,
session_name: navigator.userAgent ?? 'unknown device',
},
});
// authMethod.linkNewAccount : authMethod.login;
const { response, data, error } = await action;

if (response.status === 200 && data?.token) {
LocalStorage.set(LocalStorageItem.Token, data.token);
Expand Down
2 changes: 1 addition & 1 deletion src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';
import { timetableRoutes, timetableHandler } from './timetable';
import AppsView from '@/views/apps/AppsView.vue';
import { useProfileStore } from '@/store/profile';
import apiClient from '@/api/BaseApi';
import apiClient from '@/api/';

const routes: RouteRecordRaw[] = [
{
Expand Down
2 changes: 1 addition & 1 deletion src/store/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { scopename } from './../models/ScopeName';
import { LocalStorage, LocalStorageItem } from '@/models/LocalStorage';
import { defineStore } from 'pinia';
import { computed, ref } from 'vue';
import apiClient from '@/api/BaseApi';
import apiClient from '@/api/';

export const useProfileStore = defineStore('profile', () => {
const id = ref<number | null>(null);
Expand Down
2 changes: 1 addition & 1 deletion src/views/admin/achievement/AchievementListView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import AccessRestricted from '@/components/AccessRestricted.vue';
import IrdomLayout from '@/components/IrdomLayout.vue';
import { scopename } from '@/models/ScopeName';
import { useToolbar } from '@/store/toolbar';
import apiClient from '@/api/BaseApi';
import apiClient from '@/api/';
const toolbar = useToolbar();
Expand Down
2 changes: 1 addition & 1 deletion src/views/admin/achievement/AchievementRecieversView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import AccessRestricted from '@/components/AccessRestricted.vue';
import IrdomLayout from '@/components/IrdomLayout.vue';
import { scopename } from '@/models/ScopeName';
import { useToolbar } from '@/store/toolbar';
import apiClient from '@/api/BaseApi';
import apiClient from '@/api/';
const route = useRoute();
const toolbar = useToolbar();
Expand Down
2 changes: 1 addition & 1 deletion src/views/admin/achievement/AchievementRow.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup lang="ts">
import { AchievementGet } from '@/api/models';
import apiClient from '@/api/BaseApi';
import apiClient from '@/api/';
import { ref, watch } from 'vue';
import { useRouter } from 'vue-router';
Expand Down
2 changes: 1 addition & 1 deletion src/views/admin/group/AdminGroupView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import ScopesTable from '../ScopesTable.vue';
import { onMounted, computed } from 'vue';
import { useRoute } from 'vue-router';
import { AuthApi } from '@/api';
import apiClient from '@/api/BaseApi';
import apiClient from '@/api/';
import AccessRestricted from '@/components/AccessRestricted.vue';
import IrdomLayout from '@/components/IrdomLayout.vue';
import { scopename } from '@/models/ScopeName';
Expand Down
2 changes: 1 addition & 1 deletion src/views/admin/groups/AdminGroupsView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import IrdomLayout from '@/components/IrdomLayout.vue';
import { useToolbar } from '@/store/toolbar';
import { useProfileStore } from '@/store/profile';
import { scopename } from '@/models/ScopeName';
import apiClient from '@/api/BaseApi';
import apiClient from '@/api/';
const authStore = useAuthStore();
const toolbar = useToolbar();
Expand Down
2 changes: 1 addition & 1 deletion src/views/admin/groups/GroupTreeNode.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup lang="ts">
import apiClient from '@/api/BaseApi';
import apiClient from '@/api/';
import { scopename } from '@/models/ScopeName';
import { StoreGroup, useAuthStore } from '@/store/auth';
import { useProfileStore } from '@/store/profile';
Expand Down
2 changes: 1 addition & 1 deletion src/views/admin/scopes/AdminScopesView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import AccessRestricted from '@/components/AccessRestricted.vue';
import { useAuthStore } from '@/store/auth';
import { useProfileStore } from '@/store/profile';
import { useToolbar } from '@/store/toolbar';
import apiClient from '@/api/BaseApi';
import apiClient from '@/api/';
const authStore = useAuthStore();
const profileStore = useProfileStore();
Expand Down
2 changes: 1 addition & 1 deletion src/views/apps/ApplicationFrame.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { SuperappAuthItem } from '@/models/SuperappData';
import FullscreenLoader from '@/components/FullscreenLoader.vue';
import { AuthApi } from '@/api/controllers/auth/AuthApi';
import { ServiceData } from '@/api/controllers/ServicesApi';
import apiClient from '@/api/BaseApi';
import apiClient from '@/api/';
const route = useRoute();
const toolbar = useToolbar();
Expand Down
2 changes: 1 addition & 1 deletion src/views/apps/AsyncContent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useAppsStore } from '@/store/apps';
import { useProfileStore } from '@/store/profile';
import { useToastStore } from '@/store/toast';
import { RouterLink } from 'vue-router';
import apiClient from '@/api/BaseApi';
import apiClient from '@/api/';
const appsStore = useAppsStore();
const profileStore = useProfileStore();
Expand Down
10 changes: 7 additions & 3 deletions src/views/auth/OauthRegisterView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { isAxiosError } from 'axios';
import IrdomLayout from '@/components/IrdomLayout.vue';
import { useToolbar } from '@/store/toolbar';
import { useProfileStore } from '@/store/profile';
import { oauth2BaseApi, AuthMethods } from '@/api/auth/AuthOauth2BaseApi';
import { AuthMethods } from '@/api/models';
import apiClient from '@/api/';
const router = useRouter();
const toolbar = useToolbar();
Expand All @@ -24,8 +25,11 @@ async function handleAccept() {
return router.replace({ path: '/auth/error', query: { text: 'Непредвиденная ошибка' } });
}
const { response, data, error } = await oauth2BaseApi.register(idTokenIssuer, {
id_token: idToken,
const { response, data, error } = await apiClient.POST(`/auth/${idTokenIssuer}/registration`, {
body: {
id_token: idToken,
session_name: navigator.userAgent ?? 'unknown_device',
},
});
if (response.status == 200 && data?.token) {
localStorage.setItem('token', data.token);
Expand Down
Loading

0 comments on commit b965565

Please sign in to comment.