mirror of
https://github.com/bubblecup-12/VogelSocialMedia.git
synced 2025-07-08 12:18:46 +00:00
tokens refresh when jwt is expired and added basic axios config
This commit is contained in:
parent
c48498af95
commit
fbf645ba0f
15 changed files with 470 additions and 289 deletions
28
code/frontend/src/api/refreshToken.ts
Normal file
28
code/frontend/src/api/refreshToken.ts
Normal 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);
|
||||
}
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue