ragflow/web/src/hooks/auth-hooks.ts
balibabu 906c0c5c89
fix: Set the default value of Self RAG to false #1220 (#1702)
### What problem does this PR solve?

fix: Set the default value of Self RAG  to false #1220
fix: Change all tool file names to kebab format

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)
2024-07-25 14:38:17 +08:00

43 lines
1.2 KiB
TypeScript

import authorizationUtil from '@/utils/authorization-util';
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 };
};