tokens refresh when jwt is expired and added basic axios config

This commit is contained in:
Kai Ritthaler 2025-06-25 07:56:30 +02:00 committed by Luisa Bellitto
parent c48498af95
commit fbf645ba0f
15 changed files with 470 additions and 289 deletions

View file

@ -0,0 +1,28 @@
import axios from "axios";
const getRefreshToken = () => localStorage.getItem("refreshToken");
export const refreshToken = async () => {
const token = getRefreshToken();
if (!token) {
throw new Error("No refresh token available");
}
const response = await axios.get(
"http://localhost:3001/api/user/refreshToken",
{
headers: {
"Refresh-Token": getRefreshToken(),
},
withCredentials: true,
}
);
const authHeader = response.headers["authorization"];
if (authHeader && authHeader.startsWith("Bearer ")) {
const token = authHeader.substring(7);
localStorage.setItem("token", token);
}
const refreshToken = response.headers["refresh-token"];
if (refreshToken) {
localStorage.setItem("refreshToken", refreshToken);
}
};