duane2364 2016-11-30 11:14
浏览 29

AngularJS身份验证无法正常工作

I'm trying to add authentication to my golang/angular app. the backend authentication works fine and logs that the user has logged in but the angular part is not working as expected, it doesn't set the username as when it successfully logs in and changes page, the username is not set.

app.js

blog.controller('LoginCtrl', function($scope, $http, $window, authService){ 
      $scope.login = function({
          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;
              }
          });
      };
});

blog.factory('authService', function(username, password, callback){
    var service = {};
    var username = "";

    $http.post('/login', {Username : username, Password: password}).
    success(function(response, status){
        service.setCredentials(username, password);
        callback(response, status);
    });

    service.setCredentials = function(username, password){
              username = username;
    };

    service.getCredentials = function(){
             return username;
    };
      return service;
});

blog.controller('NavCtrl', function($scope, $rootScope, authService){
    $scope.isAuth = (authService.getCredentials() != "");
    console.log("username: " + authService.getCredentials());
    $scope.username = authService.getCredentials();
});
  • 写回答

1条回答 默认 最新

  • douwei1128 2016-12-21 16:19
    关注

    The problem is that your authService doesn't have the Login method you're calling from your controller:

    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;
                  }
              });
          };
    });
    

    Instead, you need to define your Login method within your factory like so:

    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;
    });
    

    Note that I've also changed the username function parameters to loginUsername as it was shadowing the variable you where trying to assign to. This was causing the username value to be undefined.

    评论

报告相同问题?