sksmsWKd
sksmsWKd

Reputation: 185

Nestjs get user from jwt token returns 401 Unauthorized error

i'm making google-auth site, and i made custom user-decorator and it can't return user.

@UseGuards(AuthGuard('jwt'))
  @Get('profile')
  getProfile(@UserDecorator() user: User) {
    return user;
  }

this is my controller's code

import { createParamDecorator, ExecutionContext } from '@nestjs/common';

export const UserDecorator = createParamDecorator(
  (data: unknown, ctx: ExecutionContext) => {
    const request = ctx.switchToHttp().getRequest();
    console.log(request)
    return request.user;
  },
);

this is my custom user decorator.

my controller's function supposed to return user's info using by JWT (parsing) but this function return's 401 error , so when i delete

@UseGuards(AuthGuard('jwt'))

and my custom decorator 's console.log(request) returns....->>

enter image description here

this kind of something strange object. i checked with f12 button, and jwt token was exists.

Upvotes: 0

Views: 2711

Answers (1)

Alex Dumitru
Alex Dumitru

Reputation: 106

If when you remove:

@UseGuards(AuthGuard('jwt'))

you don't receive the 401 error again, it means that you haven't done the login before you call the API.

Upvotes: 1

Related Questions