4
4
46kvolamak2021-08-31 11:19:39
JavaScript
46kvolamak, 2021-08-31 11:19:39

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


When setting the withCredentials: true parameter, the following error occurs

. Request from an external source is blocked: The single origin policy prohibits reading the remote resource at "here url". (Reason: Credentials are not supported when the CORS "Access-Control-Allow-Origin" header is set to "*"
I don't quite understand Access-Control-Allow-Origin should be set on the server because setting it in the request headers is not helps

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
veryoriginalnickname, 2021-09-01
@46kvolamak

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 question

Ask a Question

731 491 924 answers to any question