DEV Community

Cover image for Bad Request in nestjs, help me!
ALAN MARTINS AGUIAR
ALAN MARTINS AGUIAR

Posted on

2 1

Bad Request in nestjs, help me!

guys, how are you? I have a problem with the refresh token request it says it's a bad request.
My code can store the login token in the database but I can't make the Put request through the refreshToken function.
from what I understand I think my controller is wrong, because I send it through postman and I already tried to send valid, invalid and token that does not exist and all the request gives the same error, in theory it was for the non-existing token to send my response "invalid token" with was handled in refreshToken function.
I did the same procedure, I copied and pasted the same token there in my database.

@Put('refresh')
async refreshToken(@Body() data: RefreshTokenDto) {
return this.tokenService.refreshToken(data.oldToken);
}
async refreshToken(oldToken: string) {
const objToken = await this.findOneRefreshToken(oldToken);
console.log(objToken);

if (objToken) {
  return await oldToken;
} else {
  return new HttpException(
    {
      errorMessage: 'Token inválido',
    },
    HttpStatus.UNAUTHORIZED,
  );
}
Enter fullscreen mode Exit fullscreen mode

}
{
"statusCode": 400,
"message": [
"property oldToken should not exist"
],
"error": "Bad Request"
}
async findOneRefreshToken(hash: string) {
return await this.prisma.token.findUnique({
where: {
hash,
},
});
}
Note: I'm using prism and nestjs

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More