I have been developing an application that makes frequent requests to our API, however, sometimes these requests result in expired tokens.
I have explored different solutions such as calling Auth.currentSession and setting the header before sending the request. However, this approach does not work seamlessly as many of the libraries we utilize do not wait for Auth.currentSession to return the new token if the current one has expired.
It appears that the most effective solution is to manually invoke Auth.currentSession every 9 minutes and store the token in the Vuex store. Is it possible to implement this in the Vuex store.js?
import { createStore } from 'vuex'
import { Auth } from 'aws-amplify'
export default createStore({
state: {
session: null
},
getters: {
},
mutations: {
setSession(state, value){
state.session = value
}
},
// This function will automatically refresh the token every 9 minutes.
actions: {
async refreshSession({ commit }){
try {
const session = await Auth.currentSession()
commit('setSession', session)
} catch (error) {
console.log(error)
}
}
},
modules: {
}
})
I would like this process to run automatically every 9 minutes without having to manually trigger it from other components.