Skip to content

Commit

Permalink
Create App.tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Aug 9, 2024
1 parent 4589f27 commit e978a11
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions projects/pi-nexus-iam/containers/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import React, { useState, useEffect, useContext } from 'react';
import { BrowserRouter, Route, Switch, Redirect } from 'react-router-dom';
import { ThemeProvider } from 'styled-components';
import { ToastContainer } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
import { AuthContext } from '../contexts/AuthContext';
import { theme } from '../styles/theme';
import { GlobalStyle } from '../styles/global';
import Login from '../components/Login';
import Dashboard from '../components/Dashboard';
import NotFound from '../components/NotFound';

const App: React.FC = () => {
const { accessToken, refreshToken, logout } = useContext(AuthContext);
const [isAuthenticated, setIsAuthenticated] = useState(false);

useEffect(() => {
if (accessToken && refreshToken) {
setIsAuthenticated(true);
} else {
setIsAuthenticated(false);
}
}, [accessToken, refreshToken]);

const handleLogout = () => {
logout();
setIsAuthenticated(false);
};

return (
<ThemeProvider theme={theme}>
<GlobalStyle />
<ToastContainer />
<BrowserRouter>
<Switch>
<Route path="/login" exact>
{isAuthenticated ? <Redirect to="/dashboard" /> : <Login />}
</Route>
<Route path="/dashboard" exact>
{isAuthenticated ? <Dashboard /> : <Redirect to="/login" />}
</Route>
<Route path="*">
<NotFound />
</Route>
</Switch>
</BrowserRouter>
</ThemeProvider>
);
};

export default App;

0 comments on commit e978a11

Please sign in to comment.