This API handles user signup with proper validation. It checks if the user already exists, validates the password length, hashes the password with bcrypt, saves the user in MongoDB, and returns the created user. If anything goes wrong, it responds with clear error messages.

// sign-up
// check user already exist
// password check for 6 characters (optional)
// password hash (using bcrypt js)
// user create 

import dbConnect from "@/lib/db";
import UserModel from "@/model/userModel";
import bcrypt from "bcryptjs";
import { NextRequest, NextResponse } from "next/server";

export async function POST(req: NextRequest) {
    try {
        const { name, email, password } = await req.json();
        
        //connect to DB
        await dbConnect();

        if (!name || !email || !password) {
            return NextResponse.json(
                { message: "All fields are required" },
                { status: 400 }
            );
        }

        const existUser = await UserModel.findOne({ email });
        if (existUser) {
            return NextResponse.json(
                { message: "User already exists" },
                { status: 400 }
            );
        }

        if (password.length < 6) {
            return NextResponse.json(
                { message: "Password must be at least 6 characters" },
                { status: 400 }
            );
        }

        const hashedPassword = await bcrypt.hash(password, 10);

        const newUser = await UserModel.create({
            name:name,
            email:email,
            password: hashedPassword,
        });

        return NextResponse.json(newUser, { status: 201 });

    } catch (error) {
        return NextResponse.json(
            { message: "Server error", error: `${error}` },
            { status: 500 }
        );
    }
}