aboutsummaryrefslogtreecommitdiff
path: root/src/main.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.ts')
-rw-r--r--src/main.ts43
1 files changed, 42 insertions, 1 deletions
diff --git a/src/main.ts b/src/main.ts
index f6d3ea7..2886300 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -9,9 +9,50 @@ import * as bootstrap from 'bootstrap'
import 'vue-toast-notification/dist/theme-sugar.css'
axios.defaults.baseURL = '/api'
-axios.defaults.headers.common['Authorization'] = `Bearer ${localStorage.getItem('authToken')}`
axios.defaults.headers.post['Content-Type'] = 'application/json'
+axios.interceptors.request.use(
+ (config) => {
+ const token = localStorage.getItem('authToken')
+ if (token) {
+ config.headers['Authorization'] = 'Bearer ' + token
+ }
+ return config
+ },
+ (error) => {
+ return Promise.reject(error)
+ }
+)
+
+axios.interceptors.response.use(
+ (res) => {
+ return res;
+ },
+ async (err) => {
+ const originalConfig = err.config;
+
+ if (originalConfig.url !== "/auth/signin" && err.response) {
+ if (err.response.status === 401 && !originalConfig._retry) {
+ originalConfig._retry = true;
+
+ try {
+ const rs = await axios.post("/auth/refresh", {
+ RefreshToken: localStorage.getItem("refToken")
+ });
+
+ localStorage.setItem("authToken", rs.data.auth_token)
+
+ return axios(originalConfig);
+ } catch (_error) {
+ return Promise.reject(_error);
+ }
+ }
+ }
+
+ return Promise.reject(err);
+ }
+);
+
const app = createApp(App)
app.use(router)