You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
How to handle unauthorized error after main auth token expires and do a refresh token request?
I tried something like this:
import JsonApi from 'devour-client'
import store from '../store/index'
const api = new JsonApi({
apiUrl: 'http://0.0.0.0:8081/api/v1',
bearer: null,
},
)
api.define('token-request', {
token: '',
refresh_token: '',
})
const authMiddleware = {
name: 'refresh-token',
error: (payload) => {
if (payload.response) {
const response = payload.response
if (response.status === 401) {
const refreshToken = store.getters['auth/rToken']
if (!refreshToken) {
return payload
}
api.removeMiddleware('refresh-token')
console.log('refresh-token', store.getters['auth/rToken'])
api.runMiddleware({
url: api.apiUrl + '/auth/refresh-token',
method: 'POST',
data: {
refresh_token: store.getters['auth/rToken'],
},
model: 'token-request',
})
.then((r) => {
console.log('worked', r.data.token)
store.dispatch('auth/refreshToken', r.data)
// need to retry the request
return api.axios(payload)
})
.catch((err) => {
console.log('failed', err)
})
.finally(() => {
api.insertMiddlewareBefore('errors', authMiddleware)
})
}
}
return payload
},
}
api.insertMiddlewareBefore('errors', authMiddleware)
export default api
the part with api.axios(payload) is not working, I need to retry the last request if everything is OK but I get error
failed TypeError: Cannot read property 'protocol' of undefined
at isURLSameOrigin (isURLSameOrigin.js?3934:57)
at dispatchXhrRequest (xhr.js?b50d:109)
at new Promise (<anonymous>)
at xhrAdapter (xhr.js?b50d:12)
at dispatchRequest (dispatchRequest.js?5270:52)
The text was updated successfully, but these errors were encountered:
How to handle unauthorized error after main auth token expires and do a refresh token request?
I tried something like this:
the part with
api.axios(payload)
is not working, I need to retry the last request if everything is OK but I get errorThe text was updated successfully, but these errors were encountered: