Currently, I am in the process of deploying my project using Vercel. The project was built using Next.js and TypeScript.
I have integrated Next-auth
with the credentials
provider for authentication. By default, this authentication package includes an object for sessions structured as follows:
{ name?: string; email?: string; image?: string; }
. An important note is that I have added an id
property to this object as a string
in the types file of this package because I specifically need to utilize the user id within the session.
Everything appears to be functioning correctly, and I can successfully use the id for backend operations while TypeScript recognizes the id property. However, during the deployment process, I encountered an error message.
Below are some of the types where I have included the id property into the types file:
export interface DefaultUser {
name?: string | null
email?: string | null
image?: string | null
id?: string | null // Here
}
export interface DefaultJWT extends Record<string, unknown> {
name?: string | null
email?: string | null
picture?: string | null
id?: string | null // Here
sub?: string
}
export interface DefaultSession extends Record<string, unknown> {
user?: {
name?: string | null
email?: string | null
image?: string | null
id?: string | null // Here
}
expires?: string
}
This represents my auth file (/api/auth/[...nextauth].js):
export default NextAuth({
session: {
jwt: true
},
providers: [
Providers.Credentials({
async authorize(credentials: Record<"email" | "password", string>, req) {
try {
return await authOperation(credentials); // This returns { id: "****", email: "****" }
} catch (error) {
throw error;
}
}
})
],
// These codes pertain to adding the id property
callbacks: {
jwt: async (token, user, account, profile, isNewUser) => {
if (user) {
token.id = user.id;
}
return Promise.resolve(token);
},
session: async (session, user) => {
session.user.id = user.id as string;
return Promise.resolve(session);
}
}
});
The following error occurred on Vercel:
20:22:45.371 Cloning github.com/mohammaDJ23/invoice (Branch: master, Commit: 575d08a)
20:22:45.621 Cloning completed: 249.901ms
(A lengthy log detailing analysis, installation, and build runtime processes)
20:23:13.456 Failed to compile.
20:23:13.456 ./pages/api/auth/[...nextauth].ts:32:20
20:23:13.456 Type error: Property 'id' does not exist on type '{ name?: string; email?: string; image?: string; }'.
(Error continues...)