import { connectDB } from "../../../../lib/mongoose";
import { User, userValidation, loginValidation } from "../../../../models/User";
import { BadRequestError } from "../../../../lib/ErrorHandler";
import asyncHandler from "../../../../lib/asyncHandler";
import { NextRequest, NextResponse } from "next/server";
import { S3 } from "@aws-sdk/client-s3";
const s3 = new S3({
region: process.env.AWS_REGION,
});
export const POST = asyncHandler(async (req: NextRequest) => {
await connectDB(process.env.MONGODB_URI);
const data = await req.json();
const { email, firstName, lastName, password, image } = data;
console.log('from routes', data);
const { error } = userValidation.validate(data);
if (error) throw new BadRequestError(error.details[0].message);
const isEmailExist = await User.findOne({ email });
if (isEmailExist) {
throw new BadRequestError("email is already exist");
}
// if (!image) throw new BadRequestError("Image is required.");
// if (!["image/jpeg", "image/png"].includes(image.type.toLowerCase())) {
// throw new BadRequestError(
// "Invalid image type. Only JPEG and PNG are allowed."
// );
// }
// if (image.size > 1024 * 1024 * 5) {
// throw new BadRequestError("Image size should not exceed 5MB.");
// }
const extension = image.name.split(".").pop();
const fileName = `${firstName}.${lastName}.${extension}`;
const bufferedImage = await image.arrayBuffer();
await s3.putObject({
Bucket: process.env.AWS_BUCKET,
Key: fileName,
Body: Buffer.from(bufferedImage),
ContentType: image.type,
});
const user = new User({
firstName,
lastName,
email,
password,
image: fileName,
});
await user.save();
return NextResponse.json(
{
message: `Success Signing Up!`,
success: true,
user,
},
{ status: 201 }
);
});
"use server";
import { customFetch } from "./customFetch";
export const registerNewUser = async (prevState: any, formData: FormData) => {
const user = {
firstName: formData.get("firstName") as string,
lastName: formData.get("lastName") as string,
password: formData.get("password") as string,
confirmPassword: formData.get("confirmPassword") as string,
email: formData.get("email") as string,
image: formData.get("image"),
};
console.log("from actions", user);
try {
const { data } = await customFetch.post("/api/auth/sign-up", user);
return data;
} catch (error: any) {
console.log(error.response.data.error);
return { message: error.response.data.error };
}
};
Im trying to upload an image in the sign-up form to AWS, but I cant do any action on the image in my route, this is the console logs :
from actions {
firstName: 'Gal',
lastName: 'Parselany',
password: '123456',
confirmPassword: '123456',
email: '[email protected]',
image: File {
size: 201120,
type: 'image/jpeg',
name: 'style4.jpeg',
lastModified: 1713265707481
}
}
from action {
firstName: 'Gal',
lastName: 'Parselany',
password: '123456',
confirmPassword: '123456',
email: '[email protected]',
image: {}
}
as you can see,I can see the image object in my actions, but in my route.ts i dont see it at all, I want to add the image to my user model, but right now i dont have anything wrote in my image prop as you can see
can someOne help me solve it? thanks in advance