#Error when I try an auth / login request between two micro services (users and authentications)

5 messages · Page 1 of 1 (latest)

versed stirrup
#

my authentication.module.ts :

import { Module } from '@nestjs/common';
import { AuthService } from './authentication.service';
import { JwtModule } from '@nestjs/jwt';
import { ClientsModule, Transport } from '@nestjs/microservices';
import { AuthenticationController } from './authentication.controller';
import { LocalStrategy } from './local.strategy';
import { JwtStrategy } from './jwt.strategy';
import { jwtConstants } from './constants';
import { AuthGuard } from './local-auth.guard';

@Module({
    imports: [ClientsModule.register([{
      name: 'USER_CLIENT',
      transport: Transport.TCP,
      options: {
        host: '0.0.0.0',
        port: 3000,
      }
    }]), JwtModule.register({
      global: true,
      secret: jwtConstants.secret,
      signOptions: { expiresIn: '60s' }
    })
    ],
    providers: [AuthService, LocalStrategy, JwtStrategy, AuthGuard],
    controllers: [AuthenticationController],
    exports: [AuthService],
  
  })
  export class AuthModule {}

http://localhost:8001/authentication/login POST :

request :

{ "username": "yourUsername", "password": "yourPassword" }

response :

{ "statusCode": 500, "message": "Internal server error" }

#

Error when I try an auth / login request between two micro services (users and authentications)

#

// PART 2 \ :

I have a micro services architecture which pass through my api-gateway service : authentication and users.

I have set up the authentication methods in my authentication.service.ts exactly as explained in the NestJs doc.

I have a signIn method that takes username and pass and returns a token :

export class AuthService {
  constructor(
    @Inject('USER_CLIENT')
    private client: ClientProxy,
    private jwtService: JwtService
    ) {}

    async signIn(username: string, pass: string): Promise<{ access_token: string }> {
      // Envoi de la requête au microservice USERS et attente de la réponse
      const user = await this.client.send<any>('get_user_by_username', { username }).toPromise();
      
      // Vérification des informations de l'utilisateur
      if (!user || user.password !== pass) {
        throw new UnauthorizedException();
      }
  
      // Création du payload JWT
      const payload = { sub: user.userId, username: user.username };
  
      // Retourne le token JWT signé
      return {
        access_token: await this.jwtService.signAsync(payload),
      };
    }
  }
glass vale
#

Is it the microservice that's throqwing the error and the gateway is propogating it as a 500?

versed stirrup
#

the error throws in the authentication micro service (container)