1

用户在/路由上登录。

  1. 我如何引导他们远离/我的应用程序的视图

    /packages/system/public/views/index.html
    

    换一种看法/app

    /packages/system/public/views/app.html
    
  2. 我希望这个视图/app是安全的,所以只有登录的用户才能访问它。未登录的用户应被发送回/.

4

1 回答 1

1

在 /packages/users/controllers/meanUser.js

  // Register the login() function
  $scope.login = function() {
    $http.post('/login', {
      email: $scope.user.email,
      password: $scope.user.password
    })
      .success(function(response) {
        // authentication OK
        $scope.loginError = 0;
        $rootScope.user = response.user;
        $rootScope.$emit('loggedin');
        if (response.redirect) {
          if (window.location.href === response.redirect) {
            //This is so an admin user will get full admin page
            window.location.reload();
          } else {
            window.location = response.redirect;
          }
        } else {
          // Redirect Here
          $location.url('/');
          $location.url('/articles'); // Will take you to the articles view after login
        }
      })
      .error(function() {
        $scope.loginerror = 'Authentication failed.';
      });
  };

如果您需要在用户尝试访问安全路由而不登录时被重定向到另一个页面,您可以参考 /packages/articles/public/routes/articles.js 中的代码

// This function checks if the user is logged in and redirects to the login page.
var checkLoggedin = function($q, $timeout, $http, $location) {
  // Initialize a new promise
  var deferred = $q.defer();

  // Make an AJAX call to check if the user is logged in
  $http.get('/loggedin').success(function(user) {
    // Authenticated
    if (user !== '0') $timeout(deferred.resolve);

    // Not Authenticated
    else {
      $timeout(deferred.reject);
      $location.url('/login');
    }
  });

  return deferred.promise;
};

// These are your defined routes.
$stateProvider
  .state('all articles', {
    url: '/articles',
    templateUrl: 'articles/views/list.html',
    // This resolve runs the checkLoggedin function as the route is accessed 
    // and redirects if the user isn't logged in.
    resolve: {
      loggedin: checkLoggedin
    }
  });
于 2014-08-28T16:26:30.957 回答