title: How to write authorize function correctly in typescript for nextauthjs
body: <p>I am trying to use <a href="https://next-auth.js.org/providers/credentials" rel="nofollow noreferrer">Credentials Provider</a> with NextJs (&quot;next&quot;: &quot;^12.0.7&quot;) and NextAuth (&quot;next-auth&quot;: &quot;^4.1.2&quot;). I am writing this in typescript, and facing issues in getting the function correctly.</p>
  <p>Here's my <code>/pages/api/[...nextauth].ts</code></p>
  <pre class="lang-js prettyprint-override"><code>import NextAuth, {  } from &quot;next-auth&quot;;
  import CredentialsProvider from &quot;next-auth/providers/credentials&quot;;
  import { verifyOTP } from &quot;@/lib/otp&quot;;
  
  // {
  //   user: {
  //     id: 1,
  //     username: null,
  //     phone: '+919876543210',
  //     phoneVerified: true,
  //     email: null,
  //     emailVerified: false,
  //     active: true,
  //     token: 'SOME TOKEN',
  //     createDate: null
  //   },
  //   result: 'approved'
  // }
  
  export default NextAuth({
    pages: {
      signIn: '/signup',
      newUser: '/signup',
      error: '/signup'
    },
    debug: false,
    secret: &quot;SOME_SECRET&quot;,
    // Configure one or more authentication providers
    providers: [
      CredentialsProvider({
        credentials: {
          username: {label: &quot;username&quot;, type: &quot;text&quot;, placeholder: &quot;markandre&quot;},
          phone: { label: &quot;Phone &quot;, type: &quot;text&quot;, placeholder: &quot;+919876543210&quot; },
          otp: {  label: &quot;OTP&quot;, type: &quot;text&quot; }
        },
        authorize: async (credentials, _req) =&gt; {
          try {
            const res = await verifyOTP(credentials!.phone,
              credentials!.otp, credentials?.username);
            if (res.result === &quot;approved&quot;) {
              return Promise.resolve( {
                id: res.user.id,
                email: res.user.email,
                name: res.user.phone,
                token: res.user.token
              });
            }
          } catch (e: any) {
            //const errorMessage = e.response.data.message;
            //throw new Error(errorMessage);
            return Promise.reject(null);
          }
        }
      })
    ],
    session: {
      strategy: 'jwt',
      maxAge: 3 * 60 * 60, // 3 hours
    },
    callbacks: {
      jwt: async ({token, user}) =&gt; {
        if (user) {
          token.accessToken = user.token
        }
  
        return token;
      },
      session:async ({session, token}) =&gt; {
        if (token) {
          session.accessToken = token.accessToken;
        }
        return session;
      }
    },
    events: {
      signIn: async (message) =&gt; {
        console.log('signIn', message);
      },
      signOut: async (message) =&gt; {
        console.log('signOut', message);
      },
      createUser: async(message) =&gt; {
        console.log('createUser', message);
      },
      updateUser: async(message) =&gt; {
        console.log('updateUser', message);
      },
      linkAccount: async(message) =&gt; {
        console.log('linkAccount',message);
      },
      session: async(message) =&gt; {
        // console.log('session', message);
      }
    }
  })
  
  </code></pre>
  <p>When I am building this app, I keep getting the error</p>
  <pre><code>./src/pages/api/auth/[...nextauth].ts:36:7
  Type error: Type '(credentials: Record&lt;&quot;username&quot; | &quot;phone&quot; | &quot;otp&quot;, string&gt; | undefined, _req: Pick&lt;IncomingRequest, &quot;headers&quot; | &quot;body&quot; | &quot;query&quot; | &quot;method&quot;&gt;) =&gt; Promise&lt;...&gt;' is not assignable to type '(credentials: Record&lt;&quot;username&quot; | &quot;phone&quot; | &quot;otp&quot;, string&gt; | undefined, req: Pick&lt;IncomingRequest, &quot;headers&quot; | &quot;body&quot; | &quot;query&quot; | &quot;method&quot;&gt;) =&gt; Awaitable&lt;...&gt;'.
    Type 'Promise&lt;Omit&lt;User, &quot;id&quot;&gt; | null | undefined&gt;' is not assignable to type 'Awaitable&lt;Omit&lt;User, &quot;id&quot;&gt; | { id?: string | undefined; } | null&gt;'.
      Type 'Promise&lt;Omit&lt;User, &quot;id&quot;&gt; | null | undefined&gt;' is not assignable to type 'Omit&lt;User, &quot;id&quot;&gt;'.
        Index signature for type 'string' is missing in type 'Promise&lt;Omit&lt;User, &quot;id&quot;&gt; | null | undefined&gt;'.
  
    34 |         otp: {  label: &quot;OTP&quot;, type: &quot;text&quot; }
    35 |       },
  &gt; 36 |       authorize: async (credentials, _req) =&gt; {
       |       ^
    37 |         try {
    38 |           const res = await verifyOTP(credentials!.phone,
    39 |             credentials!.otp, credentials?.username);
  info  - Checking validity of types .%
  </code></pre>
  <p>My <code>.tsconfig</code> looks like below</p>
  <pre><code>{
    &quot;compilerOptions&quot;: {
      &quot;lib&quot;: [&quot;dom&quot;, &quot;dom.iterable&quot;, &quot;esnext&quot;],
      &quot;module&quot;: &quot;esnext&quot;,
      &quot;moduleResolution&quot;: &quot;node&quot;,
      &quot;resolveJsonModule&quot;: true,
      &quot;removeComments&quot;: true,
      &quot;preserveConstEnums&quot;: true,
      &quot;strict&quot;: true,
      &quot;alwaysStrict&quot;: false,
      &quot;strictNullChecks&quot;: true,
      &quot;noUncheckedIndexedAccess&quot;: true,
  
      &quot;noImplicitAny&quot;: true,
      &quot;noImplicitReturns&quot;: true,
      &quot;noImplicitThis&quot;: true,
      &quot;noUnusedLocals&quot;: true,
      &quot;noUnusedParameters&quot;: true,
      &quot;allowUnreachableCode&quot;: false,
      &quot;noFallthroughCasesInSwitch&quot;: true,
  
      &quot;target&quot;: &quot;es5&quot;,
      &quot;outDir&quot;: &quot;out&quot;,
      &quot;declaration&quot;: true,
      &quot;sourceMap&quot;: true,
  
      &quot;esModuleInterop&quot;: true,
      &quot;allowSyntheticDefaultImports&quot;: true,
      &quot;allowJs&quot;: true,
      &quot;skipLibCheck&quot;: true,
      &quot;forceConsistentCasingInFileNames&quot;: true,
  
      &quot;jsx&quot;: &quot;preserve&quot;,
      &quot;noEmit&quot;: true,
      &quot;isolatedModules&quot;: true,
      &quot;incremental&quot;: true,
  
      &quot;baseUrl&quot;: &quot;.&quot;,
      &quot;paths&quot;: {
        &quot;@/*&quot;: [&quot;./src/*&quot;],
        &quot;@/public/*&quot;: [&quot;./public/*&quot;]
      }
    },
    &quot;exclude&quot;: [&quot;./out/**/*&quot;, &quot;./node_modules/**/*&quot;],
    &quot;include&quot;: [&quot;next-env.d.ts&quot;, &quot;**/*.ts&quot;, &quot;**/*.tsx&quot;]
  }
  
  </code></pre>