-
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.
Merge pull request #33 from UtrechtUniversity/dev
V1.1 Alpha ResearchConnect
- Loading branch information
Showing
20 changed files
with
450 additions
and
270 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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
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
Oops, something went wrong.