Within my main.js
, I have implemented a code snippet that checks if a certain item exists in the localStorage. If it does, the code will add an Authorization
header to the ApolloClient setup using middleware.
However, if a new item is added to the localStorage at a later time, it will not be recognized by the middleware in main.js
. A complete page refresh becomes necessary to ensure the new item is picked up and acknowledged.
The question arises: How can I re-run the main.js
script (if this is indeed the solution) from a method within a different component responsible for user sign-in?
Here is a glimpse of my main.js
:
import Vue from 'vue'
import ApolloClient, { createNetworkInterface } from 'apollo-client'
import VueApollo from 'vue-apollo'
import App from './App'
import router from './router'
import store from './store'
const networkInterface = createNetworkInterface({ uri: 'https://api.graph.cool/simple/v1/MY-ID-HERE' })
const requestToken = localStorage.getItem('TOKEN')
networkInterface.use([{
applyMiddleware (req, next) {
if (!req.options.headers) {
req.options.headers = {}
}
req.options.headers['Authorization'] = requestToken ? `Bearer ${requestToken}` : null
next()
}
}])
const apolloClient = new ApolloClient({
networkInterface
})
const apolloProvider = new VueApollo({
defaultClient: apolloClient
})
Vue.use(VueApollo)
/* eslint-disable no-new */
new Vue({
el: '#app',
template: '<App />',
components: {
App
},
apolloProvider,
router,
store
})
I hope the intentions behind my approach are clear. Thank you for your understanding.
Best regards