how to prevent showing login page after user logged and hit browser back button without using ionic framework

江枫思渺然 提交于 2019-12-30 15:01:19

问题


I am new to anglularjs platform. I am creating a log-in application. I am facing a problem in which all things are going right but got stuck in to manage that if user is already logged in and trying to open same url in different tab it redirect the user directly to dashbord page(dashboard.html), but when i press browser back button, instead of going previous page, it opens login page(login_admin.html). scenario is like as follows:

  1. localhost:8080 --> it opens --> login_admin.html (achieved).

  2. entering credential and on submit --> opens --> dashboard.html (achieved).

  3. on entering same url(localhost:8080) in different tab (suppose previously opened facebook.com) ---> opens --> dashbord.html (achieved).

  4. on pressing browser back button, instead of going facebook.com, it opens login_admin.html (unresolved). suggest some solution please.

my js code is as follows:

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'


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

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.run(["$rootScope",'$location','userModel', function($rootScope,$location,userModel,$window){
    $rootScope.$on('$routeChangeSuccess', function(event, next, current){
        console.log("event: %j",event);
        console.log("next: %j",next);
        console.log('current: %j',current);
      if (next.$$route.authenticated) {
          console.log("next.$$route.authenticated"+next.$$route.authenticated);
          console.log('userModel.getAuthStatus app.run if 1'+userModel.getAuthStatus());


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

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

        }
    }
});
}]);



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

       /*console.log('$scope.dynamic1: %j', $scope);*/
       console.log("response success: %j",response)

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

来源:https://stackoverflow.com/questions/43348123/how-to-prevent-showing-login-page-after-user-logged-and-hit-browser-back-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!