The deployment of the Next.js project encountered issues due to TypeScript types

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...)

Answer №1

If you want to implement this functionality, you can refer to the official documentation for guidance.

session.userID = user.id

Alternatively, consider using any data type for the session variable to resolve any issues that may arise.

session: async (session:any, user) => {
    session.user.id = user.id as string;
    return Promise.resolve(session);
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The required dependencies for google-chart-angular are not found

I have been trying to set up google-chart-angular in my Angular project by following the steps outlined in this tutorial. I have also added GoogleChartsModule to my app.module.ts file. Although I believe everything is set up correctly, when I try to run t ...

Unable to locate the identifier 'Component' - Error 2304 in Angular 2 TypeScript

Exploring Angular2 and TypeScript for the first time. Attempting examples like the one below: @Component({ selector: 'counter', template: ` {{ value }} <button (click)="increase()">Increase</button> <button (clic ...

Typescript typings for higher order components in React

I am currently attempting to incorporate an existing react component (react-select) within a High Order Component (HoC) to implement conditional rendering logic. The challenge lies in ensuring that TypeScript can properly integrate the properties of both t ...

The ISR function seems to be malfunctioning on the Django server, while it is functioning properly

Hello! I recently encountered an issue where ISR in Django Python server fails, but it works on the next dev server. In the dev server, updating content in the Django admin panel reflects the changes, but when switching to production mode using the Django ...

Storing and accessing authentication tokens using Apollo and Next.js with server-side rendering

I'm struggling to understand the concept of authentication with Apollo Client and Next.js SSR. As far as I know, when using Next.js 13 (with the new app router), all components are React Server Components by default. So, if I am using Apollo Client, ...

A backend glitch is exposed by NextJS in the web application

Currently, I am utilizing Strapi for my backend and have created a small script to handle authorization for specific parts of the API. Additionally, I made a slight modification to the controller. 'use strict'; const { sanitizeEntity } = require( ...

Using Angular service within InnerHTML functions

Within my Angular 10 application, I am utilizing innerHtml to display some content that includes anchor links. My goal is to trigger a function every time a link is clicked, which will then invoke an Angular service. In the code snippet below, I am attac ...

Error message encountered: Typescript, Webpack, angular - Unable to execute due to TypeError: Object(…) does not operate as

Just starting out with TypeScript and Angular, I've been tasked with creating a typedefinition for a custom library. Despite confirming that the JavaScript from my external library is loading correctly in the resources, I encountered an error when cal ...

Updating the data and processing results settings for Select2 in an Angular 2 application

In my Angular2 app, I am utilizing Select2 and facing a challenge with accessing class properties in the data and processResults contexts. Unfortunately, these contexts do not belong to the class: export class DefaultFormInputSelectComponent { @Input ...

Unleashing the power of dynamic data imports in a Node.js application

My goal is to utilize the require function in a node/express application with typescript to import a json file. Here's how I attempted it: const url = `./data/${resource}.json`; const data = require(url); However, I encountered the error Cannot find ...

Discovering specific values for an ID using API calls in Angular (Implementing CRUD Operations in Angular with API Integration)

My current project involves CRUD operations in Angular utilizing the API created in Laravel. I have successfully added and fetched values, but encountered an issue when attempting to update values using their respective IDs. This snippet is from my app.co ...

How come the Debugger's Network tab provides easy access to my unprocessed source code?

After spending a month working on my website, I discovered an unexpected _N_E server providing access to the raw source code for each page. I suspect Sentry in NextJS may be involved, but can't find any information in their documentation. This poses a ...

Incorrect errors are displayed by VS Code in ts-node shell scripts

I came across an interesting article discussing running a TypeScript file on the command line, and while it seems to be functioning properly, I am encountering invalid errors in VS Code: https://i.sstatic.net/eis3X.png As seen in the terminal (bottom hal ...

Best practices for implementing GraphQL in Next.js applications

Within my applications, I utilize the following NPM modules to interact with Strapi, GraphQL, and Next.js: react-apollo next-apollo graphql gql recompose As a next step, I am setting up an Apollo configuration file as shown below: import { HttpLink } ...

Convert a TypeScript array of strings to a boolean array: a step-by-step guide

Upon receiving a list of objects from the front end as: item=["false","true"] I proceed to check a column within my records to identify values containing "true" or "false" using the following code: this.records.filter(x=> items.includes(x.column)) Unf ...

Create personalized access tokens through Azure Active Directory (AD)

Currently, I am working on a Next.js application that requires users to log in using their Azure ID. Once logged in, I need to verify the email and other details from the token on my Node.js backend before sending a custom token to the frontend for addit ...

Display captions on react-player videos using an .srt file

Currently, I am working on a React/Typescript project with Next.js. A key feature of this project is a modal that utilizes 'react-player' to display videos. While the video and modal are functioning as intended, I am looking to incorporate capti ...

How to inject a regular class in Angular2 without the @Injectable decorator

angular: 2.0.0-beta.9 Can we inject a non-@Injectable class into a component? For instance, if this class originates from an external Third party library. ...

The best approach for setting a select value and managing state in React using TypeScript

Currently, I am in the process of familiarizing myself with TypeScript within my React projects. I have defined a type for the expected data structure (consisting of name and url). type PokedexType = { name: string; url: string; } The API respon ...

Switching from module.exports in Javascript to Typescript format

My Node-express code currently uses module.exports to export functions. As I am converting the code to TypeScript, I need to find out how to replace module.exports in typescript. Can you help me with this? ...