mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-08 20:42:30 +08:00
### What problem does this PR solve? Fix: Try to fix the issue of not being able to log in through Oauth2 #9601 ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue)
55 lines
1.5 KiB
TypeScript
55 lines
1.5 KiB
TypeScript
import message from '@/components/ui/message';
|
|
import authorizationUtil from '@/utils/authorization-util';
|
|
import { useEffect, useMemo, useState } from 'react';
|
|
import { useNavigate, useSearchParams } from 'umi';
|
|
|
|
export const useOAuthCallback = () => {
|
|
const [currentQueryParameters, setSearchParams] = useSearchParams();
|
|
const error = currentQueryParameters.get('error');
|
|
const newQueryParameters: URLSearchParams = useMemo(
|
|
() => new URLSearchParams(currentQueryParameters.toString()),
|
|
[currentQueryParameters],
|
|
);
|
|
const navigate = useNavigate();
|
|
|
|
useEffect(() => {
|
|
if (error) {
|
|
message.error(error);
|
|
setTimeout(() => {
|
|
navigate('/login');
|
|
newQueryParameters.delete('error');
|
|
setSearchParams(newQueryParameters);
|
|
}, 1000);
|
|
return;
|
|
}
|
|
|
|
const auth = currentQueryParameters.get('auth');
|
|
if (auth) {
|
|
authorizationUtil.setAuthorization(auth);
|
|
newQueryParameters.delete('auth');
|
|
setSearchParams(newQueryParameters);
|
|
navigate('/');
|
|
}
|
|
}, [
|
|
error,
|
|
currentQueryParameters,
|
|
newQueryParameters,
|
|
navigate,
|
|
setSearchParams,
|
|
]);
|
|
|
|
console.debug(currentQueryParameters.get('auth'));
|
|
return currentQueryParameters.get('auth');
|
|
};
|
|
|
|
export const useAuth = () => {
|
|
const auth = useOAuthCallback();
|
|
const [isLogin, setIsLogin] = useState<Nullable<boolean>>(null);
|
|
|
|
useEffect(() => {
|
|
setIsLogin(!!authorizationUtil.getAuthorization() || !!auth);
|
|
}, [auth]);
|
|
|
|
return { isLogin };
|
|
};
|