javascript - Next-Auth gives error on sign in: "API handler should not return a value, received object." - Sta

I'm using [email protected] with [email protected] and Typescript. Whenever I use the signIn() fun

I'm using [email protected] with [email protected] and Typescript. Whenever I use the signIn() function, it goes through sign in as normal, but then print the following line to the server's console: API handler should not return a value, received object. I am not signed in after doing this.

I've checked my [...nextauth].ts file and can't find any errors. The code is below:

import clientPromise from "@/lib/mongodbadapter";
import { MongoDBAdapter } from "@next-auth/mongodb-adapter";
import NextAuth, { AuthOptions } from "next-auth"
import DiscordProvider from "next-auth/providers/discord";
import GitHubProvider from "next-auth/providers/github";

export const authOptions: AuthOptions = {
    adapter: MongoDBAdapter(clientPromise),
    providers: [
        DiscordProvider({
            clientId: process.env.DISCORD_CLIENT_ID!,
            clientSecret: process.env.DISCORD_CLIENT_SECRET!
        }),
        GitHubProvider({
            clientId: process.env.GITHUB_CLIENT_ID!,
            clientSecret: process.env.GITHUB_CLIENT_SECRET!
        })
    ]
}

export default NextAuth(authOptions)

I've wrapped all HTML elements in a SessionProvider, like so:

export default function App({ Component, pageProps: { session, ...pageProps } }: AppProps) {
  return <SessionProvider session={session}>
    <Navbar />
    <Component {...pageProps} />
  </SessionProvider>
}

In my index.ts, I log session, like so:

export async function getServerSideProps(ctx: any) {
  return {
    props: {
        session: await getServerSession(ctx.req, ctx.res, authOptions)
    }
  }
}

The server log is this:

undefined
- wait piling /api/auth/[...nextauth] (client and server)...
- event piled successfully in 265 ms (63 modules)
API handler should not return a value, received object.
API handler should not return a value, received object.
undefined

The undefined e from where I log the session. The piling is from me returning to the index.ts page after signing in.

Even though session is undefined, my database (I'm using @next-auth/[email protected]) has the relevant user and account documents.

What do I need to change to avoid this error and make it actually sign me in, or is it an issue with Next-Auth that's out of my control?

Edit: I confirmed my IP is allowed in my MongoDB Atlas instance.

I'm using [email protected] with [email protected] and Typescript. Whenever I use the signIn() function, it goes through sign in as normal, but then print the following line to the server's console: API handler should not return a value, received object. I am not signed in after doing this.

I've checked my [...nextauth].ts file and can't find any errors. The code is below:

import clientPromise from "@/lib/mongodbadapter";
import { MongoDBAdapter } from "@next-auth/mongodb-adapter";
import NextAuth, { AuthOptions } from "next-auth"
import DiscordProvider from "next-auth/providers/discord";
import GitHubProvider from "next-auth/providers/github";

export const authOptions: AuthOptions = {
    adapter: MongoDBAdapter(clientPromise),
    providers: [
        DiscordProvider({
            clientId: process.env.DISCORD_CLIENT_ID!,
            clientSecret: process.env.DISCORD_CLIENT_SECRET!
        }),
        GitHubProvider({
            clientId: process.env.GITHUB_CLIENT_ID!,
            clientSecret: process.env.GITHUB_CLIENT_SECRET!
        })
    ]
}

export default NextAuth(authOptions)

I've wrapped all HTML elements in a SessionProvider, like so:

export default function App({ Component, pageProps: { session, ...pageProps } }: AppProps) {
  return <SessionProvider session={session}>
    <Navbar />
    <Component {...pageProps} />
  </SessionProvider>
}

In my index.ts, I log session, like so:

export async function getServerSideProps(ctx: any) {
  return {
    props: {
        session: await getServerSession(ctx.req, ctx.res, authOptions)
    }
  }
}

The server log is this:

undefined
- wait piling /api/auth/[...nextauth] (client and server)...
- event piled successfully in 265 ms (63 modules)
API handler should not return a value, received object.
API handler should not return a value, received object.
undefined

The undefined e from where I log the session. The piling is from me returning to the index.ts page after signing in.

Even though session is undefined, my database (I'm using @next-auth/[email protected]) has the relevant user and account documents.

What do I need to change to avoid this error and make it actually sign me in, or is it an issue with Next-Auth that's out of my control?

Edit: I confirmed my IP is allowed in my MongoDB Atlas instance.

Share Improve this question edited Jul 3, 2023 at 20:11 Rgamer43 asked Jun 24, 2023 at 20:59 Rgamer43Rgamer43 6628 silver badges19 bronze badges 4
  • Did you manage to find a solution for the warning 'API handler should not return a value, received object'? – DevQandA Commented Jul 24, 2023 at 19:41
  • Not yet, but I haven't worked too much on this issue. – Rgamer43 Commented Jul 25, 2023 at 20:01
  • Like your code I had export default NextAuth(authOptions) in [...nextauth].ts. In a repo I saw someone using const handler = NextAuth(authOptions) export { handler as GET, handler as POST } instead. This solved the issue for me. I don't have an explanation for it. With Prisma the first option was fine, but with other adapters clearly not. – DevQandA Commented Jul 25, 2023 at 20:38
  • I tried your solution, but that gave me Type Error: resolver is not a function – Rgamer43 Commented Jul 26, 2023 at 12:05
Add a ment  | 

2 Answers 2

Reset to default 5

Had the same issue. Just changed [...nextauth].ts

from:

export default NextAuth(authOptions)

to:

const authHandler = NextAuth(authOptions);
export default async function handler(...params: any[]) {
  await authHandler(...params);
}

Hope it helps.

What we're doing here is initializing NextAuth on its "advanced" form instead of using its simpler form. You can read more about it in the link but it let's you access variables inside the request and response objects. For example, you could access req.cookies.

I faced the same issue, and when I reopened the Mongo-Atlas website, it showed that my current IP address wasn't added, so after clicking on "add current IP" the issue was solved.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744847078a4596905.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信