I'm late to this, but I faced the same issue and this is the workaround I found that works.
So with Next.js@13^ or App router, they ask you not to modify the next-env.d.ts
. They say it's to help them easily add updates/features of Next in the future, so they regenerate it for every build.
Specifically, the original file says,
// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
So the way around this is to create a new module with any name. Following convention, it would be a good idea to name it next-auth-extensions.d.ts
in the same location/folder as next-env.d.ts
and then add that to the includes
property in the tsconfig.json
.
So the following files are the end result for my config across Next, Typescript and Next-Auth.
// next-auth-extensions.d.ts
import NextAuth, { DefaultSession } from "next-auth";
declare module "next-auth" {
/**
* Returned by `useSession`, `getSession`, and received as a prop on the `SessionProvider` React Context
*/
interface Session {
user: {
/** The user's id */
id: string;
} & DefaultSession["user"];
}
}
// tsconfig.json
{
...
"compilerOptions": {
...
},
"include": ["next-env.d.ts", "next-auth-extensions.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
...
}
// /api/auth/[...nextauth]/route.ts
import { db } from "@/db";
import { PrismaAdapter } from "@auth/prisma-adapter";
import NextAuth, { NextAuthOptions } from "next-auth";
export const authOptions: NextAuthOptions = {
adapter: PrismaAdapter(db),
providers: [
...
],
callbacks: {
async session({ session, user }) {
console.log(user);
session.user.id = user.id;
return session;
},
},
...
};
const handler = NextAuth(authOptions);
export { handler as GET, handler as POST };
I'm not sure about any adaptations for GraphQL, since I've never worked with that before but this works for me. Look at my package.json
below just in case there might be discrepancies with regard to versions. Let me know if it works.
Note: This isn't the official way or anything, it's just something that I've researched and tried to get it to work, so if anybody has suggestions or anything, let me know.
{
...
"dependencies": {
"@auth/prisma-adapter": "^1.0.3",
"@prisma/client": "^5.4.2",
...
"next": "13.5.4",
"next-auth": "^4.23.2",
"react": "^18",
...
},
token.sub
nottoken.uid
for me. – Womenfolk