Is there a way to redirect users back to the original page after they login? For example, if a user is on a post like www.example.com/post/435
and needs to log in to "like/comment" on the post, how can I automatically redirect them back to that specific post after successful login?
$http.post('/sessionHandler/login', $scope.form).
success(function(data){
history.back(); // This approach may not work as expected, especially if the user's previous path was on another site or a new tab in the browser.
}).error(function(err){
$scope.errorMessage = err;
});
I've also come across examples using $locationChangeStart
, but I'm having trouble understanding how it works. Here's what I have so far:
$http.post('/sessionHandler/login', $scope.form).
success(function(data){
$rootScope.$on("$locationChangeStart", function (event, next, current) {
$location.path();// How can I access the previous URL where the user came from?
// How do I use event, next, and current to determine the last path within my site,
// and ensure it's not an external website?
});
})
Any help would be greatly appreciated!