I
I
iluxa18102019-05-20 15:59:54
Angular
iluxa1810, 2019-05-20 15:59:54

How to wait for application initialization?

There is CanActive, which stands on a certain route in order to prevent transitions along forbidden routes depending on roles.
So, this CanActive fires before the event of accessing the user data request API...
I tried to initialize the user through APP_INITIALIZER:

export function appInitializerFactory(authService: AuthorizationService, permissionsService: PermissionService) {
     console.log("appInitializerFactory1");
     return async () => {
          authService.authorize().pipe(tap(() => {
               permissionsService.currentUser = authService.currentUser;
               console.log("appInitializerFactory2");
          })).toPromise();
     };
}

Yes, the factory starts before CanActive, but CanActive does not wait for completion and the appInitializerFactory2 message occurs later...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shvets, 2019-05-20
@iluxa1810

return forgot
and async is not needed there
and format normally

export function appInitializerFactory(
  authService: AuthorizationService, 
  permissionsService: PermissionService,
) {
  return () => authService.authorize().pipe(
    tap(() => permissionsService.currentUser = authService.currentUser),
  )
    .toPromise();
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question