api config
This commit is contained in:
parent
581ecdc534
commit
fbc207f75f
@ -1,4 +1,6 @@
|
||||
import axios from "axios"
|
||||
import { useAuthStore } from "@/stores/auth";
|
||||
|
||||
const api = axios.create({
|
||||
baseURL: process.env.VUE_APP_BASE_URL,
|
||||
clientId : process.env.VUE_APP_CLIENT_ID,
|
||||
@ -22,27 +24,26 @@ api.interceptors.request.use(
|
||||
api.interceptors.response.use(
|
||||
response => response,
|
||||
async error => {
|
||||
if(error.response && error.response.status === 401){
|
||||
const refresh_token = localStorage.getItem('refresh_token')
|
||||
if(refresh_token){
|
||||
try {
|
||||
const res = await axios.post(`${this.baseURL}` + "/oauth/token/refresh/", {
|
||||
refresh: refresh_token,
|
||||
});
|
||||
const originalRequest = error.config;
|
||||
const auth = useAuthStore();
|
||||
|
||||
localStorage.setItem("access_token", res.data.access);
|
||||
error.config.headers.Authorization = `Bearer ${res.data.access}`;
|
||||
return api.request(error.config);
|
||||
if(error.response && error.response.status === 401 && !originalRequest._retry){
|
||||
originalRequest._retry =true;
|
||||
try {
|
||||
const res = await axios.post(`${this.baseURL}` + "/oauth/token/refresh/", {},{withCredentials:true});
|
||||
|
||||
auth.token = res.data.access_token;
|
||||
originalRequest.headers.Authorization = `Bearer ${auth.token}`;
|
||||
// error.config.headers.Authorization = `Bearer ${res.data.access}`;
|
||||
// return api.request(error.config);
|
||||
return api(originalRequest)
|
||||
} catch (refreshError) {
|
||||
// jika refresh juga gagal, logout
|
||||
localStorage.removeItem("access_token");
|
||||
localStorage.removeItem("refresh_token");
|
||||
window.location = "/login";
|
||||
auth.logout();
|
||||
return Promise.reject(refreshError)
|
||||
}
|
||||
}
|
||||
localStorage.removeItem('token')
|
||||
window.location.href = '/'
|
||||
}
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user