When I fill the user register data such as username, password and email address and then I click continuce button, I got the 500 internal server error. I'm really stuck in the **POST **method.
I used NextJS latest version app features. I setup correctly prisma schema file and env variables. But I got the 500 internal error. Alright, so I open debugger tool and I seen that error on my file RegisterModal.tsx. I tried another solution on my project but I facing the 500 error. If you can please check my code and what should be wrong in my project. I have been opened network tabs it show me 500 internal server error. What should I do that?
This is my project folder structure
RegisterModal.tsx
// RegisterModal.jsx
"use client";
import React, { useState } from "react";
import axios from "axios";
import { AiFillGithub } from "react-icons/ai";
import { FcGoogle } from "react-icons/fc";
import { toast } from "react-hot-toast";
import Button from "../Button";
import {
FieldValues,
SubmitHandler,
useForm,
} from "react-hook-form";
import useRegisterModal from "@/app/hooks/useRegisterModal";
import Heading from "@/app/components/Heading";
import Modal from "./Modal";
import Input from "../inputs/Input";
const RegisterModal = () => {
const registerModal = useRegisterModal();
const [isLoading, setIsLoading] = useState(false);
const {
register,
handleSubmit,
formState: { errors },
} = useForm<FieldValues>({
defaultValues: {
name: "",
email: "",
password: "",
},
});
const onSubmit: SubmitHandler<FieldValues> = (data) => {
setIsLoading(true);
axios
.post("/api/register", data)
.then(() => {
registerModal.onClose();
toast.success("Registration successful!");
})
.catch((err) => {
toast.error(err.response.data.message || "An error occurred");
})
.finally(() => {
setIsLoading(false);
});
};
const bodyContent = (
<div className="flex flex-col gap-4">
<Heading title="Welcome to Airbnb" subTitle="Create an account for free" />
<Input
id="email"
label="Email"
disabled={isLoading}
register={register}
errors={errors}
required
/>
<Input
id="name"
label="Name"
disabled={isLoading}
register={register}
errors={errors}
required
/>
<Input
id="password"
label="Password"
type="password"
disabled={isLoading}
register={register}
errors={errors}
required
/>
</div>
);
const footerContent = (
<div className="flex flex-col gap-4 mt-3">
<hr />
<Button
outline
label="Register with Google"
icon={FcGoogle}
onClick={() => {}}
/>
<Button
outline
label="Register with Github"
icon={AiFillGithub}
onClick={() => {}}
/>
<div className="text-natural-500 text-center mt-4 font-light">
<div className="flex flex-row items-center justify-center gap-2">
<div>Already have an account?</div>
<div
onClick={registerModal.onClose}
className="text-natural-800 cursor-pointer hover:underline"
>
Login
</div>
</div>
</div>
</div>
);
return (
<Modal
disabled={isLoading}
isOpen={registerModal.isOpen}
title="Register"
actionLabel="Continue"
onClose={registerModal.onClose}
onSubmit={handleSubmit(onSubmit)}
body={bodyContent}
footer={footerContent}
/>
);
};
export default RegisterModal;
**
pages/api/auth/[...nextauth].ts**
import bcrypt from "bcrypt"
import NextAuth, { AuthOptions } from "next-auth"
import CredentialsProvider from "next-auth/providers/credentials"
import GithubProvider from "next-auth/providers/github"
import GoogleProvider from "next-auth/providers/google"
import { PrismaAdapter } from "@next-auth/prisma-adapter"
import prisma from "@/app/libs/prismadb"
export const authOptions: AuthOptions = {
adapter: PrismaAdapter(prisma),
providers: [
GithubProvider({
clientId: process.env.GITHUB_ID as string,
clientSecret: process.env.GITHUB_SECRET as string
}),
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID as string,
clientSecret: process.env.GOOGLE_CLIENT_SECRET as string
}),
CredentialsProvider({
name: 'credentials',
credentials: {
email: { label: 'email', type: 'text' },
password: { label: 'password', type: 'password' }
},
async authorize(credentials) {
if (!credentials?.email || !credentials?.password) {
throw new Error('Invalid credentials');
}
const user = await prisma.user.findUnique({
where: {
email: credentials.email
}
});
if (!user || !user?.hashedPassword) {
throw new Error('Invalid credentials');
}
const isCorrectPassword = await bcrypt.compare(
credentials.password,
user.hashedPassword
);
if (!isCorrectPassword) {
throw new Error('Invalid credentials');
}
return user;
}
})
],
pages: {
signIn: '/',
},
debug: process.env.NODE_ENV === 'development',
session: {
strategy: "jwt",
},
secret: process.env.NEXTAUTH_SECRET,
}
export default NextAuth(authOptions);
**
libs/prismadb.ts**
import { PrismaClient } from "@prisma/client"
declare global {
var prisma: PrismaClient | undefined
}
const client = globalThis.prisma || new PrismaClient()
if (process.env.NODE_ENV !== "production") globalThis.prisma = client
export default client
**
api/register/route.ts**
import { NextResponse } from "next/server";
import bcrypt from "bcrypt";
import prisma from "@/app/libs/prismadb";
export async function POST(
request: Request,
) {
const body = await request.json();
const {
email,
name,
password,
} = body;
const hashedPassword = await bcrypt.hash(password, 12);
const user = await prisma.user.create({
data: {
email,
name,
hashedPassword,
}
});
return NextResponse.json(user);
}
I spend 4 to 5 hours of time to fix that errors. But in this time, I got the still errors. Please help me the hell.