1

我是 angularjs 的新手,并试图在 angularjs 中创建一个登录应用程序,其中流程应如下所示: 1.当用户输入usernemepassword按下输入按钮时,它应该被重定向到dashboard page.

  1. 如果用户按下浏览器的后退按钮,它不应该再次进入登录页面。它应该redirected to login page只有logout button被按下。

  2. 这样storecredentials in cookies如果用户hitting same urldifferent tab,它应该dashbord通过检查直接打开stored cookies

js code的情况如下,first two condition我上面提到的实现了。但third one我到现在都无法做到。

var app=angular.module('myApp', ['ngRoute','ngCookies']);
console.log("in appnew.js")

app.config(function($routeProvider,$locationProvider) {
    $locationProvider.html5Mode(true);
    $routeProvider
    .when('/',{
        templateUrl:  'login_admin.html',
        controller: 'userController'


    })
    .when('/dashboard',{
        templateUrl: 'dashboard.html',
        controller: 'userController',
        authenticated: true


    })
    .when('/logout',{
        templateUrl: 'logout_admin.html',
         controller: 'userController',
         authenticated: true

    })
    .otherwise({
       redirectTo: "/"
    });
});


app.factory('userModel', function($http,$cookies,$location){
   var userModel={};
   userModel.login= function(loginfrm){
    data={
      jeeb_no: loginfrm.jeeb_no,
      //password: loginfrm.jeeb_no
    };
    console.log("loginfrm"+loginfrm);
    return $http.post("http://-----------:8080/admin_login", data).
    success(function(response){
        $cookies.put('auth',response);
       console.log('response'+response);

    }).
    error(function(response){
        console.log("response"+response);
    });
   };
    userModel.getAuthStatus=function(){
        var status = $cookies.get('auth');
        if(status){
            return true;
        }
        else{
            return false;
        }
    };
    userModel.doUserLogout = function(){
        $cookies.remove('auth');
    }
    return userModel;
});


app.run(["$rootScope",'$location','userModel', function($rootScope,$location,userModel){
    $rootScope.$on('$routeChangeStart', function(event, next, current){
        console.log("event "+event);
        console.log("next "+next);
        console.log("current "+current);
      if (next.$$route.authenticated) {
          console.log("next.$$route.authenticated"+next.$$route.authenticated);

        if (!userModel.getAuthStatus()) {
            console.log("getAuthStatus"+!userModel.getAuthStatus);
            $location.path('/');
        }
      }

      if (next.$$route.originalPath =='/') {
        console.log("next.$$route.originalPath  "+next.$$route.originalPath);
        /*console.log("current.$$route.originalPath  "+$route.current.$$route.originalPath);*/
        if (userModel.getAuthStatus()) {
            console.log("current "+current);
            console.log("next "+next);
            console.log("current.$$route.originalPath"+current.$$route.originalPath);
            $location.path(current.$$route.originalPath);
        }
      }
      /*if(current.$$route.originalPath == '/dashboard' && current.$$route.originalPath == '/logout'){

      }*/
    });
}]);

app.controller('userController',function($scope,$location,userModel){

    angular.extend($scope,{
      login: function(adminfrm){
        var data={
        jeeb_no: $scope.admin.name,
        password: $scope.admin.password
    };


      userModel.login(data).then(function(){
        $location.path('/dashboard');
      });
      },
      logout: function(){
        userModel.doUserLogout();
        $location.path('/');
      }
    });


});

app.factory('userModel', function($http,$cookies,$location){
   var userModel={};
   userModel.login= function(loginfrm){
    data={
      jeeb_no: loginfrm.jeeb_no,
      //password: loginfrm.jeeb_no
    };
    console.log("loginfrm"+loginfrm);
    return $http.post("http://---------:8080/admin_login", data).
    success(function(response){
        $cookies.put('auth',response);
       console.log('response'+auth);

    }).
    error(function(response){
        console.log("response"+response);
    });
   };
    userModel.getAuthStatus=function(){
        var status = $cookies.get('auth');
        if(status){
            return true;
        }
        else{
            return false;
        }
    };
    userModel.doUserLogout = function(){
        $cookies.remove('auth');
    }
    return userModel;
})
4

0 回答 0