NestJS: AuthGuard Cannot Use Inject in Its canActivate Function
NestJS: AuthGuard Cannot Use Inject in Its canActivate Function
Cause
Recently I was writing a nestjs project. Its UserGuard is very convenient — it is like, when a route handles a request, the verification code can be handled by AuthGuard.
But while writing it, I ran into a problem that took me a very long time to solve, for example the following StaffAuthGuard.ts code:
|
|
When calling the api, when it went through StaffAuthGuard.canActivate, it reported undefined. After adding logs, I found it was this line:
|
|
that errored — here this.authService is undefined, yet it compiles fine. So I added logs in the constructor function; this.authService clearly has a value, so why is it gone by the time it is called?
Solution process
I couldn’t figure it out and struggled for several days; I was about to give up. In between I also used ChatGPT to help me solve the problem, and this is what it gave me:
|
|
Following that comparison, that is basically what I did, but it still didn’t solve the problem.
A few days later, in between I did something else to change my mood, then came back to solve this, continued to google, and found an issue in nestjs’s official issue list: [# Nest can’t resolve dependencies of the AuthGuard (Guard decorator) #893](Nest can't resolve dependencies of the AuthGuard (Guard decorator) · Issue #893 · nestjs/nest · GitHub) — the same as my problem, also unable to reference the injected service in AuthGuard. One answer caught my attention: [Also ensure that you aren’t providing AuthService twice](Nest can't resolve dependencies of the AuthGuard (Guard decorator) · Issue #893 · nestjs/nest · GitHub),
Observing the logs carefully, I found that StaffAuthGuard was initialized twice. The first log had authSerivce with a normal value, the second was undefined. Following this line of thought, I found all the places with StaffAuthGuard,
using the // method, shielding them one by one for testing, and found the cause:
My StaffJwtAuth is defined in the app, while the controller that needs to use this module is in the shared library it references. According to the official docs (I forgot the exact location), if it is in a library like this, you need to define the guard as global in the app, such as:
|
|
That is how I defined it globally, but at the controller of the api I was calling, I also added @UseGuard(StaffJwtAuthGuard), which caused it to be initialized twice, and the second time the parameter was undefined. After removing @UseGuard on the controller, it worked normally.
Thoughts
Afterwards I did a few tests, also putting StaffAuthModule into the library, shielding the global APP_GUARD code, and adding code in the controller. The result was only one initial constructor call, and the authService inside was undefined.
Then I continued to google, and found another one in the official issue: middlewares are loosely coupled from modules, thus it may be better to
use them instead. If you prefer to use guards, you need to guarantee an
access to the AuthService. You can export this service from the AuthModule, and then import this module wherever you want to use your guard., meaning that in my scenario, in the module that uses StaffAuthGuard.StaffAuthService, I need to ensure it can access StaffAuthService. So I added the import of StaffAuthModule to the imports of the module where the controller is located, and the result was OK. The conclusion is that in the module that uses AuthGuard, because AuthGuard is loosely coupled, you need to ensure all its dependencies are imported in the module that calls it
Finally solved. It took several days, and I’m not comfortable unless I solve it; now I’m comfortable, so I can keep tinkering.