-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathauth.ts
More file actions
48 lines (44 loc) · 1.31 KB
/
auth.ts
File metadata and controls
48 lines (44 loc) · 1.31 KB
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
/* eslint-disable @typescript-eslint/naming-convention */
import { message } from 'antd';
import { md5 } from 'hash-wasm';
import { rootRouterPath, router } from '@/router';
import { api } from '@/services/api';
import { setToken } from '@/services/request';
import { isSafeRedirect } from '@/utils/helper';
let _email = '';
export const setUserEmail = (email: string) => {
_email = email;
};
export const getUserEmail = () => _email;
export async function login(email: string, password: string) {
_email = email;
const params = { email, pwd: await md5(password) };
try {
const res = await api.login(params);
if (res?.token) {
setToken(res.token);
message.success('登录成功');
const loginFrom = new URLSearchParams(window.location.search).get(
'loginFrom',
);
router.navigate(
isSafeRedirect(loginFrom) ? loginFrom! : rootRouterPath.user,
);
}
} catch (err) {
const e = err as Error;
if (e.message.startsWith('423:')) {
router.navigate(rootRouterPath.inactivated);
} else {
message.error(e.message);
}
}
}
export function logout() {
const currentPath = router.state.location.pathname;
if (currentPath !== rootRouterPath.login) {
setToken('');
router.navigate(rootRouterPath.login);
window.location.reload();
}
}