#Guard and middleware order

12 messages · Page 1 of 1 (latest)

formal cloak
#

Hello. Guards are executed after all middleware. Is it possible to make it the other way around? I have an auth middleware that checks if the session is valid. I have a rate limit guard. It doesn't make any sense to fetch the database and check if the session is valid, if the user is rate limited. The session checking part should not get executed.

half roost
#

Could you make the auth middleware another guard? There's no way to change the order of enhancers so I'm looking at a different solution for you

formal cloak
half roost
#

Global guards execute before all other guards

formal cloak
#

Well, both auth and throttler guards are global

#

I want to protect every endpoint

half roost
#

How do you bind them? APP_GUARD or useGlobalGuards?

formal cloak
#

APP_GUARD in app.module.ts

#

I'll try to use useGlobalGuards and put them in order, but not sure if that will break the throttler

#

yep, doesn't work that way

#
{
  provide: APP_GUARD,
  useClass: ThrottlerBehindProxyGuard
},
{
  provide: APP_GUARD,
  useClass: CheckSessionGuard
}

Found a solution, I just put them in order in app.module.ts like this. It works, thank you for the help

half roost
#

Ah, just saw this again, yeah was gonna mention that the order you add them to providers sets the order they run in