TSlegaitis TSlegaitis - 2 months ago 9
AngularJS Question

AngularJS Logout using $http then redirect

Right so I have a little dilemma here :).

I'm working on a project that is built with angularjs and Laravel. Login is done with laravel which is out of AngularJs "scope", should I say.

So in Front End I do not see that page.

What I'm trying to do is use $http get method to get /logout (handled by laravel again). On success redirect user to /login.

So what I was thinking of doing is:

$rootScope.logout = function () {

$http.get('/logout', function (response) {
console.log('redirect');
$window.location.href('/login');
});
};


But doesn't seem to work for some reason. Suggestions? /logout clears all sessions etc, which is out of my "jurisdiction" :)

Btw I'm also using UI-router.

Answer

This location.replace should solve your issue.

$rootScope.logout = function () {

    $http.get('/logout', function (response) {
        console.log('redirect');
        var origin = $window.location.origin;
        $window.location.replace(origin + '/login');
    });
};

UPDATE : In case you have defined a route for logout page.

$rootScope.logout = function () {

    $http.get('/logout', function (response) {
        console.log('redirect');
        $state.go('logout');
    });
};

But keep in mind, you should have logout state in such case.

Comments