I changed the axios boot file like below by adding Vue.mixin instead of Vue.prototype. But now when I try to access this.$axios.post() in .vue component its throwing TypeError: Cannot read property 'post' of undefined. So the global mixin is not passing into components. I simply copied the mixin logic from here.
( didnt try the injection part as written in this post as I got stuck before
that part and trying to understand whats happening :))
import Vue from 'vue'
import axios from 'axios'
import { Cookies } from 'quasar'
Vue.mixin({
beforeCreate () {
const options = this.$options
if (options.axios) {
this.$axios = options.axios
} else if (options.parent) {
this.$axios = options.parent.$axios
}
}
})
export default function ({ app, ssrContext, store, router }) {
const axiosInstance = axios.create({
baseURL: 'http://lvh.me:3000',
headers: {
'Content-Type': 'application/json'
}
})
const cookies = process.env.SERVER
? Cookies.parseSSR(ssrContext)
: Cookies
axiosInstance.interceptors.request.use(config => {
const userString = cookies.get('user')
if (userString) {
// setup vuex
store.$db().model('users').create({
data: userString.user
})
// setup headers
config.headers.Authorization = `bearer ${userString.token}`
}
return config
}, error => {
return Promise.reject(error)
})
axiosInstance.interceptors.response.use(
response => response,
error => {
// if login route no need to reload
if (error.response.status === 401 && router.currentRoute.path !== '/login') {
// store.$db().model('users').logout()
cookies.remove('user')
location.reload()
}
return Promise.reject(error)
}
)
app.$axios = axiosInstance
}
ops, i didn't created a repo to this article, mostly because this is a simplified version of what i do.
Normally, I didn't access the cookies directly, i use vuex-persistedstate as a middleware.
but u can access the Cookies directly, there is nothing wrong with this approach github.com/TobyMosque/qpanc/blob/m...
This guide has been invaluable, thank you. Just one thing I'm trying to puzzle out - what is the purpose of the beforeCreate() code here? Is there a reason to prefer this over injecting the Axios instance in the Vue prototype?
Vue.prototype is shared between all Vue instances, what will result in a single axios instance to all Vue instances (a singleton).
But in that particular case, a singleton can mess with our autentication logic, since that is being handled into the axios interceptor and this reads the token from the Cookies plugin, what isn't a singleton, what can lead to a data leak.
Thank you for the excellent article! It would be nice to have this added to the Quasar documentation (if it is not there already), as it is rather square one if you want to implement SSR. I'd have one small remark, under 5 Navigation guards, in ./src/router/index.js, I had to switch context.router.beforeEnter to context.router.beforeEach. As far as I know, there is no global guard beforeEnter now in Vue Router, I'm not sure for older versions.
I changed the
axiosboot file like below by addingVue.mixininstead ofVue.prototype. But now when I try to accessthis.$axios.post()in.vuecomponent its throwingTypeError: Cannot read property 'post' of undefined. So the global mixin is not passing into components. I simply copied the mixin logic from here.Any insights on what could be the issue here?
this would be:
Thank you.
Great article! Can you please show your final implementation on Github repo?
really sorry, i missed your comment.:
gitlab.com/TobyMosque/quasar-couch...
I think this link is not for this article as this code is missing there. Could you please share the correct link if possible?
ops, i didn't created a repo to this article, mostly because this is a simplified version of what i do.
Normally, I didn't access the cookies directly, i use
vuex-persistedstateas a middleware.but u can access the Cookies directly, there is nothing wrong with this approach
github.com/TobyMosque/qpanc/blob/m...
And here the axios part:
github.com/TobyMosque/qpanc/blob/m...
I'll try to create a repo to this article at the weekend.
Ok. Thank you for the support
This guide has been invaluable, thank you. Just one thing I'm trying to puzzle out - what is the purpose of the
beforeCreate()code here? Is there a reason to prefer this over injecting the Axios instance in the Vue prototype?Vue.prototypeis shared between allVueinstances, what will result in a singleaxiosinstance to allVueinstances (a singleton).But in that particular case, a singleton can mess with our autentication logic, since that is being handled into the
axiosinterceptor and this reads the token from the Cookies plugin, what isn't a singleton, what can lead to a data leak.Also a great Article and perfect understandable written.
Thank you for sharing your know how.
Thank you for the excellent article! It would be nice to have this added to the Quasar documentation (if it is not there already), as it is rather square one if you want to implement SSR. I'd have one small remark, under 5 Navigation guards, in ./src/router/index.js, I had to switch context.router.beforeEnter to context.router.beforeEach. As far as I know, there is no global guard beforeEnter now in Vue Router, I'm not sure for older versions.
probably a small typo, thx for report.
Thank you man!
how can I get app context for call my other service?
I didn't understood u question