|
1 | 1 | import { ref, computed, onMounted, onUnmounted } from 'vue' |
2 | 2 | import { defineStore } from 'pinia' |
3 | | -import { callAdminForthApi } from '@/utils'; |
| 3 | +import { callAdminForthApi, handleNotAuthorized } from '@/utils'; |
4 | 4 | import websocket from '@/websocket'; |
5 | 5 | import { useAdminforth } from '@/adminforth'; |
6 | 6 |
|
7 | | -import type { AdminForthResourceCommon, AdminForthResourceColumnCommon, GetBaseConfigResponse, ResourceVeryShort, AdminUser, UserData, AdminForthConfigMenuItem, AdminForthConfigForFrontend, AdminForthResourceFrontend } from '@/types/Common'; |
| 7 | +import type { AdminForthResourceCommon, AdminForthResourceColumnCommon, GetConfigResponse, ResourceVeryShort, AdminUser, UserData, AdminForthConfigMenuItem, AdminForthConfigForFrontend, AdminForthResourceFrontend } from '@/types/Common'; |
8 | 8 | import type { Ref } from 'vue' |
9 | 9 |
|
10 | 10 |
|
@@ -75,25 +75,42 @@ export const useCoreStore = defineStore('core', () => { |
75 | 75 | window.localStorage.setItem('af__theme', theme.value); |
76 | 76 | } |
77 | 77 |
|
78 | | - async function fetchMenuAndResource() { |
79 | | - const resp: GetBaseConfigResponse = await callAdminForthApi({ |
80 | | - path: '/get_base_config', |
| 78 | + |
| 79 | + async function fetchConfig( |
| 80 | + { redirectToLoginIfNotLoggedIn = true }: { redirectToLoginIfNotLoggedIn?: boolean } = {} |
| 81 | + ): Promise<GetConfigResponse | null> { |
| 82 | + const resp: GetConfigResponse | null = await callAdminForthApi({ |
| 83 | + path: '/get_config', |
81 | 84 | method: 'GET', |
82 | 85 | }); |
83 | 86 |
|
84 | 87 | if(!resp){ |
85 | | - return |
| 88 | + return null |
| 89 | + } |
| 90 | + |
| 91 | + config.value = { ...config.value, ...resp.config } as AdminForthConfigForFrontend; |
| 92 | + |
| 93 | + if (!resp.loggedIn) { |
| 94 | + if (redirectToLoginIfNotLoggedIn) { |
| 95 | + await handleNotAuthorized(); |
| 96 | + } |
| 97 | + return resp; |
86 | 98 | } |
| 99 | + |
87 | 100 | menu.value = resp.menu; |
88 | 101 | resourceById.value = resp.resources.reduce((acc: Record<string, ResourceVeryShort>, resource: ResourceVeryShort) => { |
89 | 102 | acc[resource.resourceId] = resource; |
90 | 103 | return acc; |
91 | 104 | }, {}); |
92 | | - config.value = resp.config; |
93 | 105 | adminUser.value = resp.adminUser; |
94 | 106 | userData.value = resp.user; |
95 | 107 | console.log('🌍 AdminForth v', resp.version); |
96 | 108 | subscribeToMenuRefresh(); |
| 109 | + return resp; |
| 110 | + } |
| 111 | + |
| 112 | + async function fetchMenuAndResource() { |
| 113 | + await fetchConfig(); |
97 | 114 | } |
98 | 115 |
|
99 | 116 | async function refreshMenu() { |
@@ -234,14 +251,6 @@ export const useCoreStore = defineStore('core', () => { |
234 | 251 | isResourceFetching.value = false; |
235 | 252 | } |
236 | 253 |
|
237 | | - async function getPublicConfig() { |
238 | | - const res = await callAdminForthApi({ |
239 | | - path: '/get_public_config', |
240 | | - method: 'GET', |
241 | | - }); |
242 | | - config.value = {...config.value, ...res}; |
243 | | - } |
244 | | - |
245 | 254 | async function getLoginFormConfig() { |
246 | 255 | const res = await callAdminForthApi({ |
247 | 256 | path: '/get_login_form_config', |
@@ -279,8 +288,8 @@ export const useCoreStore = defineStore('core', () => { |
279 | 288 | username, |
280 | 289 | userFullname, |
281 | 290 | userAvatarUrl, |
282 | | - getPublicConfig, |
283 | | - fetchMenuAndResource, |
| 291 | + fetchConfig, |
| 292 | + fetchMenuAndResource, |
284 | 293 | refreshMenu, |
285 | 294 | getLoginFormConfig, |
286 | 295 | fetchRecord, |
|
0 commit comments