Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Destroy cookie NodeJs

I am using Cookies module for setting cookie. Here is following my code:

var options = {     maxAge: ALMOST_ONE_HOUR_MS,     domain: '.test.com',     expires: new Date(Date.now() + ALMOST_ONE_HOUR_MS) }; var value = userInfo.token; cookies.set("testtoken", value, options); 

But in documentation I haven't found how to destroy this cookie.

Any suggestion would be appreciated.

like image 759
Manwal Avatar asked Jan 16 '15 07:01

Manwal


People also ask

How do I delete cookies in node JS?

There is no way to delete a cookie according to the HTTP specification. To effectively "delete" a cookie, you set the expiration date to some date in the past.

How do you destroy cookies?

Destroying cookies is upto the browser however you can remove a cookie (which is the same for your app) by setting the date in the past: setcookie($cookie_name, "", 1); Most set the time to 1970 .

How do I delete cookies in res cookies?

The res. clearCookie() function is used to clear the cookie specified by name. This function is called for clearing the cookies which as already been set. For example if a user cookie is set, then it can be cleared using this function.

How do I delete all cookies in Express?

Deleting Existing Cookies To delete a cookie, use the clearCookie function. For example, if you need to clear a cookie named foo, use the following code. var express = require('express'); var app = express(); app. get('/clear_cookie_foo', function(req, res){ res.


1 Answers

For webapp you can just set cookie in response as :

res.cookie("key", value); 

and to delete cookie :

res.clearCookie("key"); 

and don't forget to:

res.end() 

to avoid the web request hanging.

like image 184
vashishatashu Avatar answered Sep 17 '22 12:09

vashishatashu