# Conflicts:
#	pages/api/account/index.js
This commit is contained in:
Mike Cao 2022-10-03 17:17:53 -07:00
commit d784b2a8db
31 changed files with 178 additions and 149 deletions

View file

@ -1,6 +1,7 @@
import { getAllWebsites, getUserWebsites } from 'queries';
import { createWebsite, getAllWebsites, getUserWebsites } from 'queries';
import { ok, methodNotAllowed, unauthorized, getRandomChars } from 'next-basics';
import { useAuth } from 'lib/middleware';
import { ok, methodNotAllowed, unauthorized } from 'next-basics';
import { uuid } from 'lib/crypto';
export default async (req, res) => {
await useAuth(req, res);
@ -22,5 +23,24 @@ export default async (req, res) => {
return ok(res, websites);
}
if (req.method === 'POST') {
await useAuth(req, res);
const { is_admin: currentUserIsAdmin, user_id: currentUserId } = req.auth;
const { name, domain, owner, enable_share_url } = req.body;
const website_owner = +owner;
if (website_owner !== currentUserId && !currentUserIsAdmin) {
return unauthorized(res);
}
const website_uuid = uuid();
const share_id = enable_share_url ? getRandomChars(8) : null;
const website = await createWebsite(website_owner, { website_uuid, name, domain, share_id });
return ok(res, website);
}
return methodNotAllowed(res);
};