4

Currently, we have a 'Portfolio' tool in beta. Once a user logs in to the main app, if they have been given access to the beta, they can navigate to the Portfolio tool directly, without any additional login. If not, they should be redirected to a Portfolio login page (state is called portfolio.login) where they can login or contact support/sales etc. Right now I have the check in the resolve block, however $state.go('portfolio.login') seems to fetch the right partials, but doesn't render them on screen or navigate to the appropriate URL.

Code:

angular.module('portfolio.manager').config(function ($logProvider, $stateProvider) {
'use strict';

    $stateProvider
    .state('portfolio.manager', {
        url: '/manager',
        resolve: {
            CheckLoggedIn: function ($state, loggedIn) {
                var _loggedIn = loggedIn.checkUser();
                if (!_loggedIn) {
                    $state.go('portfolio.login');
                    console.log('not authorized');
                }
            },
            portfolioAuthService: 'portfolioAuthService',

            User: function(portfolioAuthService){
              return portfolioAuthService.getUser();

            },
            Portfolios: function (User, portfolioManagerService) {
                return portfolioManagerService.getPortfolios();
            }
        },
        views: {
            'main@': {
                templateUrl: 'app/portfolio/manager/portfolio-manager.html',
                controller: 'PortfolioManagerCtrl'
            },
            '[email protected]': {
                templateUrl: 'app/portfolio/manager/partials/no-portfolios.html'
            },
            '[email protected]': {
                templateUrl: 'app/portfolio/manager/partials/create.html'
            }
        }
    })

1 Answer 1

3

I ran in the same problem days ago. Instead of using resolve, I check if the user is logged when state changes, defining run module and listening $stateChangeStart event, then check if the current state required authentication. If so, check if the user is logged in.

angular.module('portfolio.manager').config(function ($logProvider, $stateProvider) {
'use strict';

    $stateProvider
    .state('portfolio.manager', {
        url: '/manager',
        resolve: {
            portfolioAuthService: 'portfolioAuthService',

            User: function(portfolioAuthService){
              return portfolioAuthService.getUser();

            },
            Portfolios: function (User, portfolioManagerService) {
                return portfolioManagerService.getPortfolios();
            }
        },
        data: {
            requiredAuthentication: true
        },
        views: {
            'main@': {
                templateUrl: 'app/portfolio/manager/portfolio-manager.html',
                controller: 'PortfolioManagerCtrl'
            },
            '[email protected]': {
                templateUrl: 'app/portfolio/manager/partials/no-portfolios.html'
            },
            '[email protected]': {
                templateUrl: 'app/portfolio/manager/partials/create.html'
            }
        }
    })

})
.run(run);

  run.$inject = ['$rootScope','$state','loggedIn'];

  function run($rootScope,$state,loggedIn){

    $rootScope.$on('$stateChangeStart',function(e,toState){

      if ( !(toState.data) ) return;
      if ( !(toState.data.requiredAuthentication) ) return;

      var _requiredAuthentication = toState.data.requiredAuthentication;


      if (_requiredAuthentication && !loggedIn.checkUser() ){

        e.preventDefault();
        $state.go('portfolio.login', { notify: false });
        console.log('not authorized');
      }
      return;


    });
  };
Sign up to request clarification or add additional context in comments.

7 Comments

That makes sense, and seems like it should work. I had to take the brackets off of 'notify:false', but even still, no luck. With your code, when an unauthorized user tries to navigate to the portfolio tool, the three portfolio views are requested, the portfolio login view is not requested, and nothing new is rendered.
@DanielBogart ahh ok but, have you defined 'portfolio.login' state? because it looks that my approach works. but you have a problem with your portfolio.login state.
Yeah @levi, I have defined the portfolio.login state. It's in a separate module, the parent portfolio module, but it works fine on it's own. For instance, if i visit domain.com/portfolio, this navigates to the portfolio.login state.
@DanielBogart ok, just check that console.log('not authorized') is printed in console to know if it reaches that point.
@DanielBogart oh sorry, I misspelled the variable in the state. It should be requiredAuthentication : true not requiredAunthentication
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.