whenever am i trying to remove cookie its show me this error.
TypeError: universal_cookie__WEBPACK_IMPORTED_MODULE_3__.default.remove is not a function
this is my code
Logout = () =>{
var user = Cookies.get('shailuKiCookie');
// console.log(user);
if(user){
Cookies.remove("shailuKiCookie");
// alert("logout successfully");
window.location.reload(false);
}else{
window.location.reload(false);
}
}
enter image description here
function set_cookie(name, value) {
document.cookie = name +'='+ value +'; Path=/;';
}
function delete_cookie(name) {
document.cookie = name +'=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}
You don't have to specify a cookie value when you delete a cookie.
Just set the expires parameter to a past date:
note : HttpOnly cookies cannot be deleted with JavaScript on the client side.
you should create a Cookies object.
const cookies = new Cookies()
cookies.remove('cookie name')
Related
I try to use following code to delete all cookies in page unload event. However it doesn't seem to work in Chrome. Is there a better way to do it?
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var chip = cookies[i],
entry = chip.split("="),
name = entry[0];
document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT; domain=' + window.location.hostname + '; path=/ ';
}
From another post I got a hint. The cookies I want to delete has Host only flag. Therefore I can not set domain when I write cookie back. Here is a working copy.
ar cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var chip = cookies[i],
entry = chip.split("="),
name = entry[0];
document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/ ';
}
I am trying to set a cookie in ESRI Arcgis online using ESRI runtime SDK for .net v100.
var cookie = new CookieHeaderValue("customCookie", cred.Token);
var response = Request.CreateResponse(HttpStatusCode.OK, new {
token = cred.Token,
expires = cred.ExpirationDate
});
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
response.Headers.AddCookies(new CookieHeaderValue[] { cookie });
return response;
Now when I try to retrieve that cookie later on in subsequent requests using below I get null.
CookieHeaderValue cookie = context.Request.Headers.GetCookies("customCookie").FirstOrDefault();
I am wondering if there is another way to get the cookie which I set back?
Are you using v100?
If yes, you can try the following code:
ArcGISHttpClientHandler.HttpRequestBegin += (sender, request) =>
{
var cookieContainer = ((System.Net.Http.HttpClientHandler)sender).CookieContainer;
var cookies = cookieContainer.GetCookies(request.RequestUri);
var customCookie = new Cookie("customCookie", "someValue") { Domain = request.RequestUri.Host };
bool foundCookie = false;
foreach (Cookie cookie in cookies)
{
if (cookie.Name == customCookie.Name)
{
foundCookie = true;
break;
}
}
if (!foundCookie)
cookieContainer.Add(customCookie);
};
ArcGISHttpClientHandler has an event HttpRequestBegin which is invoked on every request. You can use CookieContainer.GetCookies and Add to retrieve/add cookies.
Im using the google translate code on my page and it works good, but if I look at the cookie it says that it only have expiration during the session!? So I want to set it so it does´t expire, so that it is the same language when the user comes back as he choosed the first time.
Im using this now.
SOLVED! OK so with this the user can select a language and the next time he visit the page it is translated to the language he picked before!
var ckDomain;
function googleTranslateElementInit() {
function getCookie(name)
{
var re = new RegExp(name + "=([^;]+)");
var value = re.exec(document.cookie);
return (value != null) ? unescape(value[1]) : null;
}
var kakan;
var googkakan;
kakan=getCookie("googtrans22");
$$(document).on('change', '#google_translate_element', function (e) {
setTimeout(function(){
function getCookie(name)
{
var re = new RegExp(name + "=([^;]+)");
var value = re.exec(document.cookie);
return (value != null) ? unescape(value[1]) : null;
}
googkakan=getCookie("googtrans");
document.cookie = "googtrans22="+googkakan+"; expires=Thu, 07-Mar-2047 20:22:40 GMT; path=/" + ckDomain;
document.cookie = "googtrans22="+googkakan+"; expires=Thu, 07-Mar-2047 20:22:40 GMT; path=/";
},1000);
});
for (var ckDomain = window.location.hostname.split("."); 2 < ckDomain.length;){
ckDomain.shift();
}
ckDomain = ";domain=" + ckDomain.join(".");
// domain cookie
document.cookie = "googtrans="+kakan+"; expires=Thu, 07-Mar-2047 20:22:40 GMT; path=/" + ckDomain;
// host-only cookie (with no domain name definition)
document.cookie = "googtrans="+kakan+"; expires=Thu, 07-Mar-2047 20:22:40 GMT; path=/";
new google.translate.TranslateElement({
pageLanguage: 'sv',
autoDisplay: false,
layout: google.translate.TranslateElement
}, 'google_translate_element');
}
(function() {
var googleTranslateScript = document.createElement('script');
googleTranslateScript.type = 'text/javascript';
googleTranslateScript.async = true;
googleTranslateScript.src = 'https://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit';
( document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0] ).appendChild( googleTranslateScript );
})();
Apparently the library is forced to write over the cookie to make it expire at end of the session.
The good news is that before doing that it reads the existing cookie, so you can feed it before each initialization call.
To force the library to translate a Swedish page into English:
function googleTranslateElementInit() {
var ckDomain;
for (var ckDomain = window.location.hostname.split("."); 2 < ckDomain.length;){
ckDomain.shift();
}
ckDomain = ";domain=" + ckDomain.join(".");
// domain cookie
document.cookie = "googtrans=/sv/en; expires=Thu, 07-Mar-2047 20:22:40 GMT; path=/" + ckDomain;
// host-only cookie (with no domain name definition)
document.cookie = "googtrans=/sv/en; expires=Thu, 07-Mar-2047 20:22:40 GMT; path=/";
new google.translate.TranslateElement({
pageLanguage: 'sv',
autoDisplay: false,
layout: google.translate.TranslateElement
}, 'google_translate_element');
}
How to add expiry date to Angular 2 cookie. I am trying with Angular 2.0.0-beta.15. When i try to add options like below:
var expireDate = new Date (new Date().getTime() + (1000 * data.expires_in));
this._cookieService.put('token', this.token, {expires: expireDate});
above code throwing error like "[ts] Argument of type '{ expires: Date; }' is not assignable to parameter of type 'CookieOptionsArgs'.
Property 'path' is missing in type '{ expires: Date; }'. (local var)
expireDate: Date"
I can see on https://www.npmjs.com/package/angular2-cookie to override default options globally. I doubt if this is the method to add expiry date to cookie? Can somebody help me to understand more on this?
I have used this library with success: https://github.com/BCJTI/ng2-cookies. Use it like below with setting expiry date:
Include the lib:
import {Cookie} from 'ng2-cookies/ng2-cookies';
Set a cookie with expiry date of 365 days:
Cookie.setCookie("cookie-key", "cookie-value", 365);
A little bit late but Should be something like this
let key = 'testCookieKey';
let value = 'testCookieValue';
let opts: CookieOptionsArgs = {
expires: new Date('2030-07-19')
};
cookieService.put(key, value, opts);
Hope that
For Expiry date parameter in set cookies the value should be seconds to days calculation.
i.e. 2 min = (2/1440)
//use for 2 minutes....
auth-cookies.ts
import { Injectable } from '#angular/core';
import { Cookie } from 'ng2-cookies/ng2-cookies';
#Injectable()
export class AuthCookie {
constructor() { }
getAuth(): string {
return Cookie.get('id_token');
}
setAuth(value: string): void {
//0.0138889=20 minuts
//this accept day not minuts
//use for 20 minuts
Cookie.set('cookie_token', value, 0.0138889);
//use for 2 minuts
// Cookie.set('cookie_token', value,0.00138889)
}
deleteAuth(): void {
Cookie.delete('id_token');
}
}
i post it too late but hope this will be useful.
I am already using this code add and delete in the cookies
deleted code:
String profileScore=null;
Cookie cookiesScore =new Cookie("profileScore","");
cookiesScore.setValue("");
cookiesScore.setMaxAge(0);
response.addCookie(cookiesScore);
but its not deleted properly please help me give any example
Try this if using javascript
create a cookie:
call a this method at login:
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
delete a cookie:
call a this method at logout:
function deleteCookies() {
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i];
var eqPos = cookie.indexOf("=");
var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
}
}