How to Set Cookie in Vuejs

How to set cookie in vuejs?

You could use the vue-cookie or vue-cookies npm package.
You can set a cookie in the created method.

created() {
this.$cookie.set("keyName", keyValue, "expiring time")

Setting an cookie from an third-party API

Below is a simple example of how u do that

function setCookie(cname, cvalue, exdays) {
const d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
let expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";

function getCookie(cname) {
let name = cname + "=";
let ca = document.cookie.split(';');
for(let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
return "";

function checkCookie() {
let user = getCookie("username");
if (user != "") {
alert("Welcome again " + user);
} else {
user = prompt("Please enter your name:", "");
if (user != "" && user != null) {
setCookie("username", user, 365);

how to set cookie with httponly flag in vuejs and vuex that cookie cames from server(Laravel)

you should create cookie with httponly flag on server. for example, in nodejs + express you do it with res.cookie("token", token, {httpOnly: true}).
also notice, with httponly flag you cant read cookie on client.

how to set cookies during vuejs post

I posted this question some time ago, I have managed to work around it by running the vue frontend on the same network as the django backend. Follow this tutorial for instructions: integrating vuejs and django
Once I had the application set up I was able to set the cookies much more cleanly using :

axios.defaults.xsrfCookieName = 'csrftoken'
axios.defaults.xsrfHeaderName = "X-CSRFTOKEN"

Here is my login page for example

<div class = "container">
<h2>Sign In</h2>
<b-form v-on:submit.prevent="submit()">
<b-form-group id="signin" label="">
<!-- dynamic error message -->
<p class="loginErr" v-if="logErr">Incorrect Username or Password</p>



<b-button v-if="!loading" type="submit" variant="primary">Submit</b-button>
<b-spinner v-if="loading"></b-spinner>

import axios from 'axios'
import Vue from 'vue'
export default {

data: ()=>{
loading: false,
logErr: false,
next: '%2Findy%2Fprofile%2F'
created: function(){

methods: {
var vm = this;
vm.loading = true;
var dataStr = 'username='+vm.username+'&password='+vm.password

//set the csrf tokens so django doesn't get fussy when we post
axios.defaults.xsrfCookieName = 'csrftoken'
axios.defaults.xsrfHeaderName = "X-CSRFTOKEN"'http://localhost:8000/api/signin/', dataStr)
.then(function (response) {
vm.loading = false;
//determine if indy accepts the login request

var res =

vm.logErr = true;

.catch(function (error) {
//currentObj.output = error;
redirect(path) {
this.$router.push('/' + path);
color: orange;

Vue 3 with vue3-cookies sets same cookies to different locations on variable routes

If I going to route like:

<router-link to=`/user/{}`>

Then issue occurs.

But, If I going to route like:

<router-link :to="{ name: 'user', params: { id: String(user?.id) } }">

That's fine.
Strange behavior.

set-cookie is set in response header but it is not showing in Application->Cookies

After long search I found that you cannot set cookie for cross origin.
I am using Vue and Django Rest Framework.

You can use subdomains and set cookie with samesite 'none', secure and set domain to ''.

How to get cookie in Vue Router in Vue 3? this undefined in router/index.js

Vue Router 4 removes, but you could add it yourself when setting up the router:

// main.js
import router from './router'

const app = createApp(App)

app.use(router) = app


Then in the router config script, you could use that reference to get at app.config.globalProperties.$cookies (the global added by vue3-cookies):

// router.js
const router = createRouter({
history: createWebHistory(),

function requireAuth(to, from, next) {
const { $cookies } =
console.log('_ga', $cookies.get('_ga'))

export default router


Related Topics

Leave a reply