0

I have an application with a working system of authentication. My issue here is that when a user is connected, he can still access by URL to the login page. I would like to redirect all connected users to home page instead of login page.

So, when someone ask for /authentication/login:

  • If the user is connected, he will be redirected to /home.
  • If nobody is connected, access to authentication/login is open.

Here is my actual working code (it doesn't redirect a connected user to the home page).

angular.module('authentication').config(RouteConfig);
RouteConfig.$inject = ['$routeProvider', 'UserSession'];

function RouteConfig($routeProvider, UserSession) {
    $routeProvider.when('/authentication/login/', {
        templateUrl: 'section/authentication/login.tmpl',
        controller: 'LoginController',
        controllerAs: 'lo'
    });
}

Is that possible to add a conditional statement in the above code? Something like:

$routeProvider.when('/authentication/login/', {
    if(UserSession.getUser() != null) {
        // Go to Home page instead
    } else {
        // Normal way
    }
});
Mistalis
  • 17,793
  • 13
  • 73
  • 97

4 Answers4

3

You can use this.

  $routeProvider.when('$routeProvider.',
    {
      redirectTo: function (routeParams, path, search) {
        console.log(routeParams);
        console.log(path);
        console.log(search);
        return "/";
      }
    })

Try to return the home route conditionally in return statement. For more information please refer to redirectTo in the $routeProvider docs or look here at Thinkster

2

You can understand the following code as:

On every route change, "if a connected user wants to access /authentication/login/, then he will be redirect to /home".

angular.module('authentication').run(function($rootScope, $location, Session) {
    $rootScope.$on("$routeChangeStart", function(event, next, current) {
        if(Session.getUser() != null && next.$$route.originalPath == '/authentication/login/') {
            $location.path("/home");
        }
    });
});

This solution is inspired of @st.never's answer on this question.

Community
  • 1
  • 1
Mistalis
  • 17,793
  • 13
  • 73
  • 97
1

You can do conditional statement like this.

angular.module('authentication').config(RouteConfig);
RouteConfig.$inject = ['$routeProvider', 'UserSession'];

function RouteConfig($routeProvider, UserSession) {
 $routeProvider.when('/authentication/login/', {
    templateUrl: 'section/authentication/login.tmpl',
    controller: 'LoginController',
    controllerAs: 'lo',
    resolve: {
      factory: checkRouting
    }
 });
}


var checkRouting= function ($q, $rootScope, $location) {
  if ($rootScope.userProfile) {
    return true;
  } else {
    var deferred = $q.defer();
    $http.post("/yourUrl",data)
        .success(function (response) {
            $location.path("/login");
            deferred.resolve(true);
        })
        .error(function () {
            deferred.reject();
            $location.path("/home");
         });
    return deferred.promise;
  }
 };
Mistalis
  • 17,793
  • 13
  • 73
  • 97
Rjoydip
  • 66
  • 3
  • Thanks, but provide [**your source**](http://stackoverflow.com/questions/11541695/redirecting-to-a-certain-route-based-on-condition), next time. – Mistalis Dec 20 '16 at 09:34
0

I have a very similar method in one of my Angular applications. In the route's config you can add any key you like and access these keys using the $routeParams service.

So for your example:

function RouteConfig($routeProvider, UserSession) {
    $routeProvider.when('/authentication/login/', {
        templateUrl: 'section/authentication/login.tmpl',
        controller: 'LoginController',
        controllerAs: 'lo',
        blockAuthenticated: true // <-- add a new flag, something like this 
    });
}

Then somewhere else in your application that is most appropriate, e.g. module.run()

function run($location, $routeParams, UserSession)
{
    // redirect the user home if they're logged in and the route doesn't allow
    // authed users

    if ($routeParams.blockAuthenticated && UserSession.getUser() != null) {
        $location.path('/home');
    }
}
Ankh
  • 5,478
  • 3
  • 36
  • 40