Javascript AngularJS身份验证未按预期工作
我正在尝试向我的golang/angular应用程序添加身份验证。后端身份验证工作正常,记录用户已登录,但角度部分未按预期工作,它未将用户名设置为成功登录并更改页面时的用户名,未设置用户名 app.jsJavascript AngularJS身份验证未按预期工作,javascript,angularjs,go,Javascript,Angularjs,Go,我正在尝试向我的golang/angular应用程序添加身份验证。后端身份验证工作正常,记录用户已登录,但角度部分未按预期工作,它未将用户名设置为成功登录并更改页面时的用户名,未设置用户名 app.js 问题是authService没有从控制器调用的登录方法: blog.controller('LoginCtrl', function($scope, $http, $window, authService){ $scope.login = function({
问题是authService没有从控制器调用的登录方法:
blog.controller('LoginCtrl', function($scope, $http, $window, authService){
$scope.login = function({
// Well there's your problem!
authService.Login($scope.username, $scope.password, function(response, status){
if(status == 200){
authService.setCredentials($scope.username, $scope.password);
$window.location.href="/";
} else {
$scope.invalidLogin = true;
}
});
};
});
相反,您需要在工厂内定义登录方法,如下所示:
myApp.factory('authService', function(){
var service = {};
var username = "";
service.setCredentials = function(loginUsername, password){
username = loginUsername;
};
service.getCredentials = function(){
return username;
};
service.Login = function(loginUsername, password, callback){
$http.post('/login', {Username : loginUsername, Password: password}).
success(function(response, status){
service.setCredentials(loginUsername, password);
callback(response, status);
});
}
return service;
});
请注意,我还将username函数参数更改为loginUsername,因为它隐藏了您试图分配给的变量。这导致用户名值未定义。问题在于authService没有从控制器调用的登录方法:
blog.controller('LoginCtrl', function($scope, $http, $window, authService){
$scope.login = function({
// Well there's your problem!
authService.Login($scope.username, $scope.password, function(response, status){
if(status == 200){
authService.setCredentials($scope.username, $scope.password);
$window.location.href="/";
} else {
$scope.invalidLogin = true;
}
});
};
});
相反,您需要在工厂内定义登录方法,如下所示:
myApp.factory('authService', function(){
var service = {};
var username = "";
service.setCredentials = function(loginUsername, password){
username = loginUsername;
};
service.getCredentials = function(){
return username;
};
service.Login = function(loginUsername, password, callback){
$http.post('/login', {Username : loginUsername, Password: password}).
success(function(response, status){
service.setCredentials(loginUsername, password);
callback(response, status);
});
}
return service;
});
请注意,我还将username函数参数更改为loginUsername,因为它隐藏了您试图分配给的变量。这导致用户名值未定义。对此我不确定,但当状态为200且您正在设置凭据时,您可能无法访问$scope,因此它们最终未定义,请尝试从后端返回用户名,并从response.username或response.token访问它,或者你需要的任何东西。谢谢你的回复。在那里试过,但没有成功。response.username确实获得了用户名,但是一旦页面发生更改,变量仍然没有定义。我注意到,现在尝试更改这两行:service.setCredentials=function(username,password){this.username=username;};service.getCredentials=function(){返回this.username;};还是不走运。我注意到的一件事是console.log(“username”+authService.getCredentials())总是显示未定义。这不应该是我在blog.factory中设置的空字符串吗?请检查是否与“username”混淆-您将其用作函数参数和局部变量。我不确定这一点,但当状态为200且您正在设置凭据时,您可能无法访问$scope,因此它们最终未定义,尝试从后端返回用户名,并从response.username或response.token或您需要的任何内容访问它。感谢您的响应。在那里尝试过,但没有成功。response.username确实获得了用户名,但是一旦页面发生更改,变量仍然没有定义。我注意到,现在尝试更改这两行:service.setCredentials=function(username,password){this.username=username;};service.getCredentials=function(){返回this.username;};还是不走运。我注意到的一件事是console.log(“username”+authService.getCredentials())总是显示未定义。这不应该是我在blog.factory中设置的空字符串吗?检查是否与“username”混淆-您将其用作函数参数和局部变量。