-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
8 changed files
with
78 additions
and
47 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
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,36 +1,44 @@ | ||
import React, { useEffect } from 'react'; | ||
import { useNavigate } from 'react-router-dom'; | ||
import { isAuthenticated } from '../services/authService'; | ||
// components/AuthWrapper.tsx | ||
|
||
import React, { useEffect, useState } from 'react'; | ||
import { useNavigate, useLocation } from 'react-router-dom'; | ||
import { isAuthenticated, getUserRole } from '../services/authService'; | ||
|
||
interface AuthWrapperProps { | ||
children: React.ReactNode; | ||
} | ||
|
||
const AuthWrapper: React.FC<AuthWrapperProps> = ({ children }) => { | ||
const navigate = useNavigate(); | ||
const location = useLocation(); | ||
const [loading, setLoading] = useState<boolean>(true); | ||
|
||
useEffect(() => { | ||
const checkAuthStatus = () => { | ||
const checkAuthStatus = async () => { | ||
if (isAuthenticated()) { | ||
if (window.location.pathname === '/login') { | ||
navigate('/dashboard', { replace: true }); | ||
const role = await getUserRole(); | ||
if (role === 3) { | ||
// User has the Researcher role | ||
setLoading(false); // Allow rendering of protected components | ||
} else { | ||
// User does not have the Researcher role | ||
navigate('/unauthorized', { replace: true }); | ||
} | ||
} else { | ||
if (window.location.pathname !== '/login') { | ||
navigate('/login', { replace: true }); | ||
} | ||
// User is not authenticated | ||
navigate('/login', { replace: true }); | ||
} | ||
}; | ||
|
||
checkAuthStatus(); | ||
}, [navigate, location]); | ||
|
||
// Set an interval to keep checking if the user is authenticated | ||
const intervalId = setInterval(checkAuthStatus, 5000); // Check every 5 seconds | ||
|
||
return () => clearInterval(intervalId); | ||
}, [navigate]); | ||
if (loading) { | ||
// You can show a loading indicator while checking authentication | ||
return <div>Loading...</div>; | ||
} | ||
|
||
return <>{children}</>; | ||
}; | ||
|
||
export default AuthWrapper; | ||
export default AuthWrapper; |
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 |
---|---|---|
|
@@ -8,8 +8,8 @@ const Unauthorized: React.FC = () => { | |
It seems like you don't have the <strong>Researcher</strong> role right now. | ||
</p> | ||
<p> | ||
If you believe this is a mistake or you should have the necessary role, please contact | ||
<a href="mailto:[email protected]" > [email protected]</a> for further assistance. | ||
If you believe this is a mistake or you should have the necessary role, | ||
please contact the organization | ||
</p> | ||
</div> | ||
); | ||
|
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