add web
This commit is contained in:
54
web/src/hooks/auth-hooks.ts
Normal file
54
web/src/hooks/auth-hooks.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
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 };
|
||||
};
|
||||
Reference in New Issue
Block a user