nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
66.8k stars 7.55k forks source link

graphql-ws authentication issue #12733

Closed caferyukseloglu closed 10 months ago

caferyukseloglu commented 10 months ago

Is there an existing issue for this?

Current behavior

https://docs.nestjs.com/graphql/subscriptions According to documentation this must be correct on the issue which says "Cannot read properties of undefiend (reading 'authorization')" I see the logged user data from below console.log however getting error like above.

GraphQLModule.forRoot<ApolloDriverConfig>({
  driver: ApolloDriver,
  subscriptions: {
    'graphql-ws': {
      onConnect: (context: Context<any>) => {
        const { connectionParams } = context;
        const authToken = connectionParams?.Authorization;
        const token = authToken.split(' ')[1];
       const decoded = jwt.verify(token, 'default_secret');
        (context.extra as any).ueser = decoded;
      },
    },
  },
  context: ({ extra }) => {
    console.log(extra.user)
  },
});

Minimum reproduction code

-

Steps to reproduce

No response

Expected behavior

Must have worked without an error.

Package

Other package

graphql-ws

NestJS version

10.0.0

Packages versions

{
  "name": "my-api",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "private": true,
  "license": "-",
  "scripts": {
    "build": "nest build",
    "format": "prettier --write \"src/*/.ts\" \"test/*/.ts\"",
    "start": "nest start",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/main",
    "lint": "eslint \"{src,apps,libs,test}/*/.ts\" --fix"
  },
  "dependencies": {
    "@apollo/server": "^4.9.3",
    "@nestjs/apollo": "^12.0.9",
    "@nestjs/common": "^10.0.0",
    "@nestjs/core": "^10.0.0",
    "@nestjs/graphql": "^12.0.9",
    "@nestjs/jwt": "^10.1.1",
    "@nestjs/mapped-types": "*",
    "@nestjs/passport": "^10.0.2",
    "@nestjs/platform-express": "^10.0.0",
    "@prisma/client": "^5.3.0",
    "@types/passport-jwt": "^3.0.9",
    "bcrypt": "^5.1.1",
    "dotenv": "^16.3.1",
    "express": "^4.18.2",
    "firebase-admin": "^11.11.0",
    "graphql": "^16.8.0",
    "graphql-subscriptions": "^2.0.0",
    "graphql-upload": "^13.0.0",
    "graphql-ws": "^5.14.2",
    "jsonwebtoken": "^9.0.2",
    "passport": "^0.6.0",
    "passport-facebook": "^3.0.0",
    "passport-google-oauth20": "^2.0.0",
    "passport-jwt": "^4.0.1",
    "reflect-metadata": "^0.1.13",
    "rxjs": "^7.8.1"
  },
  "devDependencies": {
    "@nestjs/cli": "^10.0.0",
    "@nestjs/schematics": "^10.0.0",
    "@nestjs/testing": "^10.0.0",
    "@types/bcrypt": "^5.0.0",
    "@types/express": "^4.17.17",
    "@types/graphql": "^14.5.0",
    "@types/graphql-upload": "^8.0.11",
    "@types/jest": "^29.5.2",
    "@types/node": "^20.3.1",
    "@types/passport-google-oauth20": "^2.0.12",
    "@types/supertest": "^2.0.12",
    "@typescript-eslint/eslint-plugin": "^5.59.11",
    "@typescript-eslint/parser": "^5.59.11",
    "eslint": "^8.42.0",
    "eslint-config-prettier": "^8.8.0",
    "eslint-plugin-prettier": "^4.2.1",
    "jest": "^29.5.0",
    "prettier": "^2.8.8",
    "prisma": "^5.3.0",
    "source-map-support": "^0.5.21",
    "supertest": "^6.3.3",
    "ts-jest": "^29.1.0",
    "ts-loader": "^9.4.3",
    "ts-node": "^10.9.1",
    "tsconfig-paths": "^4.2.0",
    "typescript": "^5.1.3"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "*/.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

Node.js version

19.7.0

In which operating systems have you tested?

Other

VSCode IDE

kamilmysliwiec commented 10 months ago

Please, use our Discord channel (support) for such questions. We are using GitHub to track bugs, feature requests, and potential improvements.