-
Notifications
You must be signed in to change notification settings - Fork 349
/
http.ts
146 lines (139 loc) 路 4.84 KB
/
http.ts
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2023 Zinc Labs Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
import store from "../stores";
import type { AxiosInstance } from "axios";
import axios from "axios";
import config from "../aws-exports";
import { Notify } from "quasar";
const http = ({ headers } = {} as any) => {
let instance: AxiosInstance;
headers = {
...headers,
};
instance = axios.create({
// timeout: 10000,
withCredentials: true,
baseURL: store.state.API_ENDPOINT,
headers,
});
instance.interceptors.response.use(
function (response) {
return response;
},
function (error) {
if (error && error.response && error.response.status) {
switch (error.response.status) {
case 400:
console.log(
JSON.stringify(error.response.data["error"] || "Bad Request")
);
break;
case 401:
console.log(
JSON.stringify(
error.response.data["error"] || "Invalid credentials"
)
);
if (
config.isCloud == "true" &&
!error.request.responseURL.includes("/auth/login")
) {
store.dispatch("logout");
localStorage.clear();
sessionStorage.clear();
window.location.reload();
}
// Check if the failing request is not the login or refresh token request
else if (
config.isEnterprise == "true" &&
(store.state as any).zoConfig.sso_enabled &&
!error.config.url.includes("/config/dex_login") &&
!error.config.url.includes("/config/dex_refresh") &&
!error.config.url.includes("/auth/login")
) {
// Modify the request to include the refresh token
return instance
.get("/config/dex_refresh", {
//headers: { Authorization: `${refreshToken}` },
})
.then((res) => {
if (res.status === 200) {
// Token refreshed successfully, retry the original request
return instance.request(error.config);
}
})
.catch((refreshError) => {
instance.get("/config/logout", {}).then((res) => {
store.dispatch("logout");
localStorage.clear();
sessionStorage.clear();
window.location.reload();
return Promise.reject(refreshError);
});
});
} else {
if (!error.request.responseURL.includes("/login")) {
store.dispatch("logout");
localStorage.clear();
sessionStorage.clear();
window.location.reload();
}
}
break;
case 403:
if (config.isEnterprise == "true" || config.isCloud == "true") {
Notify.create({
message:
"Unauthorized Access: You are not authorized to perform this operation, please contact your administrator.",
timeout: 0, // This ensures the notification does not close automatically
color: "negative", // Customize color as needed
position: "top",
actions: [
{
color: "white",
icon: "close",
size: "sm",
},
],
});
}
console.log(
JSON.stringify(
error.response.data["error"] || "Unauthorized Access"
)
);
break;
case 404:
console.log(
JSON.stringify(error.response.data["error"] || "Not Found")
);
break;
case 500:
console.log(
JSON.stringify(
error.response.data["error"] || "Invalid ServerError"
)
);
break;
default:
// noop
}
}
return Promise.reject(error);
}
);
return instance;
};
export default http;