-
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
1 parent
20a8db3
commit 7cfc293
Showing
6 changed files
with
219 additions
and
29 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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Navigate } from 'react-router-dom'; | ||
import { useEffect, useState } from 'react'; | ||
import CircularProgress from '@mui/material/CircularProgress'; | ||
import Backdrop from '@mui/material/Backdrop'; | ||
import { useAuth } from './context/authContext'; | ||
|
||
const ProtectedRoute = ({ children }: { children: JSX.Element }) => { | ||
const { isAuthenticated } = useAuth(); | ||
const [loading, setLoading] = useState(true); | ||
|
||
useEffect(() => { | ||
const checkAuthStatus = async () => { | ||
setLoading(true); | ||
setTimeout(() => { | ||
setLoading(false); | ||
}, 1000); | ||
}; | ||
|
||
checkAuthStatus(); | ||
}, [isAuthenticated]); | ||
|
||
if (loading) { | ||
return ( | ||
<Backdrop open={loading} style={{ zIndex: 1201 }}> | ||
<CircularProgress color="inherit" /> | ||
</Backdrop> | ||
); | ||
} | ||
|
||
if (!isAuthenticated) { | ||
return <Navigate to="/auth/login" replace />; | ||
} | ||
|
||
return children; | ||
}; | ||
|
||
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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { | ||
createContext, | ||
useState, | ||
useContext, | ||
ReactNode, | ||
useEffect, | ||
useMemo, | ||
} from 'react'; | ||
|
||
interface AuthContextType { | ||
isAuthenticated: boolean; | ||
jwt: string | null; | ||
setAuthInfo: (jwt: string) => void; | ||
signOut: () => void; | ||
} | ||
|
||
const AuthContext = createContext<AuthContextType | undefined>(undefined); | ||
|
||
export const AuthProvider = ({ children }: { children: ReactNode }) => { | ||
const [isAuthenticated, setIsAuthenticated] = useState(false); | ||
const [jwt, setJwt] = useState<string | null>(null); | ||
|
||
useEffect(() => { | ||
const token = localStorage.getItem('OCI_TOKEN'); | ||
if (token) { | ||
setJwt(token); | ||
setIsAuthenticated(true); | ||
} | ||
}, []); | ||
|
||
const setAuthInfo = (token: string) => { | ||
setJwt(token); | ||
setIsAuthenticated(true); | ||
localStorage.setItem('OCI_TOKEN', token); | ||
}; | ||
|
||
const signOut = () => { | ||
setJwt(null); | ||
setIsAuthenticated(false); | ||
localStorage.removeItem('OCI_TOKEN'); | ||
}; | ||
|
||
const value = useMemo( | ||
() => ({ | ||
isAuthenticated, | ||
jwt, | ||
setAuthInfo, | ||
signOut, | ||
}), | ||
[isAuthenticated, jwt] | ||
); | ||
|
||
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>; | ||
}; | ||
|
||
export const useAuth = (): AuthContextType => { | ||
const context = useContext(AuthContext); | ||
if (context === undefined) { | ||
throw new Error('useAuth must be used within an AuthProvider'); | ||
} | ||
return context; | ||
}; |
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