Reputation: 3516
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
Upvotes: 3
Views: 1950
Reputation: 1009
instead of $cookie.put
var userdetails = { userEmail: user.email, password: user.password };
$cookies.UserDetails = angular.toJson(userdetails);
Upvotes: 0
Reputation: 38713
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");
}
})
}
}
});
Upvotes: 5