Search code examples
node.jsnestjsmicroservicespassport.jsmonorepo

Microservices autorization issue(passport js)


I recently started a course on microservices and ran into a problem with the application of the passport. The problem is that my strategy apparently does not see my token and does not call the validate method. After reading the articles, I realized that this may be due to the fact that my token is not valid and his time ran out, but I checked this case. I will leave all the photos of my services, the structure below. I will also add a link to git. Thank you in advance

JWTStrategy seems like this: enter image description here

My login method(rmq): enter image description here

App module: enter image description here

RMQ service: enter image description here

problem like this:enter image description here link to github: click .


Solution

    1. For JWTAuthGuard to work, you need to add a call to the fromAuthHeaderAsBearerToken function in jwt.straragy.ts line 11:
    // from:
    jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken,
    //to:
    jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken()
    
    1. add import JwtStratagy to api app.module.ts providers array
    providers: [JwtStrategy]
    
    1. And the decorator @UserId() does not work correctly. It returns a user object but should return user.id from request.user. For @UserId decorator to correct work, you need to change line 4 in user.decorator.ts:
    // from:
    return ctx.switchToHttp().getRequest()?.user;
    //to:
    return ctx.switchToHttp().getRequest()?.user?.id;
    

    My issue with same answer: https://github.com/AlariCode/6-microservices-demo-1/issues/1

    My implementation: https://github.com/Rudimo/nx-nest-microservices-rmq-demo