mirror of
https://github.com/kremalicious/umami.git
synced 2024-11-16 02:05:04 +01:00
06bebadbb9
* Auth checkpoint. * Merge branch 'dev' into feat/um-114-roles-and-permissions
66 lines
1.5 KiB
TypeScript
66 lines
1.5 KiB
TypeScript
import { Prisma } from '@prisma/client';
|
|
import { NextApiRequestQueryBody } from 'interface/api/nextApi';
|
|
import { checkAdmin } from 'lib/auth';
|
|
import { uuid } from 'lib/crypto';
|
|
import { useAuth, useCors } from 'lib/middleware';
|
|
import { NextApiResponse } from 'next';
|
|
import { methodNotAllowed, ok } from 'next-basics';
|
|
import { createWebsite, getAllWebsites, getWebsitesByUserId } from 'queries';
|
|
|
|
export interface WebsitesRequestQuery {
|
|
include_all?: boolean;
|
|
}
|
|
|
|
export interface WebsitesRequestBody {
|
|
name: string;
|
|
domain: string;
|
|
shareId: string;
|
|
teamId?: string;
|
|
}
|
|
|
|
export default async (
|
|
req: NextApiRequestQueryBody<WebsitesRequestQuery, WebsitesRequestBody>,
|
|
res: NextApiResponse,
|
|
) => {
|
|
await useCors(req, res);
|
|
await useAuth(req, res);
|
|
|
|
const {
|
|
user: { id: userId },
|
|
} = req.auth;
|
|
|
|
if (req.method === 'GET') {
|
|
const { include_all } = req.query;
|
|
|
|
const isAdmin = await checkAdmin(userId);
|
|
|
|
const websites =
|
|
isAdmin && include_all ? await getAllWebsites() : await getWebsitesByUserId(userId);
|
|
|
|
return ok(res, websites);
|
|
}
|
|
|
|
if (req.method === 'POST') {
|
|
const { name, domain, shareId, teamId } = req.body;
|
|
|
|
const data: Prisma.WebsiteUncheckedCreateInput = {
|
|
id: uuid(),
|
|
name,
|
|
domain,
|
|
shareId,
|
|
};
|
|
|
|
if (teamId) {
|
|
data.teamId = teamId;
|
|
} else {
|
|
data.userId = userId;
|
|
}
|
|
|
|
const website = await createWebsite(data);
|
|
|
|
return ok(res, website);
|
|
}
|
|
|
|
return methodNotAllowed(res);
|
|
};
|