Next.js Discord

Discord Forum

Empty POST request in Next-auth credentials

Unanswered
Somali posted this in #help-forum
Open in Discord
SomaliOP
Hi, I'm trying to authorize a user using my custom backend, I'm submitting data from my form to the backend and expecting to receive a user object.
But when I send POST data to the authorization function, I am not sending my data, I am sending an emty object, that's why I got an error from the backend.
Can you help me please debug or solve this problem, I tried many solutions but can't figure out what I'm doing wrong.

source code in next message...

1 Reply

SomaliOP
import { NextAuthOptions } from 'next-auth';
import NextAuth from 'next-auth/next';
import CredentialsProvider from 'next-auth/providers/credentials';

import client from '@/api/client';

const handler = NextAuth({
    debug: true,

    providers: [
        CredentialsProvider({
            name: 'Account',
            credentials: {
                email: {
                    label: 'Email',
                    type: 'email',
                },
                password: { label: 'Password', type: 'password' },
            },
            async authorize(credentials) {
                if (!credentials) {
                    throw new Error('No credentials provided');
                }

                const user = await client
                    .post('login', {
                        json: {
                            email: 'john.doe@example.com',
                            password: '123456',
                        },
                        hooks: {
                            afterResponse: [
                                async (d, o, r) => {
                                    const data = await r.json();
                                    const k = await d.json();
                                    console.log(data);
                                    console.log(k);
                                },
                            ],
                        },
                    })
                    .json();

                if (user) {
                    return user;
                } else {
                    throw new Error(
                        JSON.stringify({ errors: user.errors, status: false }),
                    );
                }
            },
        }),
    ],
    session: {
        strategy: 'jwt',
    },
    secret: process.env.NEXTAUTH_SECRET,
});

export { handler as GET, handler as POST };