balibabu commited on
Commit
30910b7
·
1 Parent(s): b40a87c

feat: call useLoginWithGithub in useAuth (#107)

Browse files
web/src/hooks/authHook.ts CHANGED
@@ -1,15 +1,8 @@
1
  import authorizationUtil from '@/utils/authorizationUtil';
2
- import { useMemo, useState } from 'react';
 
3
  import { useNavigate, useSearchParams } from 'umi';
4
 
5
- export const useAuth = () => {
6
- const [isLogin, setIsLogin] = useState(
7
- () => !!authorizationUtil.getAuthorization(),
8
- );
9
-
10
- return { isLogin };
11
- };
12
-
13
  export const useLoginWithGithub = () => {
14
  const [currentQueryParameters, setSearchParams] = useSearchParams();
15
  const error = currentQueryParameters.get('error');
@@ -20,6 +13,7 @@ export const useLoginWithGithub = () => {
20
  const navigate = useNavigate();
21
 
22
  if (error) {
 
23
  navigate('/login');
24
  newQueryParameters.delete('error');
25
  setSearchParams(newQueryParameters);
@@ -33,4 +27,16 @@ export const useLoginWithGithub = () => {
33
  newQueryParameters.delete('auth');
34
  setSearchParams(newQueryParameters);
35
  }
 
 
 
 
 
 
 
 
 
 
 
 
36
  };
 
1
  import authorizationUtil from '@/utils/authorizationUtil';
2
+ import { message } from 'antd';
3
+ import { useEffect, useMemo, useState } from 'react';
4
  import { useNavigate, useSearchParams } from 'umi';
5
 
 
 
 
 
 
 
 
 
6
  export const useLoginWithGithub = () => {
7
  const [currentQueryParameters, setSearchParams] = useSearchParams();
8
  const error = currentQueryParameters.get('error');
 
13
  const navigate = useNavigate();
14
 
15
  if (error) {
16
+ message.error(error);
17
  navigate('/login');
18
  newQueryParameters.delete('error');
19
  setSearchParams(newQueryParameters);
 
27
  newQueryParameters.delete('auth');
28
  setSearchParams(newQueryParameters);
29
  }
30
+ return auth;
31
+ };
32
+
33
+ export const useAuth = () => {
34
+ const auth = useLoginWithGithub();
35
+ const [isLogin, setIsLogin] = useState(true);
36
+
37
+ useEffect(() => {
38
+ setIsLogin(!!auth || !!authorizationUtil.getAuthorization());
39
+ }, [auth]);
40
+
41
+ return { isLogin };
42
  };
web/src/layouts/index.tsx CHANGED
@@ -4,7 +4,6 @@ import { Outlet } from 'umi';
4
  import '../locales/config';
5
  import Header from './components/header';
6
 
7
- import { useLoginWithGithub } from '@/hooks/authHook';
8
  import styles from './index.less';
9
 
10
  const { Content } = Layout;
@@ -14,8 +13,6 @@ const App: React.FC = () => {
14
  token: { colorBgContainer, borderRadiusLG },
15
  } = theme.useToken();
16
 
17
- useLoginWithGithub();
18
-
19
  return (
20
  <Layout className={styles.layout}>
21
  <Layout>
 
4
  import '../locales/config';
5
  import Header from './components/header';
6
 
 
7
  import styles from './index.less';
8
 
9
  const { Content } = Layout;
 
13
  token: { colorBgContainer, borderRadiusLG },
14
  } = theme.useToken();
15
 
 
 
16
  return (
17
  <Layout className={styles.layout}>
18
  <Layout>