Skip to content

Commit

Permalink
feat: set initial state of auth to null (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
cike8899 committed Mar 7, 2024
1 parent cfc3b62 commit b69b5dd
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
1 change: 1 addition & 0 deletions web/.umirc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default defineConfig({
hack: `true; @import "~@/less/index.less";`,
},
},
devtool: 'source-map',
proxy: {
'/v1': {
target: 'http://123.60.95.134:9380/',
Expand Down
5 changes: 3 additions & 2 deletions web/src/hooks/authHook.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import authorizationUtil from '@/utils/authorizationUtil';
import { message } from 'antd';
import { useEffect, useMemo, useState } from 'react';
import { Nullable } from 'typings';
import { useNavigate, useSearchParams } from 'umi';

export const useLoginWithGithub = () => {
Expand Down Expand Up @@ -32,10 +33,10 @@ export const useLoginWithGithub = () => {

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

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

return { isLogin };
Expand Down
6 changes: 4 additions & 2 deletions web/src/wrappers/auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { Navigate, Outlet } from 'umi';

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

return <></>;
};

0 comments on commit b69b5dd

Please sign in to comment.