-
Notifications
You must be signed in to change notification settings - Fork 225
Open
Description
- RegisterPage
Change handleFormSubmit method
async function handleFormSubmit(ev) {
ev.preventDefault();
setCreatingUser(true);
setError(false);
setUserCreated(false);
let response;
await fetch("/api/register", {
method: "POST",
body: JSON.stringify({ email, password }),
headers: { "Content-Type": "application/json" },
})
.then((res) => res.json())
.then((res) => {
console.log("Response: ", res);
response = res;
})
.catch((err) => {
console.error("Error: ", err);
response = err;
});
if (response._id) {
setUserCreated(true);
} else {
setError(true);
}
setCreatingUser(false);
}
- api/register/route.js
Update POST method
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) {
return Response.json(
{
message: "Password must be at least 5 characters",
status: 400,
ok: false
},
{
status: 400,
}
);
}
const notHashedPassword = pass;
const salt = bcrypt.genSaltSync(10);
body.password = bcrypt.hashSync(notHashedPassword, salt);
const createdUser = await User.create(body);
return Response.json(createdUser);
}
Metadata
Metadata
Assignees
Labels
No labels