#Interceptor for nested lazy loaded routes

15 messages · Page 1 of 1 (latest)

magic vessel
magic vessel
#

Well I guess because it is a service doing the request the interceptor is not being applied. How would I fix that?

fiery pivot
#

So an interceptor fires when an Http call is made to any endpoint and will in your case add the authorization header to the request.
The reason why the interceptor fires in your 'protected' component is because an api call is made ( api/showuser/me). In the User component you don't do any call so why do you expect it to do something?

magic vessel
#

in the protected component I do the request directly using this.http.get(...) but when I use the service (getUsers -> api call to /api/get/users) in the users component the interceptor doesn't do anything. Wouldn't it have also the same interceptor as the parent or the service is in a different scope? (I have resolved the problem, though I'm not too sure if it is the best way. I put the provider on app.module and it works)

#

I'll push the new changes to develop branch

fiery pivot
#

So providing the AuthInterceptor on app.module does not seem strange to me. When you provide an interceptor on root level (app.module) this interceptor will be pluggedIn on every call (even for lazy loaded modules). As this is what you want, this is the way to go.

magic vessel
#

But from what I saw it didn't apply to the signup or login component. Btw I'm probably thinking it works in some way when i doesn't

fiery pivot
magic vessel
#

Wouldn't this interceptor also apply to the signup and login component?

fiery pivot
#

I would think so

magic vessel
#

wait I'm dumb

#

it does not apply because of this condition:

if (idToken) {
      req = req.clone({
        headers: req.headers.set('Authorization', 'Bearer ' + idToken),
      });
    }
#

if there's not token no header is set

#

so the interceptor doesn't do anything in this case

#

I'll close the post since the issue has been resolved