ragflow/web/src/hooks/authHook.ts
balibabu fce14ee187
feat: add loading to ChatContainer and set font family to inter and add tooltip to Form.Item and download documents on the document list page (#136)
* feat: download documents on the document list page

* feat: add tooltip to Form.Item

* feat: set font family to inter

* feat: add loading to ChatContainer
2024-03-20 18:20:42 +08:00

43 lines
1.2 KiB
TypeScript

import authorizationUtil from '@/utils/authorizationUtil';
import { message } from 'antd';
import { useEffect, useMemo, useState } from 'react';
import { useNavigate, useSearchParams } from 'umi';
export const useLoginWithGithub = () => {
const [currentQueryParameters, setSearchParams] = useSearchParams();
const error = currentQueryParameters.get('error');
const newQueryParameters: URLSearchParams = useMemo(
() => new URLSearchParams(currentQueryParameters.toString()),
[currentQueryParameters],
);
const navigate = useNavigate();
if (error) {
message.error(error);
navigate('/login');
newQueryParameters.delete('error');
setSearchParams(newQueryParameters);
return;
}
const auth = currentQueryParameters.get('auth');
if (auth) {
authorizationUtil.setAuthorization(auth);
newQueryParameters.delete('auth');
setSearchParams(newQueryParameters);
}
return auth;
};
export const useAuth = () => {
const auth = useLoginWithGithub();
const [isLogin, setIsLogin] = useState<Nullable<boolean>>(null);
useEffect(() => {
setIsLogin(!!authorizationUtil.getAuthorization() || !!auth);
}, [auth]);
return { isLogin };
};