简体   繁体   中英

restrict the redirection to login page once the user has already logged in even if he press the back button of browser in angular?

i want to make a login page which check the conditon and user is redirected to home page afterwards , now i want to implement a condition that when user has visited home page and he clicks on the back button or browser he should'nt be able to again visit login page . i tried to set a variable isLogged in as false and after checking the username and password make that variable to true and store that in local storage and use it like flag , but i am not able to properly get where's m wrong .

here is my app.js

var validationApp = angular.module('validationApp',['ngRoute','ngStorage']);
var loggedIn = false;
validationApp.run(function($rootScope, $localStorage){
  //var loggedIn = false;
    //if(!loggedIn) {
        storage = window.localStorage;
        //storage.setItem("loggedIn", true);
        //console.log(storage.getItem("loggedIn"));
//    $rootScope.$storage = $localStorage.$default({
//        loggedIn: false
//    })
//    }
});

validationApp.controller('mainController', function($scope,loginservice,$localStorage) {
    $scope.dologin = function () {
        //storage.setItem("loggedIn" ,true);
        //loginsucess = storage.getItem("loggedIn");

        if(loggedIn == false) {
            //console.log(loginsucess);
            if ($scope.userForm.$valid) {
                loginservice.login($scope.user.email, $scope.user.password);
            }
            storage.setItem("loggedIn", true);
        }
    }
});

i tried to put a check condition into my routes also and now i am not able to get my home page even after valid credentials .

validationApp.config(

    function($routeProvider) {
        $routeProvider
            .when('/', {
                templateUrl: 'main.html',
                controller: 'mainController'
            })
            .when('/home', {
                templateUrl: 'home.html',
                resolve:{
                    "check":function($location){
                        if(storage.getItem(loggedIn) == true)
                        {
                            alert(loggedIn);
                            $location.path("/home");
                        }
                        else
                        {
                            $location.path("/main");
                        }
                    }
                },
                controller: 'mainController'
            })
            .otherwise({
                redirectTo: '/'
            });
});


validationApp.service('loginservice',function($location)
{
    this.login = function(username, password){
        console.log(username,password);

        if(username == "ank@gmail.com" && password=="1234")
        {
            //alert('thank you for submitting your form');
            $location.path("/home");

            //console.log(storage.getItem("loggedIn"));
        }
        else
        {
          //alert("invalid username and password");
            $location.path("/main");

        }
    }
});

Please see working example: http://plnkr.co/edit/46O0znC5HFDE4cYXSm5h?p=preview

Stored data in cookies in login function as follows,

$cookies.userinfo = {
    'id': 1,
    'name': 'pqr'
};

And on logout remove that data -

delete $cookies.userinfo;

then check for 'angular.isDefined($cookies.userinfo)' (userinfo is cookie name which given at the time of storing data in it) if find then redirect it to your page which you want to see after login. ie

app.run(function ($cookies) {
      $rootScope.$on("$routeChangeStart", function () {
        if (angular.isDefined($cookies.userinfo)) {
            $location.path("/pathname");
        }

      });
});

See if this serves your purpose

var validationApp = angular.module('validationApp',  ['ngRoute','ngStorage']);

    var loggedIn = false;
    validationApp.run(function($rootScope, $localStorage, $location){

       storage = window.localStorage;
       $rootScope.$on("$routeChangeStart", function (evt, next, current) {

           if(storage.getItem(loggedIn) == true) {
                if (next.$$route.originalPath == '/login')
                    $location.path('/home');
           }
           else
             $location.path('/login');
       });


});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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