0

i used the local Storage to but after the logout if i hit the same URL the user is able to login again even i checked with the session Storage i am facing the same issue, is there any way that i can handle a cookie/session so that after the page refresh or the user logout and hit the same URL user should not be able to login.

logout() {
   this.cookie.delete("token");
   this.cookie.delete("userId");
   this.cookie.delete("accountId");
}

this is how i am clearing the cookie.

Santhosh
  • 810
  • 2
  • 10
  • 28

1 Answers1

0

I have written a function for this

function deleteCookies(removeCookies) {
    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;
        if(name in removeCookies)         
      document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
    }
}

var removeCookieSet = ["token","userId","accountId"] ;
deleteCookies(removeCookieSet)

this set has been taken from i have rewritten so you can use it. Clearing all cookies with JavaScript

Above is direct , if you want to work with angular JS

angular.module('cookiesExample', ['ngCookies'])
.controller('ExampleController', ['$cookies', function($cookies) {

 var removeCookieSet = ["token","userId","accountId"];

 for(i=0;i<removeCookieSet.length;i++)
  // Setting a cookie
  $cookies.remove(removeCookieSet[i]);
}]);

Using angular JS , you will have to inject ngcookies

and using typescript

https://tutorialedge.net/typescript/angular/angular-cookies-tutorial/

Shyam Joshi
  • 387
  • 4
  • 9