简体   繁体   中英

angularjs redirection after login

may I know how to redirect user to where they originally at after login ? say i have a post wwww.example.com/post/435 and it required user to log in in order to "like/comment" the post, so after the user login how can i redirect user back to the post ? i have tried this

 $http.post('/sessionHandler/login',$scope.form).
      success(function(data){
        history.back(); // bring user back to previous path
      }).error(function(err){
        $scope.errorMessage = err;
      });

however this is not really effective, if a user previous path is other site or a browser new tab it will redirect the user back to where they are.

i also found some example by using $locationChangeStart but i'm having some difficulties to understand how it works this is what i have so far for $locationChangeStart

$http.post('/sessionHandler/login',$scope.form).
      success(function(data){
   $rootScope.$on("$locationChangeStart", function (event, next, current) {
      $location.path();// how to access to previous url where the user from ?
                       // how to use event, next and current to get the user last
                      // path on my site, and how to determine the last site is
                      // on my site but not other website ?  
    });

      })

your help is appreciated !!

May not be the best answer, but I use a cookie for this - so whenever I'm redirecting to a new location, I'll use $cookies.loginDestination = $location.path() , then whenever a user logs in, I use something like:

var loginDestination = $cookies.loginDestination || '/';
$cookies.loginDestination = null;
$location.path(loginDestination);

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