generated from nyu-software-engineering/generic-mern-stack-project
-
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.
- Loading branch information
1 parent
c793125
commit de58201
Showing
5 changed files
with
98 additions
and
74 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,13 +1,16 @@ | ||
// ProtectedRoute.js | ||
import React from 'react'; | ||
import { Navigate } from 'react-router-dom'; | ||
import { Navigate, useLocation } from 'react-router-dom'; | ||
|
||
const ProtectedRoute = ({ children, loggedInUser }) => { | ||
if (!loggedInUser) { | ||
return <Navigate to="/login" replace />; | ||
const location = useLocation(); | ||
const token = localStorage.getItem('token'); | ||
|
||
if (!loggedInUser || !token) { | ||
return <Navigate to="/login" state={{ from: location }} replace />; | ||
} | ||
|
||
return children; | ||
}; | ||
|
||
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
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