next-auth icon indicating copy to clipboard operation
next-auth copied to clipboard

`unstable_getServerSession()` doesn't work on graphql (type-graphql)

Open vincent-thomas opened this issue 3 years ago • 0 comments

Environment

  System:
    OS: Linux 5.10 Ubuntu 22.04 LTS 22.04 LTS (Jammy Jellyfish)
    CPU: (2) x64 AMD Ryzen 7 5800X 8-Core Processor
    Memory: 582.82 MB / 3.83 GB
    Container: Yes
    Shell: 3.3.1 - /usr/bin/fish
  Binaries:
    Node: 16.16.0 - ~/.volta/tools/image/node/16.16.0/bin/node
    Yarn: 3.2.2 - ~/.volta/tools/image/yarn/1.22.19/bin/yarn
    npm: 8.11.0 - ~/.volta/tools/image/node/16.16.0/bin/npm
  Browsers:
    Chrome: 104.0.5112.79

    next: 12.2.4
    type-graphql 1.1.1

   (not specifiying react because it's entirely on the server-side)

Reproduction URL

https://github.com/vincent-thomas/typegraphql-with-next-auth-issue

Describe the issue

The issue is that i can't get the user on server-side with graphql (more precisely type-graphql). I do have access to the request object. (I guess it follows the built-in 'Request' interface, so not next.js 'NextApiRequest'). I have tried with unstable_getServerSession, getSession and getToken. None of which worked

// backend/resolvers/dog/resolver.ts
@Resolver(DogObject)
export class DogResolver {
  @Query(() => [DogObject])
  async dogs(@Ctx() { req }: IGraphqlContext): Promise<DogObject[]> {
    const user = await getToken({ req }); // <- 'req' is the request object
    console.log("USER", user); // <- 'user' = null, outputs "USER null"
    return dogs;
  }

Is this not possible?

How to reproduce

Run

git clone https://github.com/vincent-thomas/typegraphql-with-next-auth-issue && \
yarn install && \ # Yarn is required because i'm using pnp
docker compose -d && yarn develop

Not a pro on bash, you may have to modify it a bit

Expected behavior

That it fetches the user as expected on the server with graphql and type-graphql

vincent-thomas avatar Aug 09 '22 17:08 vincent-thomas