Fixed change password issue. API refactoring. Closes #1592.

This commit is contained in:
Mike Cao 2022-10-25 10:45:56 -07:00
parent 994cf950c8
commit ac070d3ce9
24 changed files with 74 additions and 111 deletions

View file

@ -2,13 +2,14 @@ import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { allowQuery } from 'lib/auth';
import { useAuth, useCors } from 'lib/middleware';
import { getActiveVisitors } from 'queries';
import { TYPE_WEBSITE } from 'lib/constants';
export default async (req, res) => {
await useCors(req, res);
await useAuth(req, res);
if (req.method === 'GET') {
if (!(await allowQuery(req))) {
if (!(await allowQuery(req, TYPE_WEBSITE))) {
return unauthorized(res);
}

View file

@ -3,13 +3,14 @@ import { getEventData } from 'queries';
import { ok, badRequest, methodNotAllowed, unauthorized } from 'next-basics';
import { allowQuery } from 'lib/auth';
import { useAuth, useCors } from 'lib/middleware';
import { TYPE_WEBSITE } from 'lib/constants';
export default async (req, res) => {
await useCors(req, res);
await useAuth(req, res);
if (req.method === 'POST') {
if (!(await allowQuery(req))) {
if (!(await allowQuery(req, TYPE_WEBSITE))) {
return unauthorized(res);
}

View file

@ -3,6 +3,7 @@ import { getEventMetrics } from 'queries';
import { ok, badRequest, methodNotAllowed, unauthorized } from 'next-basics';
import { allowQuery } from 'lib/auth';
import { useAuth, useCors } from 'lib/middleware';
import { TYPE_WEBSITE } from 'lib/constants';
const unitTypes = ['year', 'month', 'hour', 'day'];
@ -11,7 +12,7 @@ export default async (req, res) => {
await useAuth(req, res);
if (req.method === 'GET') {
if (!(await allowQuery(req))) {
if (!(await allowQuery(req, TYPE_WEBSITE))) {
return unauthorized(res);
}

View file

@ -2,19 +2,20 @@ import { allowQuery } from 'lib/auth';
import { useAuth, useCors } from 'lib/middleware';
import { getRandomChars, methodNotAllowed, ok, serverError, unauthorized } from 'next-basics';
import { deleteWebsite, getAccount, getWebsite, updateWebsite } from 'queries';
import { TYPE_WEBSITE } from 'lib/constants';
export default async (req, res) => {
await useCors(req, res);
await useAuth(req, res);
const { id: websiteId } = req.query;
const { id: websiteUuid } = req.query;
if (!(await allowQuery(req))) {
if (!(await allowQuery(req, TYPE_WEBSITE))) {
return unauthorized(res);
}
if (req.method === 'GET') {
const website = await getWebsite({ websiteUuid: websiteId });
const website = await getWebsite({ websiteUuid });
return ok(res, website);
}
@ -32,7 +33,7 @@ export default async (req, res) => {
}
}
const website = await getWebsite({ websiteUuid: websiteId });
const website = await getWebsite({ websiteUuid });
const newShareId = enableShareUrl ? website.shareId || getRandomChars(8) : null;
@ -44,7 +45,7 @@ export default async (req, res) => {
shareId: shareId ? shareId : newShareId,
userId: account ? account.id : +owner || undefined,
},
{ websiteUuid: websiteId },
{ websiteUuid },
);
} catch (e) {
if (e.message.includes('Unique constraint') && e.message.includes('share_id')) {
@ -56,11 +57,11 @@ export default async (req, res) => {
}
if (req.method === 'DELETE') {
if (!(await allowQuery(req, true))) {
if (!(await allowQuery(req, TYPE_WEBSITE))) {
return unauthorized(res);
}
await deleteWebsite(websiteId);
await deleteWebsite(websiteUuid);
return ok(res);
}

View file

@ -1,8 +1,8 @@
import { allowQuery } from 'lib/auth';
import { FILTER_IGNORED } from 'lib/constants';
import { FILTER_IGNORED, TYPE_WEBSITE } from 'lib/constants';
import { useAuth, useCors } from 'lib/middleware';
import { badRequest, methodNotAllowed, ok, unauthorized } from 'next-basics';
import { getPageviewMetrics, getSessionMetrics, getWebsiteByUuid } from 'queries';
import { getPageviewMetrics, getSessionMetrics, getWebsite } from 'queries';
const sessionColumns = ['browser', 'os', 'device', 'screen', 'country', 'language'];
const pageviewColumns = ['url', 'referrer', 'query'];
@ -38,7 +38,7 @@ export default async (req, res) => {
await useAuth(req, res);
if (req.method === 'GET') {
if (!(await allowQuery(req))) {
if (!(await allowQuery(req, TYPE_WEBSITE))) {
return unauthorized(res);
}
@ -94,7 +94,7 @@ export default async (req, res) => {
let domain;
if (type === 'referrer') {
const website = await getWebsiteByUuid(websiteId);
const website = await getWebsite({ websiteUuid: websiteId });
if (!website) {
return badRequest(res);

View file

@ -3,6 +3,7 @@ import { getPageviewStats } from 'queries';
import { ok, badRequest, methodNotAllowed, unauthorized } from 'next-basics';
import { allowQuery } from 'lib/auth';
import { useAuth, useCors } from 'lib/middleware';
import { TYPE_WEBSITE } from 'lib/constants';
const unitTypes = ['year', 'month', 'hour', 'day'];
@ -11,7 +12,7 @@ export default async (req, res) => {
await useAuth(req, res);
if (req.method === 'GET') {
if (!(await allowQuery(req))) {
if (!(await allowQuery(req, TYPE_WEBSITE))) {
return unauthorized(res);
}

View file

@ -2,6 +2,7 @@ import { resetWebsite } from 'queries';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { allowQuery } from 'lib/auth';
import { useAuth, useCors } from 'lib/middleware';
import { TYPE_WEBSITE } from 'lib/constants';
export default async (req, res) => {
await useCors(req, res);
@ -10,7 +11,7 @@ export default async (req, res) => {
const { id: websiteId } = req.query;
if (req.method === 'POST') {
if (!(await allowQuery(req))) {
if (!(await allowQuery(req, TYPE_WEBSITE))) {
return unauthorized(res);
}

View file

@ -2,13 +2,14 @@ import { getWebsiteStats } from 'queries';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { allowQuery } from 'lib/auth';
import { useAuth, useCors } from 'lib/middleware';
import { TYPE_WEBSITE } from 'lib/constants';
export default async (req, res) => {
await useCors(req, res);
await useAuth(req, res);
if (req.method === 'GET') {
if (!(await allowQuery(req))) {
if (!(await allowQuery(req, TYPE_WEBSITE))) {
return unauthorized(res);
}