由于这个'我非常困惑。属性。
" delete this.user;
"在AuthenticationFactory
中表示。我认为功能" check
"是一种方法,因此它将与' auth
'宾语。但是,没有' user
'属于' auth
'宾语。你能解释一下吗?
此外,在' UserAuthFactory
' (delete AuthenticationFactory.user
,delete AuthenticationFactory.userRole
)
我无法弄清楚" user
"和" userRole
"属性。 AuthenticationFactory
中没有此类属性。
这是我http://thejackalofjavascript.com/architecting-a-restful-node-js-app/
的代码myApp.factory('AuthenticationFactory', function($window) {
var auth = {
isLogged: false,
check: function() {
if ($window.sessionStorage.token && $window.sessionStorage.user) {
this.isLogged = true;
} else {
this.isLogged = false;
delete this.user;
}
}
}
return auth;
});
myApp.factory('UserAuthFactory', function($window, $location, $http, AuthenticationFactory) {
return {
login: function(username, password) {
return $http.post('http://localhost:3000/login', {
username: username,
password: password
});
},
logout: function() {
if (AuthenticationFactory.isLogged) {
AuthenticationFactory.isLogged = false;
delete AuthenticationFactory.user;
delete AuthenticationFactory.userRole;
delete $window.sessionStorage.token;
delete $window.sessionStorage.user;
delete $window.sessionStorage.userRole;
$location.path("/login");
}
}
}
});
答案 0 :(得分:0)
如果再往下看,请看控制器代码:
$scope.login = function() {
var username = $scope.user.username,
password = $scope.user.password;
if (username !== undefined && password !== undefined) {
UserAuthFactory.login(username, password).success(function(data) {
AuthenticationFactory.isLogged = true;
AuthenticationFactory.user = data.user.username;
AuthenticationFactory.userRole = data.user.role;
$window.sessionStorage.token = data.token;
$window.sessionStorage.user = data.user.username; // to fetch the user details on refresh
$window.sessionStorage.userRole = data.user.role; // to fetch the user details on refresh
$location.path("/");
}).error(function(status) {
alert('Oops something went wrong!');
});
} else {
alert('Invalid credentials');
}
};
成功登录后,控制器正在将属性user
和userRole
添加到AuthenticationFactory。