
* run `yarn lint --fix`
* Revert "Revert "add linting to ci""
This reverts commit 0bbbbee4be
.
* Fixed some errors
* remove unused code - not sure why this was here?
* assert env var
* more type fixes
* fix typings og gcal callback - needs testing
* rename `md5.ts` to `md5.js`
it is js.
* fix types
* fix types
* fix lint errors
* fix last lint error
Co-authored-by: Alex van Andel <me@alexvanandel.com>
71 lines
1.5 KiB
TypeScript
71 lines
1.5 KiB
TypeScript
import prisma from "../../../lib/prisma";
|
|
import { hashPassword } from "../../../lib/auth";
|
|
|
|
export default async function handler(req, res) {
|
|
if (req.method !== "POST") {
|
|
return;
|
|
}
|
|
|
|
const data = req.body;
|
|
const { username, email, password } = data;
|
|
|
|
if (!username) {
|
|
res.status(422).json({ message: "Invalid username" });
|
|
return;
|
|
}
|
|
|
|
if (!email || !email.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;
|
|
}
|
|
|
|
const existingUser = await prisma.user.findFirst({
|
|
where: {
|
|
OR: [
|
|
{
|
|
username: username,
|
|
},
|
|
{
|
|
email: email,
|
|
},
|
|
],
|
|
AND: [
|
|
{
|
|
emailVerified: {
|
|
not: null,
|
|
},
|
|
},
|
|
],
|
|
},
|
|
});
|
|
|
|
if (existingUser) {
|
|
const message: string =
|
|
existingUser.email !== email ? "Username already taken" : "Email address is already registered";
|
|
|
|
return res.status(409).json({ message });
|
|
}
|
|
|
|
const hashedPassword = await hashPassword(password);
|
|
|
|
await prisma.user.upsert({
|
|
where: { email },
|
|
update: {
|
|
username,
|
|
password: hashedPassword,
|
|
emailVerified: new Date(Date.now()),
|
|
},
|
|
create: {
|
|
username,
|
|
email,
|
|
password: hashedPassword,
|
|
},
|
|
});
|
|
|
|
res.status(201).json({ message: "Created user" });
|
|
}
|