setting pilot page behind protected route (#44)
This commit was merged in pull request #44.
This commit is contained in:
@@ -1,56 +1,32 @@
|
|||||||
import { useEffect } from 'react';
|
import { Navigate, Route, Routes } from 'react-router-dom';
|
||||||
import { Route, Routes } from 'react-router-dom';
|
|
||||||
import Pilots from './components/pilots/Pilots';
|
import Pilots from './components/pilots/Pilots';
|
||||||
import Logbook from './components/logbook/Logbook';
|
import Logbook from './components/logbook/Logbook';
|
||||||
import { useAppContext } from './hooks/appContext/UseAppContext';
|
|
||||||
import { AxiosInstance, AxiosResponse } from 'axios';
|
|
||||||
import { useHttpClient } from './hooks/httpClient/UseHttpClient';
|
|
||||||
import { useFeatureFlag } from './hooks/featureFlag/UseFeatureFlag';
|
|
||||||
import SiteNav from './components/siteNav/SiteNav';
|
import SiteNav from './components/siteNav/SiteNav';
|
||||||
import { IPublicClientApplication } from '@azure/msal-browser';
|
import { useIsAuthenticated } from '@azure/msal-react';
|
||||||
import { MsalProvider } from '@azure/msal-react';
|
|
||||||
|
|
||||||
interface AppProps {
|
interface ProtectedRouteProps {
|
||||||
pca: IPublicClientApplication
|
children: React.ReactNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
const App: React.FC<AppProps> = ({ pca }: AppProps) => {
|
const App = () => {
|
||||||
const httpClient: AxiosInstance = useHttpClient();
|
const isAuthenticated = useIsAuthenticated()
|
||||||
const appContext = useAppContext();
|
|
||||||
|
|
||||||
// useEffect(() => {
|
const ProtectedRoute = ({ children }: ProtectedRouteProps) => {
|
||||||
// const getFeatureFlags = async () => {
|
return isAuthenticated ? children : <Navigate to='/' />
|
||||||
// try {
|
}
|
||||||
// const featureFlagKeys: string = 'flying-pilots';
|
|
||||||
// const response: AxiosResponse = await httpClient.get(
|
|
||||||
// `api/featureFlags?keys=${featureFlagKeys}&label=${import.meta.env.MODE}`
|
|
||||||
// );
|
|
||||||
// const featureFlags: { key: string; enabled: boolean }[] = response.data;
|
|
||||||
|
|
||||||
// if (featureFlags.length > 0) {
|
|
||||||
// appContext.dispatch({
|
|
||||||
// type: 'SET_FEATURE_FLAGS',
|
|
||||||
// payload: featureFlags
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
// } catch (error) {
|
|
||||||
// console.log(error);
|
|
||||||
// }
|
|
||||||
// };
|
|
||||||
|
|
||||||
// getFeatureFlags();
|
|
||||||
// }, []);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<MsalProvider instance={pca}>
|
<>
|
||||||
<SiteNav />
|
<SiteNav />
|
||||||
<Routes>
|
<Routes>
|
||||||
{/* {useFeatureFlag('flying-pilots')?.enabled && ( */}
|
<Route path="/pilots" element={
|
||||||
<Route path="/pilots" element={<Pilots />} />
|
<ProtectedRoute>
|
||||||
{/* )} */}
|
<Pilots />
|
||||||
|
</ProtectedRoute>
|
||||||
|
} />
|
||||||
<Route path="/" element={<Logbook />} />
|
<Route path="/" element={<Logbook />} />
|
||||||
</Routes>
|
</Routes>
|
||||||
</MsalProvider>
|
</>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ const Logbook: React.FC<unknown> = () => {
|
|||||||
dispatch({ type: 'SET_IS_LOADING', payload: true });
|
dispatch({ type: 'SET_IS_LOADING', payload: true });
|
||||||
|
|
||||||
const response: AxiosResponse = await httpClient.get(`api/logs`);
|
const response: AxiosResponse = await httpClient.get(`api/logs`);
|
||||||
console.log(response)
|
|
||||||
if (response.data.length > 0) {
|
if (response.data.length > 0) {
|
||||||
dispatch({ type: 'SET_ENTRIES', payload: response.data });
|
dispatch({ type: 'SET_ENTRIES', payload: response.data });
|
||||||
|
|
||||||
|
|||||||
@@ -27,22 +27,30 @@ type EventPayloadExtended = EventPayload & { accessToken: string };
|
|||||||
const SiteNav: React.FC<unknown> = () => {
|
const SiteNav: React.FC<unknown> = () => {
|
||||||
const [loading, setLoading] = useState<boolean>(false);
|
const [loading, setLoading] = useState<boolean>(false);
|
||||||
const [userPhoto, setUserPhoto] = useState<string>();
|
const [userPhoto, setUserPhoto] = useState<string>();
|
||||||
|
const [pages, setPages] = useState<{ name: string; url: string; }[]>([]);
|
||||||
const httpClient: AxiosInstance = useHttpClient();
|
const httpClient: AxiosInstance = useHttpClient();
|
||||||
const appContext = useAppContext();
|
const appContext = useAppContext();
|
||||||
const isAuthenticated = useIsAuthenticated();
|
const isAuthenticated = useIsAuthenticated();
|
||||||
const { getAccessToken } = useAccessToken();
|
const { getAccessToken } = useAccessToken();
|
||||||
const { inProgress, instance } = useMsal();
|
const { inProgress, instance } = useMsal();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const pages = [
|
const getPages = () => {
|
||||||
{
|
const pages = [
|
||||||
name: 'Logbook',
|
{
|
||||||
url: '/'
|
name: 'Logbook',
|
||||||
},
|
url: '/'
|
||||||
{
|
}
|
||||||
name: 'Pilots',
|
];
|
||||||
url: '/pilots'
|
|
||||||
|
if (isAuthenticated) {
|
||||||
|
pages.push({
|
||||||
|
name: 'Pilots',
|
||||||
|
url: '/pilots'
|
||||||
|
})
|
||||||
}
|
}
|
||||||
];
|
|
||||||
|
setPages(pages)
|
||||||
|
};
|
||||||
const handleSignIn = () => {
|
const handleSignIn = () => {
|
||||||
instance.loginRedirect({
|
instance.loginRedirect({
|
||||||
scopes: [`api://${import.meta.env.VITE_CLIENT_ID}/user_impersonation`]
|
scopes: [`api://${import.meta.env.VITE_CLIENT_ID}/user_impersonation`]
|
||||||
@@ -125,9 +133,14 @@ const SiteNav: React.FC<unknown> = () => {
|
|||||||
Object.keys(appContext.state.userProfile).length === 0
|
Object.keys(appContext.state.userProfile).length === 0
|
||||||
) {
|
) {
|
||||||
setUserProfile();
|
setUserProfile();
|
||||||
|
getPages();
|
||||||
}
|
}
|
||||||
}, [isAuthenticated]);
|
}, [isAuthenticated]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
getPages();
|
||||||
|
}, [])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Navbar
|
<Navbar
|
||||||
handlePageClick={handlePageClick}
|
handlePageClick={handlePageClick}
|
||||||
|
|||||||
@@ -28,11 +28,13 @@ msalInstance.initialize().then(() => {
|
|||||||
|
|
||||||
ReactDOM.createRoot(document.getElementById('root')!).render(
|
ReactDOM.createRoot(document.getElementById('root')!).render(
|
||||||
<React.StrictMode>
|
<React.StrictMode>
|
||||||
<AppContextProvider>
|
<MsalProvider instance={msalInstance}>
|
||||||
<BrowserRouter>
|
<AppContextProvider>
|
||||||
<App pca={msalInstance} />
|
<BrowserRouter>
|
||||||
</BrowserRouter>
|
<App />
|
||||||
</AppContextProvider>
|
</BrowserRouter>
|
||||||
|
</AppContextProvider>
|
||||||
|
</MsalProvider>
|
||||||
</React.StrictMode>
|
</React.StrictMode>
|
||||||
);
|
);
|
||||||
})
|
})
|
||||||
Reference in New Issue
Block a user