简体   繁体   English

如果用户未登录,如何防止用户访问下一页?

[英]how to prevent user to access next page if the user is not logged in?

This is my main js file, where I want a condition that user must be logged in to access the welcome page, if user is not logged in then it must redirect it to login page by default, but in my code the form is submitting and redirecting to welcome page but does not implement desired result. 这是我的主要js文件,在这里我希望有一个条件,要求用户必须登录才能访问欢迎页面,如果用户未登录,则默认情况下必须将其重定向到登录页面,但是在我的代码中,表单正在提交并重定向到欢迎页面,但未实现期望的结果。

here is the main.js file 这是main.js文件

var app = angular.module("myApp", ["ngRoute"])

                    .config(function($routeProvider, $locationProvider) {
                        $locationProvider.hashPrefix('');
                        $routeProvider
                                .when("/",{
                                    templateUrl : "pages/login.html",
                                    controller : "loginController"
                                })
                                .when("/welcome",{
                                    templateUrl: "pages/welcome.html",
                                    controller:"welcomeController"
                                })

                            })
                .controller("loginController", function($scope, $location, $rootScope){
                            $scope.login = function() {
                                var user = $scope.username;
                                var password = $scope.password;
                                /*var result = $scope.username + $scope.password;
                                console.log(result);*/
                                if (user == "admin" && password == 'admin'){
                                    $rootScope.loggedIn = true;
                                    $location.path('/welcome');
                                } else {
                                    alert("INVALID CREDENTIALS");
                                }
                            }
                    })

                .controller('welcomeController', function($scope){
                    $scope.message = "welcome here";
                })
                .controller('logoutController', function($scope,$location){
                      $scope.logOut = function(){
                          $location.path('/');
                      }

And here is my welcome.html 这是我的welcome.html

<span>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Molestias officia praesentium dolore alias, enim nostrum doloribus natus nisi itaque quos ullam. Tenetur aut fugit qui minus, cupiditate, rem est unde.Lorem ipsum dolor sit amet, consectetur adipisicing elit. Suscipit facilis excepturi laboriosam voluptates in doloremque ad, impedit. Nam sunt similique vitae voluptatem fugit molestias, quod accusantium alias nulla dolores ad.</span>

<div class="logout">
    <h4><span style="float: right; color:red; cursor: pointer;" ng-click="logOut()">Logout?</span></h4>
</div>

and login.html here 和login.html在这里

Enter user name: <input type="text" name="user" ng-model="username"><br>
Enter Password: <input type="password" name="password" ng-model="password"><br>
<input type="submit" name="submit" value="submit" ng-click="login()">

In your welcomeController , you need to check if the user is logged in or not. 在您的welcomeController ,您需要检查用户是否已登录。

Try following: 请尝试以下操作:

.controller('welcomeController', function($scope, $rootScope, $location) {
    if (!$rootScope.loggedIn)
        $location.path('/');

    $scope.message = "welcome here";
});

You're doing your check in the wrong controller. 您正在检查错误的控制器。 do like this: 这样做:

 .when("/welcome",{
                   templateUrl: "pages/welcome.html",
                   controller:"welcomeController",
                   resolve: {
                        userLoggedIn: function($rootScope, $location){
                              if(!$rooteScope.loggedIn){
                                   $location.path('/');
                               } 
                        }
                     }
                   })

I think this is a better way to achieve what you want... resolve execute before your welcome become active...so, you avoid to load the welcome page and only then redirect if the user is not logged in 我认为这是实现所需目标的更好方法...在welcome进入活动之前resolve执行...因此,避免加载welcome页面,只有在用户未登录时才进行重定向

ps: (maybe is missing some semicolon in my code but you can handle this) ps :(也许我的代码中缺少一些分号,但是您可以处理)

You can use routechange event 您可以使用routechange事件

angular.module(...).config( ['$routeProvider', function($routeProvider) {...}] ).run( function($rootScope, $location) {

// register listener to watch route changes
$rootScope.$on( "$routeChangeStart", function(event, next, current) {
  if ( $rootScope.loggedUser == null ) {
      // not going to #login, we should redirect now
      $location.path( "/login" );

  }         
});

}) })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM