AngularJS $ location.path () not working for returnUrl

I have a SPA using AngularJS. I just added security / authentication and everything works fine except for redirection after login if exists in the query string returnUrl.

I have code in my application that will be redirected to my login if there is no user authentication. For example, if a user tries to access http://localhost:55841/#/group/15(for which authentication is required), he is redirected to the login route with the following URL:

http://localhost:55841/#/login?returnUrl=%2Fgroup%2F15

Here is my login method, which should be redirected to the returnUrl route, if it exists upon successful login:

var login = function (credentials) {
    return $http.post(baseUrl + 'api/login', credentials).then(function (response) {
        //do stuff
        var returnUrl = $location.search().returnUrl;
        if (returnUrl) {
            $location.path(returnUrl);
            //$location.path('/group/15');
        }
        $location.path('/');
    });
};

, returnUrl /group/15, , URL-:

http://localhost:55841/#/?returnUrl=%2Fgroup%2F15

+4
1

, .

var login = function (credentials) {
    return $http.post(baseUrl + 'api/login', credentials).then(function (response) {
        $rootScope.currentUser = response.data;
        $rootScope.$broadcast('currentUser', response.data);
        var returnUrl = $location.search().returnUrl;
        if (returnUrl) {
            console.log('Redirect to:' + returnUrl);
            $location.path(decodeURI(returnUrl)); // <- executed first, but not redirect directly.
            //$location.path('/group/15');
        } else { //else :)
           console.log('Redirect returnUrl not found. Directing to "/".');
           $location.path('/'); // <- only redirect if no returnUrl isset/true
       }
    }, function (response) {
        $rootScope.currentUser = null;
        $rootScope.$broadcast('currentUser', null);
        return $q.reject(response);
    });
};

: URL- "returnUrl". , /. .

+2

All Articles