forked from GreenPioneer/nx_firebase_template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from VSchool/protectedRouteComponent
Protected route component
- Loading branch information
Showing
28 changed files
with
267 additions
and
1,832 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.