-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
protected route fix when refreshing page
- Loading branch information
1 parent
e9d3304
commit 84c12f6
Showing
3 changed files
with
20 additions
and
21 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,16 +1,10 @@ | ||
import React, { useContext } from 'react' | ||
import { Navigate } from 'react-router-dom'; | ||
import useIsAuthenticated from '../utils/useIsAuthenticated' | ||
import React, { useContext } from "react"; | ||
import { Navigate } from "react-router-dom"; | ||
import { AuthContext } from "../context/AuthContext"; | ||
function ProtectedRoute({ children }) { | ||
const { user, loading } = useContext(AuthContext); | ||
|
||
function ProtectedRoute({children}) { | ||
|
||
const isAuthenticated = useIsAuthenticated(); | ||
|
||
return ( | ||
<> | ||
{isAuthenticated ? children : <Navigate to="/login"/>} | ||
</> | ||
) | ||
return loading ? <p>Loading</p> : user ? children : <Navigate to="/login" />; | ||
} | ||
|
||
export default ProtectedRoute | ||
export default ProtectedRoute; |
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
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,11 +1,12 @@ | ||
import React, { useContext } from 'react' | ||
import { AuthContext } from '../context/AuthContext' | ||
|
||
import React, { useContext } from "react"; | ||
import { AuthContext } from "../context/AuthContext"; | ||
// ! NOT IN USE | ||
function useIsAuthenticated() { | ||
const { user } = useContext(AuthContext); | ||
const { user } = useContext(AuthContext); | ||
|
||
const isAuthenticated = user !== null ? true : false; | ||
|
||
const isAuthenticated = user !== null ? true : false; | ||
return isAuthenticated; | ||
} | ||
|
||
export default useIsAuthenticated | ||
export default useIsAuthenticated; |