Angular-app, authentication and order of resolvers in (ui-)router

后端 未结 2 834
离开以前
离开以前 2021-01-12 02:14

This questions refers to the angular-app project and the way it authenticates users.

The original implementation guards access to some urls by using resolve clause o

2条回答
  •  旧时难觅i
    2021-01-12 02:29

    I love using the resolver pattern but find it very difficult to do these types of things in angular ui router.

    one solution is to dependency inject the result of the authenticatedUser resolver into the api call resolver you want protected like:

    $stateProvider
        .state('root.tickets', {
            url: '/tickets',
            views: {
                'container@': {
                    templateUrl: 'tickets/tickets-list.tpl.html',
                    controller:'TicketsViewCtrl',
                    resolve:{
                      authenticatedUser: securityAuthorizationProvider.requireAuthenticatedUser,
                      ticketsy: function (Restangular, authenticatedUser) {
                        //Call to tickets must be authenticated
                        return Restangular.all('tickets').getList();
                      }
                    }
                }
            }
    

    This way the resolvers will run in a chain (authenticatedUser -> ticketsy) rather than async all at once.

    I hope this helped.. wish there was a better way of doing it.. thats why im search through stack overflow.

提交回复
热议问题