Impossible to use cookies in my application


#1

The truth is not that I’m doing wrong, but always get the same error

Error: [$injector:unpr] Unknown provider: $$cookieReaderProvider <- $$cookieReader <- $cookies <- auth <- $cookies

I am using this way all:

<script src="js/angular-cookies.js"></script>

App js :

angular.module('SharedServices', ['ionic','ngCordova','ngMap','ngCookies'])

.run(function($ionicPlatform,$ionicSideMenuDelegate,$rootScope) {
$ionicPlatform.ready(function() {
$rootScope.login = true;
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if(window.cordova && window.cordova.plugins.Keyboard) {
  cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if(window.StatusBar) {
  StatusBar.styleDefault();
}
document.addEventListener('touchstart', function (event) {
        // workaround for Android
        if ($ionicSideMenuDelegate.isOpenLeft()) {
            event.preventDefault();
        }
    });
 });
});


 angular.module('ionicApp', ['ionic','SharedServices','ionicLazyLoad','ngCordova','ionic.contrib.frostedGlass','ngMap','ngCookies'])
.factory("auth", function($cookies,$cookieStore,$location){
return{
    login : function(username, password)
    {
        //creamos la cookie con el nombre que nos han pasado
        $cookies.username = username,
        $cookies.password = password;
        //mandamos a la home
        $location.path("/home");
    },
    logout : function()
    {
        //al hacer logout eliminamos la cookie con $cookieStore.remove
        $cookieStore.remove("username"),
        $cookieStore.remove("password");
        //mandamos al login
        $location.path("/login");
    },
    checkStatus : function()
    {
        //creamos un array con las rutas que queremos controlar
        var rutasPrivadas = ["/home","/dashboard","/login"];
        if(this.in_array($location.path(),rutasPrivadas) && typeof($cookies.username) == "undefined")
        {
            $location.path("/login");
        }
        //en el caso de que intente acceder al login y ya haya iniciado sesión lo mandamos a la home
        if(this.in_array("/login",rutasPrivadas) && typeof($cookies.username) != "undefined")
        {
            $location.path("/home");
        }
    },
    in_array : function(needle, haystack)
    {
        var key = '';
        for(key in haystack)
        {
            if(haystack[key] == needle)
            {
                return true;
            }
        }
        return false;
    }
}
})

Which is not bad for all this, though I still examples is always the same error.


#2

Hey @sioesi,

Could you provide a codepen that contains this error? You could fork this project and add in your cookie code and then post your link here, that’ll help us solve it quicker.

You could use https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular-cookies.min.js for your cookie file.


#3

Import cookie.min.js and everything was solved, thank you very much friend !. Sorry if I upset but I’m having a problem with the plugin chamber cordova, know something about it?

Greetings and thanks for your help!


#4

Plugin chamber cordova? I don’t know what that is. Here are the docs for cordova plugins. Good luck!


#5

Sorry, write badly. Watch this post here


#6

@sioesi was having the same issue.

I think it has something to do with angular-cookies 1.4 that bower tries to install even when u tell it to use angular version 1.3.13 (the angular version ionic uses)

I downgraded to version 1.3.15 from the CDN that @seanhill recommended and its all working now thanks to this thread :+1: