umami/pages/api/users/[id]/websites.ts

35 lines
829 B
TypeScript
Raw Normal View History

2022-12-07 03:36:41 +01:00
import { useAuth, useCors } from 'lib/middleware';
import { NextApiRequestQueryBody } from 'lib/types';
2022-12-07 03:36:41 +01:00
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
2023-02-08 01:29:25 +01:00
import { getUserWebsites } from 'queries';
2022-12-07 03:36:41 +01:00
2023-05-04 02:17:57 +02:00
export interface UserWebsitesRequestBody {
2022-12-07 03:36:41 +01:00
name: string;
domain: string;
shareId: string;
}
export default async (
2023-05-04 02:17:57 +02:00
req: NextApiRequestQueryBody<any, UserWebsitesRequestBody>,
2022-12-07 03:36:41 +01:00
res: NextApiResponse,
) => {
await useCors(req, res);
await useAuth(req, res);
2023-02-08 01:29:25 +01:00
const { user } = req.auth;
const { id: userId } = req.query;
2022-12-07 03:36:41 +01:00
if (req.method === 'GET') {
2023-02-08 01:29:25 +01:00
if (!user.isAdmin && user.id !== userId) {
return unauthorized(res);
}
2023-02-08 01:29:25 +01:00
const websites = await getUserWebsites(userId);
2022-12-07 03:36:41 +01:00
2023-02-08 01:29:25 +01:00
return ok(res, websites);
2022-12-07 03:36:41 +01:00
}
return methodNotAllowed(res);
};