/
app.ts
58 lines (50 loc) · 1.31 KB
/
app.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { defineAppConfig, history, defineDataLoader } from 'ice';
import { fetchUserInfo } from './services/user';
import { defineAuthConfig } from '@ice/plugin-auth/types';
import { defineStoreConfig } from '@ice/plugin-store/types';
import { defineRequestConfig } from '@ice/plugin-request/types';
// App config, see https://v3.ice.work/docs/guide/basic/app
export default defineAppConfig(() => ({
// Set your configs here.
}));
export const authConfig = defineAuthConfig(async (appData) => {
const { userInfo = {} } = appData;
if (userInfo.error) {
history?.push(`/login?redirect=${window.location.pathname}`);
}
return {
initialAuth: {
admin: userInfo.userType === 'admin',
user: userInfo.userType === 'user',
},
};
});
export const storeConfig = defineStoreConfig(async (appData) => {
const { userInfo = {} } = appData;
return {
initialStates: {
user: {
currentUser: userInfo,
},
},
};
});
export const request = defineRequestConfig(() => ({
baseURL: '/api',
}));
export const dataLoader = defineDataLoader(async () => {
const userInfo = await getUserInfo();
return {
userInfo,
};
});
async function getUserInfo() {
try {
const userInfo = await fetchUserInfo();
return userInfo;
} catch (error) {
return {
error,
};
}
}