| import { Authorization } from '@/constants/authorization'; |
| import userService from '@/services/user-service'; |
| import authorizationUtil from '@/utils/authorization-util'; |
| import { useMutation } from '@tanstack/react-query'; |
| import { message } from 'antd'; |
| import { useTranslation } from 'react-i18next'; |
| import { history } from 'umi'; |
|
|
| export interface ILoginRequestBody { |
| email: string; |
| password: string; |
| } |
|
|
| export interface IRegisterRequestBody extends ILoginRequestBody { |
| nickname: string; |
| } |
|
|
| export const useLogin = () => { |
| const { t } = useTranslation(); |
|
|
| const { |
| data, |
| isPending: loading, |
| mutateAsync, |
| } = useMutation({ |
| mutationKey: ['login'], |
| mutationFn: async (params: { email: string; password: string }) => { |
| const { data: res = {}, response } = await userService.login(params); |
| if (res.retcode === 0) { |
| const { data } = res; |
| message.success(t('message.logged')); |
| const authorization = response.headers.get(Authorization); |
| const token = data.access_token; |
| const userInfo = { |
| avatar: data.avatar, |
| name: data.nickname, |
| email: data.email, |
| }; |
| authorizationUtil.setItems({ |
| Authorization: authorization, |
| userInfo: JSON.stringify(userInfo), |
| Token: token, |
| }); |
| } |
| return res.retcode; |
| }, |
| }); |
|
|
| return { data, loading, login: mutateAsync }; |
| }; |
|
|
| export const useRegister = () => { |
| const { t } = useTranslation(); |
|
|
| const { |
| data, |
| isPending: loading, |
| mutateAsync, |
| } = useMutation({ |
| mutationKey: ['register'], |
| mutationFn: async (params: { |
| email: string; |
| password: string; |
| nickname: string; |
| }) => { |
| const { data = {} } = await userService.register(params); |
| if (data.retcode === 0) { |
| message.success(t('message.registered')); |
| } |
| return data.retcode; |
| }, |
| }); |
|
|
| return { data, loading, register: mutateAsync }; |
| }; |
|
|
| export const useLogout = () => { |
| const { t } = useTranslation(); |
| const { |
| data, |
| isPending: loading, |
| mutateAsync, |
| } = useMutation({ |
| mutationKey: ['logout'], |
| mutationFn: async () => { |
| const { data = {} } = await userService.logout(); |
| if (data.retcode === 0) { |
| message.success(t('message.logout')); |
| authorizationUtil.removeAll(); |
| history.push('/login'); |
| } |
| return data.retcode; |
| }, |
| }); |
|
|
| return { data, loading, logout: mutateAsync }; |
| }; |
|
|