Use useAuth hook
This commit is contained in:
parent
03a2fff2a5
commit
9b97e3279b
2 changed files with 141 additions and 143 deletions
|
@ -26,6 +26,7 @@ import {
|
||||||
interface AuthProviderProps {
|
interface AuthProviderProps {
|
||||||
children: React.ReactNode;
|
children: React.ReactNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
|
const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
|
||||||
const intl = useIntl();
|
const intl = useIntl();
|
||||||
const notify = useNotifier();
|
const notify = useNotifier();
|
||||||
|
@ -136,7 +137,7 @@ export const useAuth = () => {
|
||||||
isAuthenticated,
|
isAuthenticated,
|
||||||
tokenAuthLoading: user.tokenAuthLoading,
|
tokenAuthLoading: user.tokenAuthLoading,
|
||||||
tokenVerifyLoading: user.tokenVerifyLoading,
|
tokenVerifyLoading: user.tokenVerifyLoading,
|
||||||
user
|
user: user.user
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@ import { appsSection } from "./apps/urls";
|
||||||
import AttributeSection from "./attributes";
|
import AttributeSection from "./attributes";
|
||||||
import { attributeSection } from "./attributes/urls";
|
import { attributeSection } from "./attributes/urls";
|
||||||
import Auth, { getAuthToken, removeAuthToken } from "./auth";
|
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 LoginLoading from "./auth/components/LoginLoading/LoginLoading";
|
||||||
import SectionRoute from "./auth/components/SectionRoute";
|
import SectionRoute from "./auth/components/SectionRoute";
|
||||||
import { isJwtError } from "./auth/errors";
|
import { isJwtError } from "./auth/errors";
|
||||||
|
@ -138,7 +138,9 @@ const App: React.FC = () => {
|
||||||
<BackgroundTasksProvider>
|
<BackgroundTasksProvider>
|
||||||
<AppStateProvider>
|
<AppStateProvider>
|
||||||
<ShopProvider>
|
<ShopProvider>
|
||||||
|
<AuthProvider>
|
||||||
<Routes />
|
<Routes />
|
||||||
|
</AuthProvider>
|
||||||
</ShopProvider>
|
</ShopProvider>
|
||||||
</AppStateProvider>
|
</AppStateProvider>
|
||||||
</BackgroundTasksProvider>
|
</BackgroundTasksProvider>
|
||||||
|
@ -154,19 +156,18 @@ const App: React.FC = () => {
|
||||||
const Routes: React.FC = () => {
|
const Routes: React.FC = () => {
|
||||||
const intl = useIntl();
|
const intl = useIntl();
|
||||||
const [, dispatchAppState] = useAppState();
|
const [, dispatchAppState] = useAppState();
|
||||||
|
const {
|
||||||
return (
|
|
||||||
<>
|
|
||||||
<WindowTitle title={intl.formatMessage(commonMessages.dashboard)} />
|
|
||||||
<AuthProvider>
|
|
||||||
{({
|
|
||||||
hasToken,
|
hasToken,
|
||||||
isAuthenticated,
|
isAuthenticated,
|
||||||
tokenAuthLoading,
|
tokenAuthLoading,
|
||||||
tokenVerifyLoading,
|
tokenVerifyLoading,
|
||||||
user
|
user
|
||||||
}) =>
|
} = useAuth();
|
||||||
isAuthenticated && !tokenAuthLoading && !tokenVerifyLoading ? (
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<WindowTitle title={intl.formatMessage(commonMessages.dashboard)} />
|
||||||
|
{isAuthenticated && !tokenAuthLoading && !tokenVerifyLoading ? (
|
||||||
<AppLayout>
|
<AppLayout>
|
||||||
<Navigator />
|
<Navigator />
|
||||||
<ErrorBoundary
|
<ErrorBoundary
|
||||||
|
@ -277,9 +278,7 @@ const Routes: React.FC = () => {
|
||||||
component={WarehouseSection}
|
component={WarehouseSection}
|
||||||
/>
|
/>
|
||||||
{createConfigurationMenu(intl).filter(menu =>
|
{createConfigurationMenu(intl).filter(menu =>
|
||||||
menu.menuItems.map(item =>
|
menu.menuItems.map(item => hasPermission(item.permission, user))
|
||||||
hasPermission(item.permission, user)
|
|
||||||
)
|
|
||||||
).length > 0 && (
|
).length > 0 && (
|
||||||
<SectionRoute
|
<SectionRoute
|
||||||
exact
|
exact
|
||||||
|
@ -295,9 +294,7 @@ const Routes: React.FC = () => {
|
||||||
<LoginLoading />
|
<LoginLoading />
|
||||||
) : (
|
) : (
|
||||||
<Auth hasToken={hasToken} />
|
<Auth hasToken={hasToken} />
|
||||||
)
|
)}
|
||||||
}
|
|
||||||
</AuthProvider>
|
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue