0

I am currently using version 1.2.28 of AngularJS and am having issues setting and retrieving cookies.

Here's a code snippet:

administrators.createNewAdministrator($stateParams.code).then(
    function (response) {
        $cookies.name = 'Rafael';
        $cookieStore.put('name', 'Rafael');
        var url = url.getRedirectURL(window.location.origin, response.code);
        window.location.href = url;
    }
);

Opening the Application => Cookies pane does not show any of the cookies using either of these methods.

Any ideas as to what may be causing this?

User 5842
  • 2,849
  • 7
  • 33
  • 51
  • The first thing I observe here is that you create a url variable, which depends on itself. Do you have any errors in your console if you temporarily remove the line window.location.href = url; ? – Lajos Arpad Mar 24 '18 at 14:03
  • @LajosArpad, no. Actually, even if I remove those last 2 lines and only keep the cookie logic, the issue persists. – User 5842 Mar 24 '18 at 14:13
  • I was asking specifically about any errors you receive in your browser's console. Removing the redirect was meant to allow you to see the error messages, not to fix the problem. First let's see what the problem is and only then fix it. – Lajos Arpad Mar 24 '18 at 14:27
  • @LajosArpad, good point. There are no Console errors showing up. – User 5842 Mar 24 '18 at 14:30
  • If you put a console.log at the top of your function, like console.log('The function is running'); do you see it in the console? – Lajos Arpad Mar 24 '18 at 14:32
  • @LajosArpad, yes, I do see that. If it helps, this is the only function inside of my `$scope.init = function() { ... }`. I also tried removing the lines outside of this function and the issue remains. – User 5842 Mar 24 '18 at 14:36
  • After you set the cookie values how did you try to access them? – Lajos Arpad Mar 24 '18 at 15:17
  • Should I not be able to see them in the cookies pane in the Dev Tools? – User 5842 Mar 24 '18 at 15:18
  • The best way to check is to run document.cookie in your console. – Lajos Arpad Mar 24 '18 at 15:19
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/167470/discussion-between-user-5842-and-lajos-arpad). – User 5842 Mar 24 '18 at 15:28

1 Answers1

0

For a cookie to be persistent over browser sessions, the expiration date should be set. Unfortunately, this does not seem to be supported with the angularjs $cookie service. See this post

How to set expiration date for cookie in AngularJS

Check the above post if still the issue doesn't solve then check weather redirecting to a particular url is clearing the cookies.

Neha Tawar
  • 687
  • 7
  • 23
  • Thanks for the answer. I’ll look into this, but even without doing the redirect, the cookies are not being set. I cannot access them through the cookies pane in the Dev Tools – User 5842 Mar 24 '18 at 15:17
  • I'm not sure I'm able to use the `expiry` in my version of Angular @Neha Tawar – User 5842 Mar 24 '18 at 15:59