Skip to content

Commit

Permalink
add protect route
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdi-torabiv committed Aug 14, 2024
1 parent d87e21a commit 55c81d9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
22 changes: 19 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import '@rainbow-me/rainbowkit/styles.css';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { ThemeProvider } from '@mui/material/styles';
import CssBaseline from '@mui/material/CssBaseline';
import { BrowserRouter, Route, Routes } from 'react-router-dom';
import { BrowserRouter, Route, Routes, Navigate } from 'react-router-dom';
import { WagmiProvider } from 'wagmi';
import {
AuthenticationStatus,
Expand All @@ -27,6 +27,7 @@ import Identifiers from './pages/Identifiers';
import Permissions from './pages/Permissions';
import Attestation from './pages/Identifiers/Attestation';
import Callback from './pages/Callback';
import ProtectedRoute from './ProtectedRoute';

const queryClient = new QueryClient({
defaultOptions: {
Expand Down Expand Up @@ -119,8 +120,23 @@ const App: React.FC = () => {
<ThemeProvider theme={theme}>
<CssBaseline />
<Routes>
<Route path="/auth/login" element={<Login />} />
<Route element={<DefaultLayout />}>
<Route
path="/auth/login"
element={
authStatus === 'authenticated' ? (
<Navigate to="/" replace />
) : (
<Login />
)
}
/>
<Route
element={
<ProtectedRoute>
<DefaultLayout />
</ProtectedRoute>
}
>
<Route path="/" element={<Dashboard />} />
<Route path="/identifiers" element={<Identifiers />} />
<Route
Expand Down
24 changes: 14 additions & 10 deletions src/ProtectedRoute.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
import { Navigate } from 'react-router-dom';
import { useEffect, useState } from 'react';
import { Navigate } from 'react-router-dom';
import CircularProgress from '@mui/material/CircularProgress';
import Backdrop from '@mui/material/Backdrop';
import { useAuth } from './context/authContext';

const ProtectedRoute = ({ children }: { children: JSX.Element }) => {
const { isAuthenticated } = useAuth();
const [loading, setLoading] = useState(true);
interface ProtectedRouteProps {
children: JSX.Element;
}

const ProtectedRoute = ({ children }: ProtectedRouteProps) => {
const [loading, setLoading] = useState<boolean>(true);
const [isAuthenticated, setIsAuthenticated] = useState<boolean>(false);

useEffect(() => {
const checkAuthStatus = async () => {
setLoading(true);
setTimeout(() => {
setLoading(false);
}, 1000);
const token = localStorage.getItem('OCI_TOKEN');
if (token) {
setIsAuthenticated(true);
}
setLoading(false);
};

checkAuthStatus();
}, [isAuthenticated]);
}, []);

if (loading) {
return (
Expand Down
7 changes: 7 additions & 0 deletions src/constants.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
declare module '../../../contracts/chains/constants' {
export const SUPPORTED_CHAINS: {
chainId: number;
easContractAbi: unknown[];
easContractAddress: string;
}[];
}

0 comments on commit 55c81d9

Please sign in to comment.