mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-08 20:42:30 +08:00
### What problem does this PR solve? feat: change all file names to lowercase #1574 ### Type of change - [x] New Feature (non-breaking change which adds functionality)
This commit is contained in:
42
web/src/hooks/auth-hooks.ts
Normal file
42
web/src/hooks/auth-hooks.ts
Normal file
@ -0,0 +1,42 @@
|
||||
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 };
|
||||
};
|
||||
Reference in New Issue
Block a user