Answer the question
In order to leave comments, you need to log in
Problem updating access token. Why is this happening?
Here I create an instance and at the first login everything works fine, but when the token expires, a problem arises, it gives a 400 error when requesting UPDATE_TOKEN, while when checking all the same in postman, everything works
'token' in localStorage - refresh token
const $api = axios.create({
baseURL: LOCAL_API
})
$api.interceptors.request.use((config) => {
config.headers.Authorization = `Bearer ${localStorage.getItem('accessToken')}`
return config
})
$api.interceptors.response.use((config) => {
return config
}, async (error) => {
const originalRequest = error.config
if (error.response.status == 401 && error.config && !error._isRetry) {
originalRequest._isRetry = true
try {
const response = await axios({
method: 'post',
url: UPDATE_TOKEN,
data: localStorage.getItem('token')
})
console.log(response.data)
localStorage.setItem('accessToken', response.data.accessToken)
return $api.request(originalRequest)
} catch (e) {
console.log('Не авторизован')
}
}
throw error
}
)
export default $api
Answer the question
In order to leave comments, you need to log in
On the server, in Access-Control-Allow-Origin, try inserting a link from where the request comes from. Like if the front is on localhost:7777 , then write this link on the back (in the full version, with http://) in Access-Control-Allow-Origin.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question