umami/pages/api/share/[id].js

24 lines
549 B
JavaScript
Raw Normal View History

import { getWebsite } from 'queries';
2022-08-29 05:20:54 +02:00
import { ok, notFound, methodNotAllowed, createToken } from 'next-basics';
import { secret } from 'lib/crypto';
2020-08-15 10:17:15 +02:00
export default async (req, res) => {
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());
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);
};