1

Angular 5 error. I am having trouble with getting user details on page refresh from browser. I am trying to load the data using APP_INITIALIZER but getting below error -

compiler.js:19390 Uncaught Error: Provider parse errors: Cannot instantiate cyclic dependency! ApplicationRef ("[ERROR ->]"): in NgModule AppModule in ./AppModule@-1:-1

Below is my configuration app.module.ts

export function initUserFactory(userService: UserService) {
       return  () => { userService.initUser()};
    }
let INITIAL_PROVIDERS = [ {provide: HTTP_INTERCEPTORS,useClass: RequestAuthInterceptor, multi: true },
                           UserService,
                           MessageService,                         
                           AuthGuard,
                           AdminAuthGuard,
                           NotificationService,
                           NgxPermissionsService,
                          {
                            provide: APP_INITIALIZER,
                            useFactory: initUserFactory,
                            deps: [UserService],
                            multi: true
                          },

                          AuthService,

                        ];
@NgModule({ imports:[//required imports],declarations: INITIAL_COMPONENTS,
  providers: INITIAL_PROVIDERS,
  bootstrap: [AppComponent]
})

UserService

 initUser() {
    const promise = this.http.get<APIResponse>(this.apiurl+ServiceConstants.REFRESH_TOKEN).toPromise()
    .then(response => {

      if(response.data.result != null && response.data.result.token != null  ){
          //refresh toekn
          let accessToken = response.data.result.token;
          localStorage.setItem('auth_token', accessToken); 
            return this.getUserInfo().toPromise()
              .then(user => {
                this.currentUser = user;
              });
          }

    })
    .catch(() => null);
    return promise;
  }

Or any other suggestions where I can resolve logged in user before rending the view! Because the user is not resolved the auth guard redirect it to login page on page refresh. Any help is appreciated! Thanks in advance

maddie
  • 39
  • 11
  • I had a router dependency on the RequestAuthInterceptor - constructor(private router : Router). Removing it solved the issue but I am not sure why! – maddie Jan 19 '18 at 09:26
  • Even removing `private router: Router` it still giving the error `cyclic dependency! HTTP_INTERCEPTORS` – Pablo Mar 28 '18 at 14:21

0 Answers0