Recently, I came across create-t3-app and decided to try it out for my NextJS projects. It handles a lot of the initial setup work for TypeScript, trpc, prisma, and next-auth, which would save me a significant amount of time. While this is beneficial, it doesn't seem to be at the root of the issue I'm facing. The problem arises from using a MySQL database with auto-incrementing user IDs. The types defined in the next-auth
package specify user IDs as strings (DefaultUser
in next-auth/core/types.d.ts
and AdapterUser
in next-auth/adapters.d.ts
both indicate the ID type as string despite comments mentioning UUIDs). To try and accommodate numeric user IDs, I attempted to extend the existing types by adding the following code snippet to next-auth.d.ts
:
import { DefaultSession, DefaultUser } from 'next-auth'
declare module 'next-auth' {
interface User extends DefaultUser {
id: number;
}
interface Session {
user?: {
id: number;
} & DefaultSession['user'];
}
}
This modification seemed to work in most cases except in [...nextauth].ts
where an error occurred:
Type 'string | number' is not assignable to type 'number'. Type 'string' is not assignable to type 'number'.ts(2322)
The error specifically points to the line session.user.id = user.id
within this part of the code:
export const authOptions: NextAuthOptions = {
// Include user.id on session
callbacks: {
session({ session, user }) {
if (session.user) {
session.user.id = user.id
}
return session
}
},
adapter: PrismaAdapter(prisma),
providers: []
}
export default NextAuth(authOptions)
I managed to resolve the TypeScript error by removing the id: string;
line from the AdapterUser
section in next-auth/adapters.d.ts
, returning it to id: number;
as specified in the modified User
interface:
export interface AdapterUser extends User {
id: string; // <-- I removed this
email: string;
emailVerified: Date | null;
}
Although I believe that adjusting the library's types to facilitate numeric user IDs should not be necessary, I have exhausted all other solutions and have not found relevant information online. Should I reconsider using numeric IDs even though they align with my database structure? For context, here are the versions of the technologies I am currently utilizing:
next 12.3.1
next-auth 4.12.3
react 18.2.0
react-dom 18.2.0
superjson 1.9.1
zod 3.18.0
eslint 8.22.0
postcss 8.4.14
prettier 2.7.1
prisma 4.4.0
tailwindcss 3.1.6
typescrypt 4.8.0