Thach Huynh
Thach Huynh

Reputation: 1303

Cookies work great on localhost but does not work on VPS

I am using nodejs and angularjs 1.6.4. Everything works great on localhost but when I push my code to Digital Ocean droplet through Github, the cookies does not work. Everything that I saved to cookie does not appear in Cookie Storage. Why does it happen ?

UPDATE A warning appears when I go to Login page on VPS: "This page includes a password or credit card input in a non-secure context. A warning has been added to the URL bar"

Here is the relevant code:

$scope.login = function () {

      UserService.login($scope.user).then(function (result) {
        if (result.data.success) {
          $rootScope.userLogin = result.data.data.name;

          //------------------
          var day = new Date();
          day.setDate(day.getDay() + 30);

          var options = {
            domain: "localhost",
            httpOnly: true,
            expires: day
          };
          // cookie does not work, nothing in Cookie Storage
          // but it works perfectly on localhost
          $cookies.put('token', result.data.data.token, options);
          $cookies.put('name', result.data.data.name, options);
          // the session storage work greatly
          $sessionStorage.user = 'heheeheh';
          flash.success = result.data.message;
          $state.go('home');
        } else {
          flash.error = result.data.message;
        }
      });
    } 

UPDATE:

I created another VPS on amazon ec2 but it still does not work.

Upvotes: 1

Views: 578

Answers (2)

Alfri Ansyah
Alfri Ansyah

Reputation: 43

yeah i also face the same problem, cookies not store in local storage, i work with nuxt and nestjs. solve by send domain of frontend in cookies

 .cookie('token', token,{
    domain:"web.frontend.cloud",
    sameSite: 'none',
    secure:true,
    httpOnly: false,
  })

after add domain:"web.fronend.cloud", the cookies finally stored in localstorage/cookies

for more info how domain accepted read here https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.3

Upvotes: 1

Thach Huynh
Thach Huynh

Reputation: 1303

I found that just change the domain options from localhost to server's IP

Upvotes: 2

Related Questions