Skip to content

Commit

Permalink
Merge pull request #256 from Dias999/feature/Handle_forbidden_access
Browse files Browse the repository at this point in the history
Feature/handle forbidden access
  • Loading branch information
andreneto97 authored Oct 11, 2024
2 parents f552228 + f304c2e commit 53ad43a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 15 deletions.
17 changes: 8 additions & 9 deletions packages/react-data-provider/src/ClientProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
import React, {
createContext,
ReactNode,
useEffect,
useState,
useContext,
PropsWithChildren,
} from 'react';
import { HttpError } from './interfaces';

export type ClientContextType = {
baseUrl: string;
onRefreshTokenError: (error?: HttpError) => void;
onForbiddenAccessError: (error?: HttpError) => void;
};

export const ClientContext = createContext<ClientContextType>({
baseUrl: '',
onRefreshTokenError: () => ({}),
onForbiddenAccessError: () => ({}),
});

export const useClient = () => useContext<ClientContextType>(ClientContext);

type Props = {
baseUrl?: string;
onRefreshTokenError?: (error?: HttpError) => void;
children: ReactNode;
};

const ClientProvider = ({
baseUrl: outerBaseUrl,
onRefreshTokenError,
onForbiddenAccessError,
children,
}: Props) => {
}: PropsWithChildren<Partial<ClientContextType>>) => {
const [baseUrl, setBaseUrl] = useState<string>(outerBaseUrl || '');

useEffect(() => {
Expand All @@ -39,7 +36,9 @@ const ClientProvider = ({
}, [outerBaseUrl]);

return (
<ClientContext.Provider value={{ baseUrl, onRefreshTokenError }}>
<ClientContext.Provider
value={{ baseUrl, onRefreshTokenError, onForbiddenAccessError }}
>
{children}
</ClientContext.Provider>
);
Expand Down
5 changes: 4 additions & 1 deletion packages/react-data-provider/src/useDataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { useClient } from './ClientProvider';
const maxAge = 10000;

const useDataProvider = () => {
const { baseUrl, onRefreshTokenError } = useClient();
const { baseUrl, onRefreshTokenError, onForbiddenAccessError } = useClient();

//TODO
//let user inject any http instance that match the HttpClient interface requirements
Expand Down Expand Up @@ -107,6 +107,9 @@ const useDataProvider = () => {
const handleServerError = (err: HttpError) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { code, response, message } = err;
if (response.status === 403) {
onForbiddenAccessError?.(err);
}
throw err;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export type RocketsProps = {
* onAuthError: handleError,
* onLogout: handleLogout,
* handleRefreshTokenError: handleTokenError,
* handleForbiddenAccessError: handleForbiddenError,
* }}
* dataProvider={{ apiUrl: 'https://api.example.com' }}
* layout={{ AppBar: MyAppBar, Layout: MyLayout, menuOptions: myMenuOptions }}
Expand All @@ -66,6 +67,7 @@ const RocketsProvider = ({
<ClientProvider
baseUrl={dataProvider.apiUrl}
onRefreshTokenError={auth.handleRefreshTokenError}
onForbiddenAccessError={auth.handleForbiddenAccessError}
>
<ThemeProvider theme={theme ?? themeLight}>
<ToastContainer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export type RocketsAuthProps = {
* Function to handle token refresh errors.
*/
handleRefreshTokenError: (error: unknown) => void;
/**
* Function to handle forbidden access errors.
*/
handleForbiddenAccessError: (error: unknown) => void;
};

export type RocketsDataProviderProps = {
Expand Down
6 changes: 1 addition & 5 deletions packages/react-navigation/src/components/AuthRoute.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react';

import { Navigate } from 'react-router';
import { useAuth } from '@concepta/react-auth-provider';
import { AuthModule, AuthModuleProps } from '@concepta/react-material-ui/';
import { toast } from 'react-toastify';

Expand All @@ -15,11 +14,8 @@ type AuthRouteProps = {

const AuthRoute = (props: AuthRouteProps) => {
const { home, moduleProps, route } = props;
const { accessToken: authAccessToken } = useAuth();

const accessToken = authAccessToken ?? localStorage.getItem('accessToken');

if (accessToken) {
if (localStorage.getItem('accessToken')) {
return <Navigate to={home} replace />;
}

Expand Down

0 comments on commit 53ad43a

Please sign in to comment.