Use token authentication for API requests.

This commit is contained in:
Mike Cao 2020-09-17 22:52:20 -07:00
parent bff8806b61
commit 96bd7e5b47
34 changed files with 198 additions and 153 deletions

View file

@ -1,30 +1,30 @@
import { deleteWebsite, getWebsiteById } from 'lib/queries';
import { useAuth } from 'lib/middleware';
import { methodNotAllowed, ok, unauthorized } from 'lib/response';
import { allowQuery } from 'lib/auth';
export default async (req, res) => {
await useAuth(req, res);
const { id } = req.query;
const { user_id, is_admin } = req.auth;
const { id, share_id } = req.query;
const websiteId = +id;
const website = await getWebsiteById(websiteId);
if (req.method === 'GET') {
if (is_admin || website.user_id === user_id || (share_id && website.share_id === share_id)) {
return ok(res, website);
if (!(await allowQuery(req))) {
return unauthorized(res);
}
return unauthorized(res);
const website = await getWebsiteById(websiteId);
return ok(res, website);
}
if (req.method === 'DELETE') {
if (is_admin || website.user_id === user_id) {
await deleteWebsite(websiteId);
return ok(res);
if (!(await allowQuery(req, true))) {
return unauthorized(res);
}
return unauthorized(res);
await deleteWebsite(websiteId);
return ok(res);
}
return methodNotAllowed(res);