I
I
Igor Bezlepkin2018-05-25 11:39:07
JavaScript
Igor Bezlepkin, 2018-05-25 11:39:07

What is the best way to request a refresh_token in Vue?

Hey!
It is necessary to check the validity of the access_token before each ajax request, and if it is expired before the ajax request, make a request to reissue it by refresh_token.
I do it like this:

Vue.http.interceptors.push((request, next) => {
  var accessToken = null
  if (window.location.pathname !== '/login') {
    if (window.localStorage && typeof window.localStorage.user !== 'undefined' && typeof window.localStorage.user_auth !== 'undefined') {
      var _user = JSON.parse(window.localStorage.user)
      var userAuth = JSON.parse(window.localStorage.user_auth)
      var interval = parseInt(new Date().getTime() / 1000) - userAuth.timestamp
      // ставим - 10 секунд, перестраховываемся на счет задержки при выдачи токена
      if (interval > userAuth.expires_in - 10) {
        if (request.url !== 'https://api.domain.ru/v1/oauth/refresh') {
          accessToken = user.refreshToken(_user.id, userAuth.refresh_token)
          request.headers.set('Authorization', 'Bearer ' + accessToken)
        }
        next()
      }
    } else {

    }
    accessToken = store.state.userAuth.access_token
    request.headers.set('Authorization', 'Bearer ' + accessToken)
  }
})

But it turns out some kind of pornography.
What is the best way to do this?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question