在angularjs中创建登录应用程序

时间:2017-04-08 13:44:20

标签: javascript angularjs cookies angular-services angular-factory

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

  1. 如果用户按下浏览器的后退按钮,则不应再次进入登录页面。仅当redirected to login page被按下时才应logout button

  2. store credentials in cookies,以便{}} hitting same url中的用户为different tab时,应通过检查dashbord直接打开stored cookies }。

  3. 我的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;
    })
    

0 个答案:

没有答案