Enable public website sharing.

This commit is contained in:
Mike Cao 2020-08-15 01:17:15 -07:00
parent 48a524e09c
commit 560f1316c1
36 changed files with 294 additions and 61 deletions

18
pages/api/share/[id].js Normal file
View file

@ -0,0 +1,18 @@
import { getWebsiteByShareId } from 'lib/queries';
import { ok, notFound, methodNotAllowed } from 'lib/response';
export default async (req, res) => {
const { id } = req.query;
if (req.method === 'GET') {
const website = await getWebsiteByShareId(id);
if (website) {
return ok(res, website);
}
return notFound(res);
}
return methodNotAllowed(res);
};

View file

@ -1,13 +0,0 @@
import { parseSecureToken } from 'lib/crypto';
import { ok, badRequest } from 'lib/response';
export default async (req, res) => {
const { token } = req.body;
try {
const payload = await parseSecureToken(token);
return ok(res, payload);
} catch {
return badRequest(res);
}
};

View file

@ -1,22 +1,31 @@
import { updateWebsite, createWebsite, getWebsiteById } from 'lib/queries';
import { useAuth } from 'lib/middleware';
import { uuid } from 'lib/crypto';
import { uuid, getRandomChars } from 'lib/crypto';
import { ok, unauthorized, methodNotAllowed } from 'lib/response';
export default async (req, res) => {
await useAuth(req, res);
const { user_id, is_admin } = req.auth;
const { website_id } = req.body;
const { website_id, make_public } = req.body;
if (req.method === 'POST') {
const { name, domain } = req.body;
if (website_id) {
const website = getWebsiteById(website_id);
const website = await getWebsiteById(website_id);
if (website.user_id === user_id || is_admin) {
await updateWebsite(website_id, { name, domain });
let { share_id } = website;
console.log('exising id', share_id, website);
if (make_public) {
share_id = share_id ? share_id : getRandomChars(8);
} else {
share_id = null;
}
await updateWebsite(website_id, { name, domain, share_id });
return ok(res);
}
@ -24,7 +33,8 @@ export default async (req, res) => {
return unauthorized(res);
} else {
const website_uuid = uuid();
const website = await createWebsite(user_id, { website_uuid, name, domain });
const share_id = make_public ? getRandomChars(8) : null;
const website = await createWebsite(user_id, { website_uuid, name, domain, share_id });
return ok(res, website);
}

View file

@ -3,9 +3,6 @@ import { useAuth } from 'lib/middleware';
import { methodNotAllowed, ok, unauthorized } from 'lib/response';
export default async (req, res) => {
await useAuth(req, res);
const { user_id, is_admin } = req.auth;
const { id } = req.query;
const website_id = +id;
@ -16,6 +13,9 @@ export default async (req, res) => {
}
if (req.method === 'DELETE') {
await useAuth(req, res);
const { user_id, is_admin } = req.auth;
const website = await getWebsiteById(website_id);
if (website.user_id === user_id || is_admin) {

View file

@ -1,10 +1,7 @@
import { getMetrics } from 'lib/queries';
import { useAuth } from 'lib/middleware';
import { ok } from 'lib/response';
export default async (req, res) => {
await useAuth(req, res);
const { id, start_at, end_at } = req.query;
const metrics = await getMetrics(+id, new Date(+start_at), new Date(+end_at));

View file

@ -1,13 +1,10 @@
import moment from 'moment-timezone';
import { getPageviews } from 'lib/queries';
import { useAuth } from 'lib/middleware';
import { ok, badRequest } from 'lib/response';
const unitTypes = ['month', 'hour', 'day'];
export default async (req, res) => {
await useAuth(req, res);
const { id, start_at, end_at, unit, tz } = req.query;
if (!moment.tz.zone(tz) || !unitTypes.includes(unit)) {

View file

@ -1,13 +1,10 @@
import { getRankings } from 'lib/queries';
import { useAuth } from 'lib/middleware';
import { ok, badRequest } from 'lib/response';
const sessionColumns = ['browser', 'os', 'device', 'country'];
const pageviewColumns = ['url', 'referrer'];
export default async (req, res) => {
await useAuth(req, res);
const { id, type, start_at, end_at } = req.query;
if (!sessionColumns.includes(type) && !pageviewColumns.includes(type)) {