firebase / functions-samples

Collection of sample apps showcasing popular use cases using Cloud Functions for Firebase
https://firebase.google.com/docs/functions
Apache License 2.0
12.06k stars 3.84k forks source link

All cookies undefined when sign in with google #510

Closed poirierjm closed 5 years ago

poirierjm commented 5 years ago

I downloaded the sample project functions-samples/template-handlebars/ and when I sign into a google account, all the cookies get undefined. Only __session remain in the req.cookies in my index.js file running with express. When I logout the others cookies appear back.

But when I print my cookies in the google inspect console everything is fine and all the cookies are there including __session

Here is the code I modified in the main.handlebars file.

  <script>

    function s4() {
      return Math.floor((1 + Math.random()) * 0x10000)
        .toString(16)
        .substring(1);
    }

    function generateCartCookie() {
      var hasSessionCookie = document.cookie.indexOf('__cart=') !== -1;
      if (!hasSessionCookie) {
        var guid = "ss-s-s-s-sss".replace(/s/g, s4);
        document.cookie = '__cart=' + guid + ';max-age=' + (guid ? 900 : 0);
      }
    }

    function checkCookie() {
      // Checks if it's likely that there is a signed-in Firebase user and the session cookie expired.
      // In that case we'll hide the body of the page until it will be reloaded after the cookie has been set.
      var hasSessionCookie = document.cookie.indexOf('__session=') !== -1;
      var isProbablySignedInFirebase = typeof Object.keys(localStorage).find(function (key) {
        return key.startsWith('firebase:authUser')
      }) !== 'undefined';
      if (!hasSessionCookie && isProbablySignedInFirebase) {
        var style = document.createElement('style');
        style.id = '__bodyHider';
        style.appendChild(document.createTextNode('body{display:none}'));
        document.head.appendChild(style);
      }
    }
    checkCookie();
    generateCartCookie();

    document.addEventListener('DOMContentLoaded', function () {
      // Make sure the Firebase ID Token is always passed as a cookie.
      firebase.auth().addAuthTokenListener(function (idToken) {
        var hadSessionCookie = document.cookie.indexOf('__session=') !== -1;
        document.cookie = '__session=' + idToken + ';max-age=' + (idToken ? 3600 : 0);
        // If there is a change in the auth state compared to what's in the session cookie we'll reload after setting the cookie.
        if ((!hadSessionCookie && idToken) || (hadSessionCookie && !idToken)) {
          window.location.reload(true);
        } else {
          // In the rare case where there was a user but it could not be signed in (for instance the account has been deleted).
          // We un-hide the page body.
          var style = document.getElementById('__bodyHider');
          if (style) {
            document.head.removeChild(style);
          }
        }
      });
    });
  </script>
poirierjm commented 5 years ago

I found my answer here. https://stackoverflow.com/questions/44929653/firebase-cloud-function-wont-store-cookie-named-other-than-session/44935288#44935288