import mongoose from "mongoose";
import { User } from "../models/User.js";
import bcrypt from 'bcrypt'

export async function POST(req){
    const body = await req.json();
    mongoose.connect(process.env.MONGO_URL);

    const pass = body.password
    if (!pass?.length || pass.length < 5) {
        new Error('Password must be at least five characters');
    } 

    const passwordNothashed = pass;
    const salt = bcrypt.genSaltSync(10);
    body.password = bcrypt.hashSync(passwordNothashed, salt)


    const createdUser = await User.create(body)
    return Response.json(createdUser);
}