lijuanma1331
lijuanma1331
TtRPC
Created by DevR on 5/28/2023 in #❓-help
Next-auth session not being fetched in tRPC context
This is quite strange🤨
20 replies
TtRPC
Created by DevR on 5/28/2023 in #❓-help
Next-auth session not being fetched in tRPC context
My envinfo is
System:
OS: Windows 10 10.0.19044
CPU: (8) x64 Intel(R) Core(TM) i7-4770K CPU @ 3.50GHz
Memory: 11.07 GB / 23.94 GB
Binaries:
Node: 18.16.0 - ~\AppData\Local\Volta\tools\image\node\18.16.0\node.EXE
Yarn: 1.22.17 - ~\AppData\Local\Volta\tools\image\yarn\1.22.17\bin\yarn.CMD
npm: 9.5.1 - ~\AppData\Local\Volta\tools\image\node\18.16.0\npm.CMD
System:
OS: Windows 10 10.0.19044
CPU: (8) x64 Intel(R) Core(TM) i7-4770K CPU @ 3.50GHz
Memory: 11.07 GB / 23.94 GB
Binaries:
Node: 18.16.0 - ~\AppData\Local\Volta\tools\image\node\18.16.0\node.EXE
Yarn: 1.22.17 - ~\AppData\Local\Volta\tools\image\yarn\1.22.17\bin\yarn.CMD
npm: 9.5.1 - ~\AppData\Local\Volta\tools\image\node\18.16.0\npm.CMD
20 replies
TtRPC
Created by DevR on 5/28/2023 in #❓-help
Next-auth session not being fetched in tRPC context
So maybe next-auth related?
20 replies
TtRPC
Created by DevR on 5/28/2023 in #❓-help
Next-auth session not being fetched in tRPC context
Hi! Im getting the same error. To reproduce it just clone the websocket/subscriptions, follow the steps and on the first run the error will appear. I think this is the code where it fails
import * as trpc from '@trpc/server';
import * as trpcNext from '@trpc/server/adapters/next';
import { NodeHTTPCreateContextFnOptions } from '@trpc/server/adapters/node-http';
import { IncomingMessage } from 'http';
import { getSession } from 'next-auth/react';
import ws from 'ws';

/**
* Creates context for an incoming request
* @link https://trpc.io/docs/context
*/
export const createContext = async (
opts:
| trpcNext.CreateNextContextOptions
| NodeHTTPCreateContextFnOptions<IncomingMessage, ws>,
) => {
const session = await getSession(opts); // Always null

console.log('createContext for', session?.user?.name ?? 'unknown user');

return {
session,
};
};

export type Context = trpc.inferAsyncReturnType<typeof createContext>;
import * as trpc from '@trpc/server';
import * as trpcNext from '@trpc/server/adapters/next';
import { NodeHTTPCreateContextFnOptions } from '@trpc/server/adapters/node-http';
import { IncomingMessage } from 'http';
import { getSession } from 'next-auth/react';
import ws from 'ws';

/**
* Creates context for an incoming request
* @link https://trpc.io/docs/context
*/
export const createContext = async (
opts:
| trpcNext.CreateNextContextOptions
| NodeHTTPCreateContextFnOptions<IncomingMessage, ws>,
) => {
const session = await getSession(opts); // Always null

console.log('createContext for', session?.user?.name ?? 'unknown user');

return {
session,
};
};

export type Context = trpc.inferAsyncReturnType<typeof createContext>;
20 replies