signup and loginbasics

This commit is contained in:
Timur Ercan
2023-01-10 18:52:04 +01:00
parent 93d96c3768
commit 58fbaab935
8 changed files with 671 additions and 142 deletions

View File

@ -0,0 +1,58 @@
import { IdentityProvider } from "@prisma/client";
import { NextApiRequest, NextApiResponse } from "next";
import prisma from "@documenso/prisma";
import { hashPassword } from "@documenso/lib/auth";
import { defaultHandler, defaultResponder } from "@documenso/lib/server";
async function postHandler(req: NextApiRequest, res: NextApiResponse) {
const data = req.body;
const { email, password } = data;
const cleanEmail = email.toLowerCase();
if (!cleanEmail || !cleanEmail.includes("@")) {
res.status(422).json({ message: "Invalid email" });
return;
}
if (!password || password.trim().length < 7) {
res.status(422).json({
message: "Invalid input - password should be at least 7 characters long.",
});
return;
}
// User already exists if email already exists
const existingUser = await prisma.user.findFirst({
where: {
email: cleanEmail,
},
});
if (existingUser) {
const message: string = "Email address is already registered";
return res.status(409).json({ message });
}
const hashedPassword = await hashPassword(password);
const user = await prisma.user.upsert({
where: { email: cleanEmail },
update: {
password: hashedPassword,
emailVerified: new Date(Date.now()),
identityProvider: IdentityProvider.DOCUMENSO,
},
create: {
email: cleanEmail,
password: hashedPassword,
identityProvider: IdentityProvider.DOCUMENSO,
},
});
res.status(201).json({ message: "Created user" });
}
export default defaultHandler({
POST: Promise.resolve({ default: defaultResponder(postHandler) }),
});