浏览代码

feat: set initial state of auth to null (#108)

tags/v0.1.0
balibabu 1年前
父节点
当前提交
b69b5dd4e5
没有帐户链接到提交者的电子邮件
共有 3 个文件被更改,包括 8 次插入4 次删除
  1. 1
    0
      web/.umirc.ts
  2. 3
    2
      web/src/hooks/authHook.ts
  3. 4
    2
      web/src/wrappers/auth.tsx

+ 1
- 0
web/.umirc.ts 查看文件

hack: `true; @import "~@/less/index.less";`, hack: `true; @import "~@/less/index.less";`,
}, },
}, },
devtool: 'source-map',
proxy: { proxy: {
'/v1': { '/v1': {
target: 'http://123.60.95.134:9380/', target: 'http://123.60.95.134:9380/',

+ 3
- 2
web/src/hooks/authHook.ts 查看文件

import authorizationUtil from '@/utils/authorizationUtil'; import authorizationUtil from '@/utils/authorizationUtil';
import { message } from 'antd'; import { message } from 'antd';
import { useEffect, useMemo, useState } from 'react'; import { useEffect, useMemo, useState } from 'react';
import { Nullable } from 'typings';
import { useNavigate, useSearchParams } from 'umi'; import { useNavigate, useSearchParams } from 'umi';


export const useLoginWithGithub = () => { export const useLoginWithGithub = () => {


export const useAuth = () => { export const useAuth = () => {
const auth = useLoginWithGithub(); const auth = useLoginWithGithub();
const [isLogin, setIsLogin] = useState(true);
const [isLogin, setIsLogin] = useState<Nullable<boolean>>(null);


useEffect(() => { useEffect(() => {
setIsLogin(!!auth || !!authorizationUtil.getAuthorization());
setIsLogin(!!authorizationUtil.getAuthorization() || !!auth);
}, [auth]); }, [auth]);


return { isLogin }; return { isLogin };

+ 4
- 2
web/src/wrappers/auth.tsx 查看文件

export default () => { export default () => {
const { isLogin } = useAuth(); const { isLogin } = useAuth();
if (isLogin) {
if (isLogin === true) {
return <Outlet />; return <Outlet />;
} else {
} else if (isLogin === false) {
return <Navigate to="/login" />; return <Navigate to="/login" />;
} }
return <></>;
}; };

正在加载...
取消
保存