Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular - Logout redirect route

Tags:

angularjs

I am trying to create a route that clears the users session and redirects them back to the root homepage.

.config(function config( $routeProvider, $stateProvider ) {

    $routeProvider.
        when('/logout', {resolve: {redirect: function(Session){
            Session.clear();
            return "/home";
        }}});

I'm obviously doing something wrong here, as calling

$location.path("/logout");

... ignores the function and redirects back to the default route. I've tried adding console.log statements to the function to see if it is being called.

Am I using the redirect function incorrectly?

like image 256
Hanpan Avatar asked Oct 28 '13 12:10

Hanpan


People also ask

How to redirect after logout?

To redirect the user after they log out from a specific application, you must add the URL used in the returnTo parameter of the redirect URL to the Allowed Logout URLs list in the Settings tab of your Auth0 application that is associated with the CLIENT_ID parameter.


2 Answers

Have you considered putting your logout logic in a separate controller? Would be a little cleaner, more robust, & make redirection more straightforward. Like so:

function LogoutController($location) {
    Session.clear();
    $location.path('/home');
}

The your route is:

when('/logout', {
  template: '', //A template or templateUrl is required by AngularJS, even if your controller always redirects.
  controller: 'LogoutController'
}).    
like image 177
angularijustmether Avatar answered Sep 23 '22 15:09

angularijustmether


I had the same issue and what I did instead was create a logout function in my navigationController that gets hit when the URL is clicked

 <li><a href="" ng-click="logout()" target="_self">Log Out</a></li>

And in my navigationController:

$scope.logout = function () {
                localStorage.clearAll();
                window.location = '/logout';
            };

I'm running ASP.NET behind Angular so I needed the browser (not angular) to route to /logout which is mapped in ASP.NET config (does a few other session clean ups and redirects to authentication app)

Hope this helps

like image 37
tofortier Avatar answered Sep 21 '22 15:09

tofortier