3

I am working on a small project in AngularJs in which I am trying to store username and password into browser cookies.

here is my app.js code. you can see that I have injected ngCookies as dependency injection.

var app = angular.module('myApp', ['ui.router', 'myApp.routes', 'ngCookies']);

following is my login Controller.

app.controller('loginCtrl', function($scope, loginService) {

    $scope.login=function(user){
        loginService.login(user);
    }

});

and here is my service which is calling data for authentication

app.factory('loginService', function($http, $state, $cookies){
    return{
        login:function(user){
            var storedUser = $http.get('data/user.json').
                success(function(data) {

                if(user.email === data.email && user.password === data.password ){
                    //var favoriteCookie = $cookies.get('userEmail');
                    $cookies.put("userEmail", user.email);
                    $cookies.put("password", user.password);    
                    $state.go('dashboard', {});
                } else {
                    alert("Invalid Username or Password");
                }

                })
            }
        }
});

when I run my project I am getting this error. $cookies.put is not a function in console.

note: Version of AngularJs and Angular-cookies are AngularJS v1.2.28

Nilesh Mahajan
  • 3,506
  • 21
  • 34
  • I had a similar question - take a look here http://stackoverflow.com/questions/28971126/angular-and-cookies-cookies-get-is-not-a-function – StudioTime Apr 20 '15 at 13:35

2 Answers2

5

Please use $cookieStore instead of $cookies

Try this

app.factory('loginService', function($http, $state, $cookieStore){
    return{
        login:function(user){
            var storedUser = $http.get('data/user.json').
                success(function(data) {

                if(user.email === data.email && user.password === data.password ){
                    //var favoriteCookie = $cookies.get('userEmail');
                    $cookieStore.put("userEmail", user.email);
                    $cookieStore.put("password", user.password);    
                    $state.go('dashboard', {});
                } else {
                    alert("Invalid Username or Password");
                }

                })
            }
        }
});
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
  • any specific reason behind using $cookieStore and not $ngCookies ?, as I read on angularJs site that Note: The $cookieStore service is deprecated. Please use the $cookies service instead. – Nilesh Mahajan Apr 20 '15 at 13:54
  • @NileshMahajan If you look at https://github.com/angular/angular.js/blob/master/src/ngCookies/cookieStore.js#L36 you'll see a `factory` inside of `ngCookies` model. – Tushar May 07 '15 at 14:51
  • 1
    $cookieStore has been deprecated in favor of $cookie – Mackelito Oct 29 '15 at 21:03
0

instead of $cookie.put

var userdetails = { userEmail: user.email, password: user.password };

$cookies.UserDetails = angular.toJson(userdetails);
Ethaan
  • 11,291
  • 5
  • 35
  • 45
Dominic Scanlan
  • 1,009
  • 1
  • 6
  • 12