Skip to content

Commit f90345d

Browse files
committed
Add response interceptor and redirect user to login on 401 response
1 parent 67bfca5 commit f90345d

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

vue/src/axios.js

+12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44
import axios from "axios";
55
import store from "./store";
6+
import {useRouter} from 'vue-router'
67

78
const axiosClient = axios.create({
89
baseURL: `${import.meta.env.VITE_API_BASE_URL}/api`
@@ -13,4 +14,15 @@ axiosClient.interceptors.request.use(config => {
1314
return config;
1415
})
1516

17+
axiosClient.interceptors.response.use(response => {
18+
return response;
19+
}, error => {
20+
if (error.response.status === 401) {
21+
const router = useRouter();
22+
sessionStorage.removeItem('TOKEN')
23+
router.push({name: 'Login'})
24+
}
25+
return error;
26+
})
27+
1628
export default axiosClient;

0 commit comments

Comments
 (0)