0
app.controller('appCtrl', function ($scope, $cookieStore) {
$scope.$watch(function(){return $cookieStore.get('currentUser');},   function(newValue, oldValue){
    if(newValue !== oldValue){
            $scope.currentUser = $cookieStore.get('currentUser');
        }
    });
});

I have developed the code above with the intention to watch value saved in $cookieStore. When user singed in successfully, the json object will be saved in $cookieStore, with the help of this $watch function, user information would display on the top corner of the page.

$cookieStore.put('currentUser', response);

I am having two issues with this solution:

  1. the $watch function does not update the $scope.currentUser as I was hoping to. It only gets updated when I refresh the whole web page.
  2. Somewhere in this solution, the $digest() functions was called repeatedly. I tried to resolve this problem by adding if(newValue !== oldValue){}, it does not work.

If I change my solutions to use $cookies instead of $cookieStore, it seems to be all working as expected, but $cookies does not allow me to save a Json object, which is why I prefer to use $cookieStore instead.

Any ideas? Really appreciated it!

Jiandong Chen
  • 177
  • 1
  • 1
  • 9
  • just inject $cookies and $cookies.currentUser should work. – YOU Jun 14 '15 at 11:09
  • @YOU , thanks, it worked. Just another questions on top of it...Is it an acceptable practice to save oauth2 token as cookies in user's browser, similar to what we have done here? It sounds a bit awkward to me, but I am not able to see any other solutions. Thanks. – Jiandong Chen Jun 15 '15 at 09:55
  • I find some quotes on this "Although I have been arguing that cookies are not the best option for authentication, storing an access token in a cookie works just fine. The key is that the server should not consider the cookie to be sufficient for authentication. Instead it should require that the access token be copied from the cookie value into an OAuth header." – Jiandong Chen Jun 15 '15 at 10:32
  • This yeoman generator has good implementation with Authorization bearer for ajax requests - https://github.com/DaftMonk/generator-angular-fullstack – YOU Jun 15 '15 at 12:04

0 Answers0