Skip to content

Commit

Permalink
Merge pull request #28 from VSchool/protectedRouteComponent
Browse files Browse the repository at this point in the history
Protected route component
  • Loading branch information
tirzah-dev authored Mar 25, 2024
2 parents 069a782 + 8c3ab04 commit feb61fa
Show file tree
Hide file tree
Showing 28 changed files with 267 additions and 1,832 deletions.
91 changes: 6 additions & 85 deletions apps/frontend/src/app/app.tsx
Original file line number Diff line number Diff line change
@@ -1,94 +1,15 @@
import React, { useEffect, useState } from 'react';
import { BrowserRouter } from 'react-router-dom';
import Footer from './components/footer/footer';
import Header from './components/header/header';
import Router from './views/router/router';
import { AssetProvider } from './views/assets/AssetContext';
import './app.module.scss';
// import { UpdateAssets } from './views/assets/UpdateAssets';
import { AssetsInput } from './views/assets/AssetsInput';

// eslint-disable-next-line @typescript-eslint/no-empty-function, @typescript-eslint/no-explicit-any
export const UserContext = React.createContext({
user: {
firstName: null,
lastName: null,
id: null,
joinDate: null,
email: null,
userType: 'Reader',
picture: null,
name: null,
roles: ['None'],
},
setUser: (user: any) => {},
});


import Router from './router';


export function App() {
const [user, setUser] = React.useState<any>({
firstName: null,
lastName: null,
id: null,
joinDate: null,
email: null,
userType: 'Reader',
picture: null,
name: null,
roles: ['None'],
});

useEffect(() => {
const user = localStorage.getItem('user');
if (user) {
setUser(JSON.parse(user));
}
}, []);

useEffect(() => {
console.log(user);
}, [user]);

// this is for cypress testing, to have a test user logged in
useEffect(() => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if (window.Cypress) {
const user = {
firstName: 'Test',
lastName: 'User',
id: 'test',
joinDate: '05/05/2023',
email: 'testUser@test.com',
userType: 'Reader',
picture: 'www.google.com',
name: 'Test User',
roles: ['None'],
};
setUser(user);
}
}, []);

return (
// <UserContext.Provider value={{ user, setUser }}>
// {/* <AssetProvider value={{assets, setAssets, saveAssets, removeAsset, getAllAssets, addNewAsset }} > */}
// {/* <UpdateAssets /> */}

<BrowserRouter>
<div className="flex flex-col h-screen">
<div style={{ flex: '1 1 0' }}>
<Header></Header>
<Router></Router>

</div>

</div>
</BrowserRouter>
// {/* </AssetProvider> */}
// </UserContext.Provider>
<BrowserRouter>
<Header/>
<Router/>
</BrowserRouter>
);
}

export default App;
export default App;
41 changes: 41 additions & 0 deletions apps/frontend/src/app/components/ProtectedRoute.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React, { ReactNode, useState, useEffect } from 'react';
import { getAuth, onAuthStateChanged } from 'firebase/auth';
import { useNavigate } from 'react-router-dom';

interface Props {
children: ReactNode;
}

const ProtectedRoute: React.FC<Props> = ({ children }) => {
const auth = getAuth();
const navigate = useNavigate();
const [loading, setLoading] = useState(true);

useEffect(() => {
const unsubscribe = AuthCheck();
return () => unsubscribe();
}, []);

const AuthCheck = () => {
return onAuthStateChanged(auth, (user) => {
if (user) {
setLoading(false);
} else {
navigate('/home');
}
});
};

if (loading) return <p>...loading</p>

return <>{children}</>
};

export default ProtectedRoute;







32 changes: 0 additions & 32 deletions apps/frontend/src/app/components/header/Logo.tsx

This file was deleted.

79 changes: 9 additions & 70 deletions apps/frontend/src/app/components/header/header.css
Original file line number Diff line number Diff line change
Expand Up @@ -47,34 +47,6 @@
flex-grow: 1;
}


.trademark-image,
.trademark-letter {
font-size: large;
color: white;
}

.trademark-letter {
margin-top: 0.25rem;
padding-left: 1rem;
padding-right: 1rem;
}

.vertical-line {
position: relative;
display: flex;
}

.vertical-line::before {
content: ' ';
position: absolute;
top: 0.5rem;
bottom: 0.25rem;
left: 0.02rem;
width: 1px;
background-color: rgb(255, 255, 255);
}

.head-trademark-lineList {
margin-top: 0.40rem;
width: 1.25rem;
Expand All @@ -85,7 +57,6 @@
justify-content: space-around;
}


.head-trademark-container {
padding-top: 25%;
padding-bottom: 15%;
Expand All @@ -97,60 +68,30 @@
height: 10rem;
}

.head-trademark-letter {
font-size: 1.5rem;
color: white;
padding: 0;
margin: 0;
bottom: 0;
}

.head-trademark-symbol {
width: 1.55rem;
height: 1.55rem;
padding: 0;
margin: 0;
}

.head-vertical-line {
position: relative;
display: flex;
}

.head-vertical-line::before {
content: '';
position: absolute;
top: 0.35rem;
bottom: 0.25rem;
left: 0.02rem;
width: 1px;
background-color: rgb(255, 255, 255);
}

.head-crystalball {
width: 1.95rem;
height: 1.95rem;
padding: 0;
margin: 0;
padding-left: 0.5rem;
}

.unordered-list {
justify-content: space-around;
align-items: center;
width: 100%;
display: flex;
flex-direction: row;
margin-right: auto;
padding: 0;
}

.li-navlink {
.li-nav-logo {
color: white;
font-weight: bold;
margin-bottom: 0;
padding-right: 0.5rem;
}

.li-navlink {
font-size: x-large;
color: white;
font-weight: bold;
padding-right: 0.5rem;
}

.li-navlink:hover {
text-decoration: underline;
}
Expand All @@ -176,8 +117,6 @@
text-decoration: underline;
}



.header-profile-image {
height: 2rem;
width: 2rem;
Expand Down
Loading

0 comments on commit feb61fa

Please sign in to comment.