I have folder structure like this. But i can't inject token service/token module to guard.
This is how code works;
- I send post request to user endpoint (for create user) ✅
- I am checking additional info at guard. ✅
- If token is not provided,return 401 ✅
- if token provided and not valid return 401 ❌
Information
Normally its working if i send message from controllers, services. But it doesn't work if message coming from guard
here is code:
Create-guard.ts
@Injectable()
export class AuthRequired implements NestMiddleware {
constructor(
@Inject(ACCOUNT_SERVICE_NAME) private readonly accountQueue: ClientProxy,
) {}
async use(req: Request, res: Response, next: (error?: NextFunction) => void) {
const { isRoot } = req.body;
const token = this.extractToken(req);
if (isRoot === 'false') {
if (!token) {
return res.status(HttpStatus.UNAUTHORIZED).send({
message: 'You must be logged in to perform this action',
});
}
const isValid = this.accountQueue.send({ cmd: 'isTokenValid' }, token);
return isValid;
}
next();
}
private extractToken(req: Request) {
const authHeader = req.headers.authorization;
if (!authHeader) {
return null;
}
const token = authHeader.split(' ')[1];
return token;
}
}
Account.module.ts
@Module({
imports: [
ClientsModule.register([
{
name: ACCOUNT_SERVICE_NAME,
transport: Transport.RMQ,
options: {
urls: BROKERS,
queue: ACCOUNT_QUEUE_NAME,
queueOptions: {
durable: false,
},
},
},
]),
SequelizeModule.forFeature([Account]),
],
controllers: [AccountController],
providers: [AccountService],
})
Token.controller.ts
@MessagePattern({ cmd: 'isTokenValid' })
async isTokenValidHandler(token: string) {
console.log('DEBUGGGG');
return this.tokenService.isTokenValid(token);
}