Lightweight Angular module for access to cookies

Overview

angular-cookie Build Status

Lightweight Angular module for access to cookies

Installation

You can install angular-cookie via bower

bower install angular-cookie

Other way to install angular-cookie is to clone this repo into your project with this command

git clone [email protected]:ivpusic/angular-cookie.git

Then you need to include angular-cookie.js script into your project

<script src="/path/to/angular-cookie.min.js"></script>

or include beautified version with

<script src="/path/to/angular-cookie.js"></script>

To rebuild min.js version run

grunt build

Run example

To run example execute following commands

git clone [email protected]:ivpusic/angular-cookie.git
cd angular-cookie
npm -g install bower
npm -g install grunt
npm install
bower install
grunt

After this, go at 127.0.0.1:9001/example in your browser, and you will see running example of angular-cookie.

Usage

First you need to inject ipCookie into your angular module.

var myApp = angular.module('myApp', ['ipCookie']);

And now, for example if you want to use it from your controller

myApp.controller('cookieController', ['$scope', 'ipCookie', function($scope, ipCookie) {
  // your code here
}]);

General signature of main function is

ipCookie(key, value, options);

Set

To create a cookie use

ipCookie(key, value);

The value supports strings, numbers, booleans, arrays and objects and will be automatically serialized into the cookie.

You can also set some additional options, like number of day when a cookie expires

ipCookie(key, value, { expires: 21 });

If you want to specify a cookie path use

ipCookie(key, value, { path: '/some/path' });

If you want to set the encode or decode functions use

ipCookie(key, value, { encode: function (value) { return value; } });

Get

To get all cookies use

ipCookie();

If you want to get a cookie with a specific key use

ipCookie(key);

If any cookie was not found, function returns undefined.

The returned value will be automatically deserialized.

If you want to pass an options object, you will need to also pass 'undefined' as the second parameter:

ipCookie(key, undefined, {decode: function (value) { return value; }};

Remove

And if you want to remove a cookie use

ipCookie.remove(key);

To remove a cookie on a specific path use

ipCookie.remove(key, { path: '/some/path/' });

Options

Domain

domain: 'example.com'

The domain tells the browser to which domain the cookie should be sent. If you don't specify it, it becomes the domain of the page that sets the cookie.

Path

path: '/'

The path gives you the chance to specify a directory where the cookie is active.

Expires

expires: 21

Each cookie has an expiry date after which it is trashed. If you don't specify the expiry date the cookie is trashed when you close the browser.

Expiration Unit

expirationUnit: 'minutes'

Allows you to set the expiration time in hours, minutes, seconds, or ``milliseconds`. If this is not specified, any expiration time specified will default to days.

Secure

secure: true

The Secure attribute is meant to keep cookie communication limited to encrypted transmission, directing browsers to use cookies only via secure/encrypted connections.

Encode function

encode: function (value) { return value; }

The method that will be used to encode the cookie value (should be passed when using Set).

Default: encodeURIComponent.

Decode function

decode: function (value) { return value; }

The method that will be used to decode extracted cookie values (should be passed when using Get).

Default: decodeURIComponent.

Notes

TODO

  • Add tests
Comments
  • Module naming

    Module naming

    Hey,

    Thanks for great extension.

    Module naming is incoherent. Project called angular-cookie, which looks like it's official angular team extension. If i want to import it to angular module I need to use ivpusic.cookie but then to use it I have to use ipCookie, is it possible to normalize all names to something like:

    ipCookie or ivpusicCookie?

    Thanks

    opened by nirth 12
  • Uncaught URIError: URI malformed

    Uncaught URIError: URI malformed

    The following value of a cookie is throwing error:

    %7B%22mediaUrl%22%3A%22http%3A//ireland1.cutvmanifest.api.ottcloudservices.com/smooth/M1_SS/1407418200-1407421680/manifest%22%2C%22laUrl%22%3A%22http%3A//license.totalmovie.com/playready/rightsmanager.asmx%22%2C%22programId%22%3A%22LSC_SAN_19_10497509%22%2C%22programName%22%3A%22Az%20%E9let%20megy%20tov%E1bb%22%2C%22channelId%22%3A%22LCH_SAN_19_2%22%2C%22channelName%22%3A%22M1%22%2C%22mode%22%3A%22normal%22%2C%22castAvailable%22%3Atrue%2C%22chromecast%22%3A%22%22%2C%22castingDevice%22%3A%22%22%2C%22playing%22%3Atrue%7D
    

    The line is:

    value = decodeURIComponent(cookie.substring(pos + 1));
    
    opened by JobaDiniz 9
  • Add this repo to npm too please

    Add this repo to npm too please

    Hi there

    I've fallen a little bit in love with Browserify recently. I find this package incredible useful (thank you) and it's a little bit irritating having to faff about pulling it in with bower.

    Can you add this into npm so that I can use it with Browserify? Pretty please.

    Thanks

    S

    opened by mrsimonemms 3
  • Add support for milliseconds as an expirationUnit option

    Add support for milliseconds as an expirationUnit option

    Might as well ... actually eliminated a bunch of unit test refactoring in another project that would otherwise have to be done. Let me know if you have any questions @ivpusic !

    opened by booleanbetrayal 3
  • Optional change encode and decode functions

    Optional change encode and decode functions

    Add the ability to change the encode and decode functions by passing those functions in the option parameter.

    Getter example: ipCookie('cookieName', undefined, { decode: angular.identity, encode: angular.identity });

    Setter example: ipCookie('cookieName', 'http://test.com/#/', { decode: angular.identity, encode: angular.identity });

    opened by radotzki 3
  • Bug with reading cookie set by other libraries

    Bug with reading cookie set by other libraries

    I have experienced strange behaviour on some browsers. The library suddenly stopped working with type error URIError: malformed URI sequence Stack trace: cookieFun@............./angular-cookie.js:77:1

    The problem comes with wrong assumption that all cookies for that domain is created only by this library and are encoded with encodeURIComponent. In some cases there are some other cookies for the domain created by another scripts which is not encoded on the same way.

    The problem could be fixed by replacing:

    value = decodeURIComponent(cookie.substring(pos + 1)); /* 77 row */

    with:

    try { value = decodeURIComponent(cookie.substring(pos + 1)); } catch (e) { value = cookie.substring(pos+1); }

    The whole idea to encode cookies seems to be wrong because you can accidentally damage other third party cookies for instance: google analytics cookie.

    opened by Hypnoticbg 3
  • Encode string values as strings

    Encode string values as strings

    When a cookie is read that contains a string constisting of digits only, the call of JSON.parse will parse the String as a Number. If the number is larger than the JavaScript Number type the returned value is interpolated.

    Example:

    ipCookie('example', '545654545664564564564554554545');
    console.log(ipCookie('example')); // 5.4565454566456454e+29
    
    opened by tmair 3
  • fix breaking typo and encode strings

    fix breaking typo and encode strings

    I tried using {expires: numDays} in my project with angular-cookie. However, it appears that it doesn't work!! I just found out that it doesn't work because the ; is not encoded. Therefore, none of the options are actually appended to the cookie string. The browser will completely curtail any un-encoded characters and any characters afterward. My fix here should address the encoding problem. What are your thoughts?

    opened by dillonforrest 3
  • Trash cookie when browser closes

    Trash cookie when browser closes

    Hey man,

    I am currently setting a cookie and I am expecting it to be deleted when I quit the browser and re-open, but the cookie seems to persist.

    This doesn't seem like the expected behaviour as per the docs.

    Is this correct? Could you tell me how it gets deleted?

    Thanks!

    opened by peduarte 3
  • bug fixes & renamed module

    bug fixes & renamed module

    renamed module: ng and $ are "reserved" for angular modules options is optional return undefined instead of false return undefined if specified cookie not found, instead of object removed unnecessary functions...

    opened by matjaz 3
  • Using angular-cookie to share cookies across domains

    Using angular-cookie to share cookies across domains

    I have 2 AngularJS applications running on my localhost. I haven't transferred it yet to a real domain.

    Am I right that if I set domain on both applications to localhost, angular-cookie should be able to share the cookies to my 2 applications?

    opened by c0debreaker 2
  • Fix options object being modified

    Fix options object being modified

    If the user does ipCookie.remove and has its expires options updated to an expiry date, then the next time they save, that options object has been replaced by the expired date.

    Not modifying user’s options would be best.

    opened by mingyugao 0
  •  domain can't work

    domain can't work

    I just want all website '**.mujishi.com' can user cookie and I wrote the following: ipCookie('token', token, {expires: 20},{domain:'.mujishi.com'}); and I log in the a.mujishi.com and I can see the cookie in chrome-resources but if I open b.mujishi.com ,I can't get the cookie ,I'm wondering why?Did I write wrong or something?

    opened by MAYDAY1993 0
  • Firefox not sets the cookie

    Firefox not sets the cookie

    http://prntscr.com/awr60b

    Firefox is not adding cookies. Not receive any error, it just does not set. It's all right as the privacies.

    ipCookie ('globals', $rootScope.globals, {path: '/', expires: 0.1});

    opened by gregoryderner 0
  • Upgrade old dependencies

    Upgrade old dependencies

    Hi,

    Could you please upgrade dependencies of your module (specifically bower, but not only). Old versions have known high severity vulnerabilities and may cause problems.

    opened by piotrd 0
  • Cookie Value Modified when trying to read from Cookie

    Cookie Value Modified when trying to read from Cookie

    Right now, if you try and read a cookie that's a large number such as 4461381106811793314, when you fetch it using ipCookie('cookie_name') it returns it as 4461381106811793400.

    Is it possible to have it not do that?

    opened by royteamcmp 0
Owner
Ivan Pusic
Ivan Pusic
A lightweight vanilla ES6 cookies and local storage JavaScript library

?? CrumbsJS ?? A lightweight, intuitive, vanilla ES6 fueled JS cookie and local storage library. Quick Start Adding a single cookie or a local storage

null 233 Dec 13, 2022
sessionStorage API which gracefully degrades to window.name & cookies when not available

sessionstorage The sessionStorage API is amazing and super useful when you need to store data temporarily in the browser. We used to abuse cookies for

null 22 Jul 25, 2022
Load and save cookies within your React application

react-cookie Universal cookies for React universal-cookie Universal cookies for JavaScript universal-cookie-express Hook cookies get/set on Express fo

Reactive Stack 2.4k Dec 30, 2022
A Vue.js plugin for manipulating cookies

vue-cookie A Vue.js plugin for manipulating cookies tested up to Vue v2.0.5 Installation Install through npm npm install vue-cookie --save Include in

Alf 819 Dec 8, 2022
An AngularJS module that gives you access to the browsers local storage with cookie fallback

angular-local-storage An Angular module that gives you access to the browsers local storage Table of contents: Get Started Video Tutorial Development

Gregory Pike 2.9k Dec 25, 2022
A javascript based module to access and perform operations on Linode object storage via code.

Linode Object Storage JS Module A javascript based module to access and perform operations on Linode object storage via code. Code Guardian Installing

Core.ai 3 Jan 11, 2022
AngularRoutingWebApp - Web App dimostrativa per spiegare e riepilogare i concetti basi di Angular.ts

RoutingApp This project was generated with Angular CLI version 11.0.2. Development server Run ng serve for a dev server. Navigate to http://localhost:

Gianmarco Polizzi 1 Jan 3, 2022
A lightweight clientside JSON document store,

.____ .__ .__ | | _____ __ _ ______ ____ | |__ _____ |__|_______ | | \__ \

Brian LeRoux 2.1k Nov 24, 2022
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
A simple, lightweight JavaScript API for handling browser cookies

JavaScript Cookie A simple, lightweight JavaScript API for handling cookies Works in all browsers Accepts any character Heavily tested No dependency S

null 20.2k Jan 3, 2023
A lightweight vanilla ES6 cookies and local storage JavaScript library

?? CrumbsJS ?? A lightweight, intuitive, vanilla ES6 fueled JS cookie and local storage library. Quick Start Adding a single cookie or a local storage

null 233 Dec 13, 2022
An npm package for demonstration purposes using TypeScript to build for both the ECMAScript Module format (i.e. ESM or ES Module) and CommonJS Module format. It can be used in Node.js and browser applications.

An npm package for demonstration purposes using TypeScript to build for both the ECMAScript Module format (i.e. ESM or ES Module) and CommonJS Module format. It can be used in Node.js and browser applications.

Snyk Labs 57 Dec 28, 2022
a cobbled together alternative UI to launchdarkly, allowing read/write access via LD API access token

discount-launchdarkly a cobbled together alternative UI to launchdarkly, allowing read/write access via LD API access token setup make sure you have a

null 9 Oct 19, 2022
sessionStorage API which gracefully degrades to window.name & cookies when not available

sessionstorage The sessionStorage API is amazing and super useful when you need to store data temporarily in the browser. We used to abuse cookies for

null 22 Jul 25, 2022
Load and save cookies within your React application

react-cookie Universal cookies for React universal-cookie Universal cookies for JavaScript universal-cookie-express Hook cookies get/set on Express fo

Reactive Stack 2.4k Dec 30, 2022
A Vue.js plugin for manipulating cookies

vue-cookie A Vue.js plugin for manipulating cookies tested up to Vue v2.0.5 Installation Install through npm npm install vue-cookie --save Include in

Alf 819 Dec 8, 2022
Fully undetected stealer (steals wallets, passwords, cookies, modifies discord client like piratestealer etc.)

doenerium (CURRENTLY NOT WORKING BECAUSE OF OBFUSCATION; fixing later) Fully undetected stealer (0/67) I obfuscated this to prevent my stuff being ski

doener 363 Nov 12, 2022