javascriptangularjscookiesangular-cookies

How to delete ngCookie (angular-cookies)


I created a cookie called 'stateCookie' when I start my app. As I change state I add variables into that cookie. When I logout, I want this cookie to be completely destroyed, or empty.

On their docs it's just remove() I've done that but the cookie still exists. ie: $cookies.remove('stateCookie');

After logout and login: >>> refreshCookie {"ticker":"SPY","term_id_1":5112166,"term_id_2":30111615,"term_id_3":13064871}

enter image description here


this.logout = () => AuthFactory.webservice_logout().then((res) => {
    // $cookies.remove('stateCookie');
    angular.forEach($cookies, function(value, key) {
        $cookies.remove(key);
    });

    // console.log('$cookies', $cookies);

    // $cookies.map((value, key) => {
    //  $cookies.remove(key);
    // });

    const refreshCookie = $cookies.get('stateCookie');
    console.log('!!!refreshCookie', refreshCookie);
    $state.go('login', {}).then(() => {
        $state.reload();
    });
});

Hack fix

I'm able to clear the cookie by calling window.reload after going back to the login state.

$state.go('login', {}).then(() => window.location.reload(true));


Solution

  • Try this:

    this.logout = () => AuthFactory.webservice_logout().then((res) => {
        // $cookies.remove('stateCookie');
        var cookies = $cookies.getAll();
        console.log(cookies);
        angular.forEach(cookies, function(value, key) {
            $cookies.remove(key);
        });
    
        // console.log('$cookies', $cookies);
    
        // $cookies.map((value, key) => {
        //  $cookies.remove(key);
        // });
    
        const refreshCookie = $cookies.get('stateCookie');
        console.log('!!!refreshCookie', refreshCookie);
        $state.go('login', {}).then(() => {
            $state.reload();
        });
    });