Disclaimer: I have separated my client application (Vue.js) from the server side (DjangoRest). I am utilizing JWT for validating each request sent from the client to the server. Here is how it works - The client forwards user credentials to the server, and if the credentials are valid, the server responds with a refresh token and an access token. The client then stores these tokens. I have defined the refresh token expiry as 1 week and the access token expiry as 30 minutes. Now, I aim to ensure that the access token gets automatically refreshed 15 minutes prior to its expiration. This involves sending the stored refresh token from the client to the server, which will issue new access and refresh tokens in return. How can I incorporate this into the Vuex store? As someone new to web development and Vue.js, any code snippet or detailed explanation would be greatly appreciated.
I have successfully implemented functions like loginUser, logoutUser, registerUser in the store, and they work fine. However, I am facing difficulty in implementing the auto-refresh logic. My assumption is that the client needs to periodically check the remaining expiry time of the access token. When there's around 15 minutes left, the autorefresh function should kick in. Can someone guide me through this process?
Below is a snippet of my Vuex store:
import Vue from 'vue'
import Vuex from 'vuex'
import axiosBase from './api/axios-base'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
accessToken: '' || null,
refreshToken: '' || null
},
getters: {
loggedIn (state) {
return state.accessToken != null
}
},
mutations: {
loginUser (state) {
state.accessToken = localStorage.getItem('access_token')
state.refreshToken = localStorage.getItem('refresh_token')
},
destroyToken (state) {
state.accessToken = null
state.refreshToken = null
}
},
actions: {
registerUser (context, data) {
return new Promise((resolve, reject) => {
this.axios.post('/register', {
name: data.name,
email: data.email,
username: data.username,
password: data.password,
confirm: data.confirm
})
.then(response => {
resolve(response)
})
.catch(error => {
reject(error)
})
})
},
// More action functions can be added here...
}
})
Any assistance provided would be highly valued. Thank you!