20 lines
597 B
JavaScript
20 lines
597 B
JavaScript
import { useEffect, useRef } from 'react';
|
|
|
|
export const useJwtRefresh = (authToken, setAuthToken) => {
|
|
const refreshTimer = useRef(null);
|
|
useEffect(() => {
|
|
clearTimeout(refreshTimer.current);
|
|
if (authToken) {
|
|
refreshTimer.current = setTimeout(async () => {
|
|
const result = await fetch('/refresh_token').then((res) => res.json());
|
|
if (result.token) {
|
|
setAuthToken(result.token);
|
|
} else {
|
|
setAuthToken(null);
|
|
}
|
|
}, 60000 * 0.5); // 10 minutes
|
|
}
|
|
return () => clearTimeout(refreshTimer.current);
|
|
}, [authToken]);
|
|
};
|