I am working with Vue. Trying to create cookies using the vue-cookies library.
[[vue-cookies](https://www.npmjs.com/package/vue-cookies)]
for simplicity lets say I have 2 files a component file. register.vue,
and an authentication typescript file. auth.ts
I am trying to setup the cookies in the auth.ts such that I can then import elsewhere. What should be a trivial task is proving challenging. I do not know how to setup the cookies,and the method recommended in the docs are brief and somewhat unclear.
The data is a jwt, and it is created server side. After going to the register route on the server the created jwt is passed via http response back to the client side vue application. The intention is to store it via cookies, and pass it as necessary back to the server on any needed routes. auth.ts will only run client side (browser). Setting and reading the token is being done inorder to store the passed in jwt, and pass the info back to the server to protected routes as needed.
// file name : auth.ts
import VueCookies from 'vue-cookies'
//i tried what the npm docs seems to recommend. It doesnt work.
const $cookies = inject<VueCookies>('$cookies')
// i tried the below. It is Not callable and doesnt work
const cookies = VueCookies()
// i tried this and it doesnt work.
const cookies = VueCookies('$cookies')
Once I have the cookies object instatiated properly, the intention is to proceed like so
cookies.config('2d')
cookies.set('abc', '123) etc.
I tried many more options, which dont work. Reading the vue-cookies code base has so far proven a bit tricky.
There arent a lot of examples to go off of, and I am still learning Vue and typescript. Any assistance would be appreciated. Again I am using vue 3 with composition API and trying to set a cookie in a typescript file using the vue-cookies library.
Related post. Here the OP had the same issue. However the response provided doesnt quite answer the question directly though (though the responder alludes to why ‘inject’ might not work outside components hierarchy, but doesn’t seem to propose an alternative…)
[/questions/75551682/vue3-how-to-work-with-cookies-outside-components]
sope lanlehin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.