Javascript AngularJS - 如果用户登录,则显示/隐藏导航项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26920217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 23:31:34  来源:igfitidea点击:

AngularJS - show/hide nav items if user is logged in

javascriptangularjsnode.jsexpress

提问by Ryan Tuck

I have a single-page AngularJS app, working with Express, node.js, and MongoDB via Mongoose. Using Passport for user management/authentication.

我有一个单页 AngularJS 应用程序,通过 Mongoose 使用 Express、node.js 和 MongoDB。使用 Passport 进行用户管理/身份验证。

I'd like the navbar items to change based on whether a user is logged in or not. I'm having trouble figuring out how to implement it.

我希望导航栏项目根据用户是否登录而改变。我在弄清楚如何实现它时遇到了麻烦。

I find out if a user is logged in through an httprequest:

我确定用户是否通过http请求登录:

server.js

服务器.js

app.get('/checklogin',function(req,res){
  if (req.user)
    res.send(true);
  else
    res.send(false);

On the front end, I have a NavControllercalling this using Angular's $httpservice:

在前端,我NavController使用 Angular 的$http服务调用它:

NavController.js

导航控制器.js

angular.module('NavCtrl',[]).controller('NavController',function($scope,$http) {

    $scope.loggedIn = false;

    $scope.isLoggedIn = function() {

      $http.get('/checklogin')
        .success(function(data) {
          console.log(data);
          if (data === true)
            $scope.loggedIn = true;
          else
            $scope.loggedIn = false;
        })
        .error(function(data) {
          console.log('error: ' + data);
        });
    };
};

In my nav, I am using ng-showand ng-hideto determine which selections should be visible. I am also triggering the isLoggedIn()function when the user clicks on the nav items, checking whether the user is logged in during each click.

在我的导航中,我正在使用ng-showng-hide确定哪些选择应该是可见的。isLoggedIn()当用户点击导航项目时,我也会触发该功能,检查用户在每次点击期间是否登录。

index.html

索引.html

<nav class="navbar navbar-inverse" role="navigation">
  <div class="navbar-header">
    <a class="navbar-brand" href="/">Home</a>
  </div>
  <ul class="nav navbar-nav">
    <li ng-hide="loggedIn" ng-click="isLoggedIn()">
      <a href="/login">Login</a>
    </li>
    <li ng-hide="loggedIn" ng-click="isLoggedIn()">
      <a href="/signup">Sign up</a>
    </li>
    <li ng-show="loggedIn" ng-click="logOut(); isLoggedIn()">
      <a href="#">Log out</a>
    </li>
  </ul>
</nav>

Problem

问题

There are other places in my app where the user can log in/out, outside of the scope of the NavController. For instance, there's a login button on the login page, which corresponds to the LoginController. I imagine there's a better way to implement this across my entire app.

我的应用程序中还有其他地方可供用户登录/注销,超出了 NavController 的范围。例如,登录页面上有一个登录按钮,它对应于 LoginController。我想有一种更好的方法可以在我的整个应用程序中实现这一点。

How can I 'watch' whether req.useris trueon the back end and have my nav items respond accordingly?

我怎么能“看”是否req.usertrue在后端,并已我的导航项目作出相应的反应?

回答by Nicolás Straub

you can use $rootScopeto share info across the entire app:

您可以用来$rootScope在整个应用程序中共享信息:

.controller('NavController',function($scope,$http, $rootScope) {

    $scope.isLoggedIn = function() {

      $http.get('/checklogin')
        .success(function(data) {
          console.log(data);
          $rootScope.loggedIn = data;
        })
        .error(function(data) {
          console.log('error: ' + data);
        });
    };
};

now you can change the value of loggedInfrom other places in your app by accessing $rootScope.loggedInin the same way it is done in the code above.

现在,您可以通过以与上面代码中相同的方式loggedIn进行访问来更改应用程序中其他位置的值$rootScope.loggedIn

With that said, you should abstract the relevant code into a service and a directive. This would allow you to have one central place to handle, log in, log out, and the state of $rootScope.loggedIn. If you post the rest of the relevant code I could help you out with a more concrete answer

话虽如此,您应该将相关代码抽象为服务和指令。这将允许您有一个中央位置来处理、登录、注销和$rootScope.loggedIn. 如果您发布其余相关代码,我可以为您提供更具体的答案

回答by guilhebl

You can broadcast that event when user logs in successfully. And no need to keep polling your server if user is logged in you can keep a variable in memory that tells if you have a valid session or not. You can use a token-based authentication which is set in the server side:

您可以在用户成功登录时广播该事件。如果用户已登录,则无需继续轮询您的服务器,您可以在内存中保留一个变量,告诉您是否有一个有效的会话。您可以使用在服务器端设置的基于令牌的身份验证:

services.factory('UserService', ['$resource',                                        
  function($resource){

    // represents guest user - not logged
    var user = {
        firstName : 'guest',
        lastName : 'user',
        preferredCurrency : "USD",
        shoppingCart : {
            totalItems : 0,
            total : 0
        },                                                  
    };

    var resource = function() {
        return $resource('/myapp/rest/user/:id', 
            { id: "@id"}
    )};

    return {
        getResource: function() { 
            return resource;
        },

        getCurrentUser: function() {
            return user;
        },

        setCurrentUser: function(userObj) {
            user = userObj;
        },

        loadUser: function(id) {
            user = resource.get(id);
        }
    }

  }]);

services.factory('AuthService', ['$resource', '$rootScope', '$http', '$location', 'AuthenticationService', 
  function ($resource, $rootScope, $http, $location, AuthenticationService) {
    var authFactory = {
        authData: undefined       
    };

    authFactory.getAuthData = function () {
        return this.authData;
    };

    authFactory.setAuthData = function (authData) {
        this.authData = {
            authId: authData.authId,
            authToken: authData.authToken,
            authPermission: authData.authPermission
        };
        // broadcast the event to all interested listeners
        $rootScope.$broadcast('authChanged');
    };

    authFactory.isAuthenticated = function () {
        return !angular.isUndefined(this.getAuthData());
    };

    authFactory.login = function (user, functionObj) {
        return AuthenticationService.login(user, functionObj);          
    };

    return authFactory;
}]);

services.factory('AuthenticationService', ['$resource',
  function($resource){
    return $resource('/myapp/rest/auth/', 
            {},
            {
              'login': { method: "POST" }
            }               
    );
  }]);          

services.factory('authHttpRequestInterceptor', ['$injector',  
 function ($injector) {
    var authHttpRequestInterceptor = {
        request: function ($request) {
            var authFactory = $injector.get('AuthService');
            if (authFactory.isAuthenticated()) {
                $request.headers['auth-id'] = authFactory.getAuthData().authId;
                $request.headers['auth-token'] = authFactory.getAuthData().authToken;
            }
            return $request;
        }
    };

    return authHttpRequestInterceptor;
}]);

controller:

控制器:

controllers.controller('LoginCtrl', ['$scope', '$rootScope', 'AuthService', 'UserService', 
  function LoginCtrl($scope, $rootScope, AuthService, UserService) {
    $scope.login = function () {
        AuthService.login($scope.userInfo, function (data) {
            AuthService.setAuthData(data);
            // set user info on user service to reflect on all UI components
            UserService.setCurrentUser(data.user);
            $location.path('/home/');               
        });
    };

    $scope.isLoggedIn = function () {
        return AuthService.isAuthenticated();
    }

    $scope.user = UserService.getCurrentUser();         
}])

回答by yotamsha

You can add user's session data inside the index.html using some templating library like EJS.

您可以使用一些模板库(如 EJS)在 index.html 中添加用户的会话数据。

Just add ejs middleware:

只需添加 ejs 中间件:

var ejs = require('ejs');
// Register ejs as .html.
app.engine('.html', ejs.__express);

And then, when returning the index.html render the session data into the response.

然后,当返回 index.html 时,将会话数据呈现到响应中。

            res.render( "/index.html", {
                session : {
                    user_data : JSON.stringify(req.user)
                }

            });

You'll now have access to this data in the index.html, now you need to load it into Angular app. I used preload-resourceexample, but you can use your own way.

您现在可以访问 index.html 中的这些数据,现在您需要将其加载到 Angular 应用程序中。我使用了preload-resource示例,但您可以使用自己的方式。

回答by Jonathan

You can also use $localStorage if you want the login to persist outside of the current session. I've found this library has been super helpful for these types of situations. (https://github.com/grevory/angular-local-storage)

如果您希望登录名在当前会话之外持续存在,您也可以使用 $localStorage。我发现这个库对这些类型的情况非常有帮助。( https://github.com/grevory/angular-local-storage)