Implements inviting external users to teams

This commit is contained in:
Alex van Andel committed 2021-06-09 21:43:37 +00:00
1 parent 4448175377
commit 40eb9036f4
6 files changed
+300 -81

No files matched your search

+59 -43
View File
@@ -2,55 +2,71 @@ import prisma from '../../../lib/prisma';
import { hashPassword } from "../../../lib/auth";
export default async function handler(req, res) {
if (req.method !== 'POST') {
return;
}
if (req.method !== 'POST') {
return;
}
const data = req.body;
const { username, email, password } = data;
const data = req.body;
const { username, email, password } = data;
if (!username) {
res.status(422).json({message: 'Invalid username'});
return;
}
if (!username) {
res.status(422).json({message: 'Invalid username'});
return;
}
if (!email || !email.includes('@')) {
res.status(422).json({message: 'Invalid email'});
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;
}
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
}
]
const existingUser = await prisma.user.findFirst({
where: {
OR: [
{
username: username
},
{
email: email
}
});
if (existingUser) {
res.status(422).json({message: 'A user exists with that username or email address'});
return;
}
const hashedPassword = await hashPassword(password);
const user = await prisma.user.create({
data: {
username,
email,
password: hashedPassword
],
AND: [
{
emailVerified: {
not: null,
},
}
});
]
}
});
res.status(201).json({message: 'Created user'});
if (existingUser) {
let message: string = (
existingUser.email !== email
) ? 'Username already taken' : 'Email address is already registered';
return res.status(409).json({message});
}
const hashedPassword = await hashPassword(password);
const user = 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'});
}