mirror of
https://github.com/stonith404/pingvin-share.git
synced 2024-11-05 07:20:13 +01:00
fix: reduce refresh access token calls
This commit is contained in:
parent
4dae7e250a
commit
1aa3d8e5e8
@ -50,7 +50,12 @@ function App({ Component, pageProps }: AppProps) {
|
||||
}, [router.pathname]);
|
||||
|
||||
useEffect(() => {
|
||||
setInterval(async () => await authService.refreshAccessToken(), 30 * 1000);
|
||||
const interval = setInterval(
|
||||
async () => await authService.refreshAccessToken(),
|
||||
2 * 60 * 1000, // 2 minutes
|
||||
);
|
||||
|
||||
return () => clearInterval(interval);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
|
@ -36,8 +36,10 @@ const signOut = async () => {
|
||||
const refreshAccessToken = async () => {
|
||||
try {
|
||||
const accessToken = getCookie("access_token") as string;
|
||||
|
||||
// If the access token expires in less than 2 minutes refresh it
|
||||
if (
|
||||
!accessToken ||
|
||||
accessToken &&
|
||||
(jose.decodeJwt(accessToken).exp ?? 0) * 1000 < Date.now() + 2 * 60 * 1000
|
||||
) {
|
||||
await api.post("/auth/token");
|
||||
|
Loading…
Reference in New Issue
Block a user