0

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;
})
Sunny
  • 577
  • 6
  • 20
  • could you please create a snippet of this example – Dhiraj Apr 13 '17 at 10:46
  • Possible duplicate of [Remove page from history, so "back" will work properly](http://stackoverflow.com/questions/26441698/remove-page-from-history-so-back-will-work-properly) – Claies Apr 13 '17 at 10:49
  • the duplicate I posted has alternatives that don't use ionic framework, notably the [answer](http://stackoverflow.com/a/36348261/2495283) from Rebecca – Claies Apr 14 '17 at 00:06

0 Answers0