1Z10
1Z10

Reputation: 3321

JHipster-Angular 5: using service from another module ends up with Status Code: 401 Unauthorized

I was trying to reuse an existing (generated) service for one of the entities, from within another Lazy-Loaded feature module.

What I did was to just import the module containing the service in the feature module. e.g.

@NgModule({ 
    imports: [ 
        Module1,
        Module2,
        ServiceModule 
    ]
})

While in ServiceModule there is the following:

@NgModule({
    providers: [
        MyService
    ]
})

But when injecting MyService in a component inside my LazyLoadedModule using one of its methods gives me the Status Code: 401 Unauthorized. The authentication method used is JWT tokens, which are handled by JHipster default code, by storing them in the browser. The service does a simple get all call to the back-end API, which I tested with postman and it's working fine.

All of this doesn't happen if I use a service within the lazy loaded module. Any idea of what's missing here?

Upvotes: 1

Views: 403

Answers (1)

1Z10
1Z10

Reputation: 3321

The reason behind this problem, was the lack of http-interceptors in the feature-module. The http-interceptors I'm refferring are the same used in the app.module. So, what fixed this was to copy and paste the following code from app.module.ts to feature.moduele.ts

providers: [ 
        { 
            provide: HTTP_INTERCEPTORS, 
            useClass: AuthInterceptor, 
            multi: true, 
            deps: [ 
                LocalStorageService, 
                SessionStorageService 
            ] 
        }, 
        { 
            provide: HTTP_INTERCEPTORS, 
            useClass: AuthExpiredInterceptor, 
            multi: true, 
            deps: [ 
                Injector 
            ] 
        }, 
        { 
            provide: HTTP_INTERCEPTORS, 
            useClass: ErrorHandlerInterceptor, 
            multi: true, 
            deps: [ 
                JhiEventManager 
            ] 
        }, 
        { 
            provide: HTTP_INTERCEPTORS, 
            useClass: NotificationInterceptor, 
            multi: true, 
            deps: [ 
                Injector 
            ] 
        }
    ] 

Upvotes: 1

Related Questions