Repository navigation
Expand file tree
/
Copy pathclient.js
More file actions
88 lines (76 loc) · 2.9 KB
/
Copy pathclient.js
File metadata and controls
88 lines (76 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import axios from "axios";
import toast from "react-hot-toast";
import { API } from "./endpoints";
const client = axios.create({
baseURL: import.meta.env.VITE_API_URL,
withCredentials: true,
timeout: 10000,
});
client.interceptors.request.use((config) => {
const isAdminRequest = config.adminRequest || config.url?.includes("/admin");
const token = localStorage.getItem(isAdminRequest ? "adminToken" : "token");
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
});
const isAdminRequest = (config) => !!(config?.adminRequest || config?.url?.includes("/admin"));
const storageKey = (admin) => (admin ? "adminToken" : "token");
const refreshUrl = (admin) => (admin ? `${API.ADMIN_AUTH}/refresh-token` : `${API.AUTH}/refresh-token`);
const isAuthEndpoint = (url) =>
!!url && ["/refresh-token", "/login", "/google", "/logout"].some((p) => url.includes(p));
// Independent in-flight state per surface — a tab can hold both a student and
// an admin session at once, so a single global flag would wrongly queue one
// surface's 401 behind the other's refresh call.
const refreshState = {
student: { inFlight: null },
admin: { inFlight: null },
};
const doRefresh = async (admin) => {
const res = await axios.post(
`${client.defaults.baseURL}${refreshUrl(admin)}`,
{},
{ withCredentials: true }
);
const newToken = res.data.token;
localStorage.setItem(storageKey(admin), newToken);
return newToken;
};
client.interceptors.response.use(
(response) => response,
async (error) => {
const { config, response } = error;
const admin = isAdminRequest(config || {});
// Hackathon actions are blocked until every Edit Profile field is filled
// — send people to the form instead of leaving them on a dead-end error.
if (response?.status === 403 && response.data?.code === "PROFILE_INCOMPLETE") {
toast.error(response.data.message || "Complete your profile to take part in hackathons", {
id: "profile-incomplete",
});
if (!window.location.pathname.startsWith("/dashboard")) {
setTimeout(() => window.location.assign("/dashboard?completeProfile=1"), 1400);
}
return Promise.reject(error);
}
if (!response || response.status !== 401 || !config || config._retry || isAuthEndpoint(config.url)) {
if (response?.status === 401) localStorage.removeItem(storageKey(admin));
return Promise.reject(error);
}
config._retry = true;
const state = refreshState[admin ? "admin" : "student"];
if (!state.inFlight) {
state.inFlight = doRefresh(admin).finally(() => {
state.inFlight = null;
});
}
try {
const newToken = await state.inFlight;
config.headers.Authorization = `Bearer ${newToken}`;
return client(config);
} catch {
localStorage.removeItem(storageKey(admin));
return Promise.reject(error);
}
}
);
export default client;