Next.js Discord

Discord Forum

API handler should not return a value, received object.

Unanswered
American Crocodile posted this in #help-forum
Open in Discord
American CrocodileOP
1. Trying to do Google and Github Authentication.

27 Replies

American CrocodileOP
[...nextauth].ts

import NextAuth from "next-auth";
import Credentials from "next-auth/providers/credentials";
import {compare} from "bcrypt";

import GoogleProvider from "next-auth/providers/google";
import GithubProvider from "next-auth/providers/github";

import {PrismaAdapter} from "@next-auth/prisma-adapter";
import { NextResponse } from "next/server";

import prismadb from "@/lib/prismadb";

export default NextAuth({
    providers: [
        GithubProvider({
            clientId: process.env.GITHUB_ID || '',
            clientSecret: process.env.GITHUB_SECRET || '',
        }),
        GoogleProvider({
            clientId: process.env.GOOGLE_CLIENT_ID || '',
            clientSecret: process.env.GOOGLE_CLIENT_SECRET || '',
        }),
        Credentials({
            id: 'credentials',
            name: 'Credentials',
            credentials: {
                email: {
                    label: 'Email',
                    type: 'text',
                },
                password: {
                    label: 'Password',
                    type: 'password'
                }
            },
            async authorize(credentials) {
                if (!credentials?.email || !credentials?.password) {
                    throw new Error('Email and password required!');
                }

                const user = await prismadb.user.findUnique({
                    where: {
                        email: credentials.email
                    }
                });

                if (!user || !user.hashedPassword) {
                    throw new Error('Email does not exist');
                }

                const isCorrectPassword = await compare(credentials.password, user.hashedPassword);

                if (!isCorrectPassword) {
                    throw new Error('Incorrect password');
                }

                return user
            }
        }),
    ],
    pages: {
        signIn: '/auth'
    },
    debug: process.env.NODE_ENV === 'development',
    adapter: PrismaAdapter(prismadb),
    session: {strategy: 'jwt'},
    jwt: {
        secret: process.env.NEXTAUTH_JWT_SECRET,
    },
    secret: process.env.NEXTAUTH_SECRET
});
I am trying to do Google and Github Authentication. But every time after login i see an error in terminal: "API handler should not return a value, received object."
I tried to solve that with NextResponse but i see a different error
instead of return user i wrote return new NextResponse(user)

And the error was:

async authorize(credentials)
TS2322: Type '(credentials: Record<"email" | "password", string> | undefined) => Promise<NextResponse<unknown>>' is not assignable to type '(credentials: Record<"email" | "password", string> | undefined, req: Pick<RequestInternal, "headers" | "body" | "query" | "method">) => Awaitable<...>'.   Type 'Promise<NextResponse<unknown>>' is not assignable to type 'Awaitable<User | null>'.     Type 'Promise<NextResponse<unknown>>' is not assignable to type 'PromiseLike<User | null>'.       Types of property 'then' are incompatible.
What DB are you using?
American CrocodileOP
I am using Prisma + mongoDB
I saw someone having the same issue and they just needed to whitelist their IP address.
Try going to your Atlas website and add your current IP by clicking on "Add current IP"
And i also added all ip's
Are you using server actions?
American CrocodileOP
i think no
And i want to say that i receive an error "API handler should not return a value, received object." only when i log with other providers. With my own providers everything is good
Try looking for the "return" keyword in your API folder and check if any is returning an object instead of a NextResponse
user is an object
i tried to replace it to NextResponse but i got errors then
American CrocodileOP
I tried
But i got these errors
@not-milo.tsx This is wrong, it should be `return NextResponse.json(user)`
thats for API route. not in nextauth authOption
Yeah, I was just pointing out that it was an incorrect usage of NextResponse
Anyway, I haven't been able to find anything about this. The only thing that gets close is this discussion on GitHub: https://github.com/vercel/next.js/discussions/48951
Can you link me a minimum reproduction repository? So I can try and dig through the code
@American Crocodile But i got these errors
Dont return nextresponse in there
@alfon Dont return nextresponse in there
American CrocodileOP
sorry, where i should write return ?
Leave the return statement inside authorize as is. It should only return user.

The warning you're getting is likely caused by something else.
American CrocodileOP
okey. Thanks