Added router push if received 401 on client side

This commit is contained in:
Donald Zou 2025-08-11 16:32:21 +08:00
parent 5c58f548c0
commit 9424ad1f13

View File

@ -1,16 +1,23 @@
import axios from "axios";
import {useRouter} from "vue-router";
export const requestURl = (url) => {
return import.meta.env.MODE === 'development' ? '/client' + url
: `${window.location.protocol}//${(window.location.host + window.location.pathname + url).replace(/\/\//g, '/')}`
}
const router = useRouter()
export const axiosPost = async (URL, body = {}) => {
try{
const res = await axios.post(requestURl(URL), body)
return res.data
} catch (error){
console.log(error)
if (error.status === 401){
await router.push('/signin')
}
return undefined
}
}
@ -21,6 +28,9 @@ export const axiosGet = async (URL, query = {}) => {
return res.data
} catch (error){
console.log(error)
if (error.status === 401){
await router.push('/signin')
}
return undefined
}
}