2022-12-07 03:36:41 +01:00
|
|
|
import { NextApiRequestQueryBody } from 'lib/types';
|
2022-08-29 05:20:54 +02:00
|
|
|
import { secret } from 'lib/crypto';
|
2022-11-15 22:21:14 +01:00
|
|
|
import { NextApiResponse } from 'next';
|
|
|
|
import { createToken, methodNotAllowed, notFound, ok } from 'next-basics';
|
|
|
|
import { getWebsite } from 'queries';
|
|
|
|
|
|
|
|
export interface ShareRequestQuery {
|
|
|
|
id: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ShareResponse {
|
|
|
|
id: string;
|
|
|
|
token: string;
|
|
|
|
}
|
2020-08-15 10:17:15 +02:00
|
|
|
|
2022-11-15 22:21:14 +01:00
|
|
|
export default async (
|
|
|
|
req: NextApiRequestQueryBody<ShareRequestQuery>,
|
|
|
|
res: NextApiResponse<ShareResponse>,
|
|
|
|
) => {
|
2022-11-01 07:42:37 +01:00
|
|
|
const { id: shareId } = req.query;
|
2020-08-15 10:17:15 +02:00
|
|
|
|
|
|
|
if (req.method === 'GET') {
|
2022-11-01 07:42:37 +01:00
|
|
|
const website = await getWebsite({ shareId });
|
2020-08-15 10:17:15 +02:00
|
|
|
|
|
|
|
if (website) {
|
2022-11-01 07:42:37 +01:00
|
|
|
const { id } = website;
|
|
|
|
const data = { id };
|
2022-10-25 04:48:10 +02:00
|
|
|
const token = createToken(data, secret());
|
2020-09-18 07:52:20 +02:00
|
|
|
|
2022-10-25 04:48:10 +02:00
|
|
|
return ok(res, { ...data, token });
|
2020-08-15 10:17:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return notFound(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
return methodNotAllowed(res);
|
|
|
|
};
|