From 9b97e3279bb4ae83cb0f2d30029f0f184f90349a Mon Sep 17 00:00:00 2001 From: Dawid Tarasiuk Date: Tue, 14 Jul 2020 10:11:43 +0200 Subject: [PATCH] Use useAuth hook --- src/auth/AuthProvider.tsx | 3 +- src/index.tsx | 281 +++++++++++++++++++------------------- 2 files changed, 141 insertions(+), 143 deletions(-) diff --git a/src/auth/AuthProvider.tsx b/src/auth/AuthProvider.tsx index 5db93208c..05b0f8236 100644 --- a/src/auth/AuthProvider.tsx +++ b/src/auth/AuthProvider.tsx @@ -26,6 +26,7 @@ import { interface AuthProviderProps { children: React.ReactNode; } + const AuthProvider: React.FC = ({ children }) => { const intl = useIntl(); const notify = useNotifier(); @@ -136,7 +137,7 @@ export const useAuth = () => { isAuthenticated, tokenAuthLoading: user.tokenAuthLoading, tokenVerifyLoading: user.tokenVerifyLoading, - user + user: user.user }; }; diff --git a/src/index.tsx b/src/index.tsx index 15250b8cd..4bf643631 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -20,7 +20,7 @@ import { appsSection } from "./apps/urls"; import AttributeSection from "./attributes"; import { attributeSection } from "./attributes/urls"; import Auth, { getAuthToken, removeAuthToken } from "./auth"; -import AuthProvider from "./auth/AuthProvider"; +import AuthProvider, { useAuth } from "./auth/AuthProvider"; import LoginLoading from "./auth/components/LoginLoading/LoginLoading"; import SectionRoute from "./auth/components/SectionRoute"; import { isJwtError } from "./auth/errors"; @@ -138,7 +138,9 @@ const App: React.FC = () => { - + + + @@ -154,150 +156,145 @@ const App: React.FC = () => { const Routes: React.FC = () => { const intl = useIntl(); const [, dispatchAppState] = useAppState(); + const { + hasToken, + isAuthenticated, + tokenAuthLoading, + tokenVerifyLoading, + user + } = useAuth(); return ( <> - - {({ - hasToken, - isAuthenticated, - tokenAuthLoading, - tokenVerifyLoading, - user - }) => - isAuthenticated && !tokenAuthLoading && !tokenVerifyLoading ? ( - - - - dispatchAppState({ - payload: { - error: "unhandled" - }, - type: "displayError" - }) - } - > - - - - - - - - - - - - - - - - - - - - - - {createConfigurationMenu(intl).filter(menu => - menu.menuItems.map(item => - hasPermission(item.permission, user) - ) - ).length > 0 && ( - - )} - - - - - ) : hasToken && tokenVerifyLoading ? ( - - ) : ( - - ) - } - + {isAuthenticated && !tokenAuthLoading && !tokenVerifyLoading ? ( + + + + dispatchAppState({ + payload: { + error: "unhandled" + }, + type: "displayError" + }) + } + > + + + + + + + + + + + + + + + + + + + + + + {createConfigurationMenu(intl).filter(menu => + menu.menuItems.map(item => hasPermission(item.permission, user)) + ).length > 0 && ( + + )} + + + + + ) : hasToken && tokenVerifyLoading ? ( + + ) : ( + + )} ); };