Неправильная сериализация данных grpc в nestjs приложении
Я пытаюсь передать данные на клиент (в моём случае Postman) посредством grpc в NestJS-приложении. Метод Verify и его реализация представлены ниже.
auth.proto:
syntax = "proto3";
package auth;
service AuthService {
rpc Login (LoginDto) returns (UserAndToken) {}
rpc Signup (SignupDto) returns (UserAndToken) {}
rpc Verify (VerifyDto) returns (VerifyResponse) {}
}
message LoginDto {
string email = 1;
string password = 2;
}
message SignupDto {
string email = 1;
string password = 2;
}
message VerifyDto {
string accessToken = 1;
}
message UserAndToken {
User user = 1;
string accessToken = 2;
}
message VerifyResponse {
User user = 1;
}
message User {
optional string id = 1;
string email = 2;
}
auth.contoller.ts:
import { Controller, UseFilters } from '@nestjs/common'
import { GrpcMethod } from '@nestjs/microservices'
import { VerifyDto } from './dto/verify.dto'
import { AuthService } from './auth.service'
import { ExceptionFilter } from 'src/rpc-exception.filter'
@Controller('auth')
export class AuthController {
constructor(readonly authService: AuthService) {}
@UseFilters(new ExceptionFilter())
@GrpcMethod('AuthService', 'Verify')
async verify(verifyDto: VerifyDto) {
return this.authService.verify(verifyDto)
}
}
auth.service.ts:
import { Inject, Injectable } from '@nestjs/common'
import { Repository } from 'typeorm'
import { User } from './entities/user.entity'
import { USER_REPOSITORY } from 'src/constants'
import { JwtService } from '@nestjs/jwt'
import * as bcrypt from 'bcrypt'
import { RpcException } from '@nestjs/microservices'
import * as grpc from '@grpc/grpc-js'
import { VerifyDto } from './dto/verify.dto'
import { JwtPayload } from './interfaces/jwtPayload'
@Injectable()
export class AuthService {
constructor(
@Inject(USER_REPOSITORY) private userRepository: Repository<User>,
private jwtService: JwtService
) {}
async verify({ accessToken }: VerifyDto) {
const { email, password } = await this.jwtService.verifyAsync<JwtPayload>(accessToken)
const user = await this.userRepository.findOneBy({ email })
if (!user) throw new RpcException({ message: 'USER_NOT_FOUND', code: grpc.status.NOT_FOUND })
const isMatchPassword = password === user.password
if (!isMatchPassword)
throw new RpcException({ message: 'PASSWORD_DOES_NOT_MATCH', code: grpc.status.PERMISSION_DENIED })
return {
user: {
id: user.id,
email
}
}
}
}
В ответ на Verify запрос получаю:
{
"email": "",
"id": "\n$16e48eb0-f4ac-4c68-9d8d-8b4d5fcc8ae4\u0012\[email protected]"
}
При этом методы Signup и Login реализованы очень похожим образом и работают прекрасно. Чувствую проблема в какой-то маленькой детали.
Я новичок в grpc, так что буду рад любым замечаниям и доработкам