-
Notifications
You must be signed in to change notification settings - Fork 1
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 #17 from nhattpn/nhat
update token and logout
- Loading branch information
Showing
11 changed files
with
83 additions
and
64 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,18 @@ | ||
import { useEffect } from 'react'; | ||
import { Outlet, Navigate, useNavigate } from 'react-router-dom' | ||
|
||
const PrivateRoutes = () => { | ||
const navigate = useNavigate(); | ||
const redirectUrl = '/'; | ||
const token = sessionStorage.getItem('jwtToken'); | ||
useEffect(() => { | ||
if(!token){ | ||
navigate(redirectUrl, {replace: true }); | ||
} | ||
}, []); | ||
return( | ||
<Outlet/> | ||
) | ||
} | ||
|
||
export default PrivateRoutes |
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,11 @@ | ||
export const LogOut = async () => { | ||
try{ | ||
sessionStorage.removeItem('jwtToken'); | ||
await fetch('/api/logout', { method: 'POST' }); | ||
console.log('Logged out successfully (backend)'); | ||
} catch (error) { | ||
console.error('Error logging out (backend):', error); | ||
} finally { | ||
window.location.href = '/'; | ||
} | ||
}; |
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,20 +1,6 @@ | ||
import { useContext } from 'react'; | ||
import { AuthContext } from '../context/authContext'; | ||
import { useEffect, useNavigate } from 'react'; | ||
|
||
export function useAuth() { | ||
return useContext(AuthContext); | ||
} | ||
export function useRequireAuth(redirectUrl = '/login') { | ||
const auth = useAuth(); | ||
const navigate = useNavigate(); | ||
|
||
useEffect(() => { | ||
const token = sessionStorage.getItem('jwtToken'); | ||
if (!token) { | ||
navigate(redirectUrl); | ||
} | ||
}, [navigate, redirectUrl]); | ||
|
||
return auth; | ||
} |
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
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