Nest: GraphQL auth guard custom decorator undefined

Created on 18 Mar 2020  路  3Comments  路  Source: nestjs/nest

Bug Report

I created a new nest 7 project and followed the authentication documentation, but for the graphql the decorator threw an error of undefined when using custom route decorator

Current behavior

GraphQL guard Custom decorator doesn't work.

Input Code


https://github.com/rained23/nestjs-graphql-decorator

Get JWT

curl -X POST http://localhost:3000/auth/login -d '{"username": "john", "password": "changeme"}' -H "Content-Type: application/json"
{
    whoAmI{
        username
    }
}
TypeError: undefined is not a function
    at exports.CurrentUser.common_1.createParamDecorator (/home/me/graphauth/dist/auth/user.decorator.js:4:53)
    at shared_utils_1.isFunction.args (/home/me/graphauth/node_modules/@nestjs/core/helpers/context-utils.js:32:28)
    at resolveParamValue (/home/me/graphauth/node_modules/@nestjs/core/helpers/external-context-creator.js:141:31)

Expected behavior

Should be working because it is from the official documentation

Environment


[Nest Platform Information]
platform-express version : 7.0.0
passport version         : 7.0.0
graphql version          : 7.0.6
common version           : 7.0.0
core version             : 7.0.0
jwt version              : 7.0.0


For Tooling issues:
- Node version: 11.15.00  
- Platform:  Linux

Others:

needs triage

Most helpful comment

Thanks for reporting! Just fixed: https://docs.nestjs.com/techniques/authentication#graphql (update your decorator's code):

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;
  },
);

All 3 comments

Thanks for reporting! Just fixed: https://docs.nestjs.com/techniques/authentication#graphql (update your decorator's code):

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;
  },
);

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hackboy picture hackboy  路  3Comments

rlesniak picture rlesniak  路  3Comments

2233322 picture 2233322  路  3Comments

VRspace4 picture VRspace4  路  3Comments

artaommahe picture artaommahe  路  3Comments