0

I am trying to implement login system using graphql in nest js.

I have login EP - works fine - returs Bearer token.

But I have query to get current user:

    import { CurrentUser } from '../auth/user.decorator'
.
.
.
    @Query()
    @UseGuards(JwtAuthGuard)
    public async me(@CurrentUser() user: any) {
        console.log(user)
        return { email: 'foo' }
    }

With custom decorator (user.decorator.ts):

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

export const CurrentUser = createParamDecorator(
  (data: unknown, context: ExecutionContext) => {
    const ctx = GqlExecutionContext.create(context);
    return ctx.getContext().req.user;
  },
);

When I run me I am getting an error "host.getArgByIndex is not a function", - it throw insinde CurrentUser decorator by ctx.getContext()... what is wrong?

Is my Import correct?

It it directly from nestjs doc: https://docs.nestjs.com/techniques/authentication

ciyada8549
  • 61
  • 4

1 Answers1

0

You should check the version of Nest as Jay McDoniel's comment. If it is version 6, change the createParamDecorator to implement as the following

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

export const CurrentUser = createParamDecorator(
  (data, [root, args, ctx, info]) => ctx.req.user,
);