Use next-basics package.

This commit is contained in:
Mike Cao 2022-08-28 20:20:54 -07:00
parent 1a6af8fc41
commit f4e0da481e
62 changed files with 255 additions and 373 deletions

View file

@ -1,6 +1,6 @@
import { getAccountById, deleteAccount } from 'queries';
import { useAuth } from 'lib/middleware';
import { methodNotAllowed, ok, unauthorized } from 'lib/response';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
export default async (req, res) => {
await useAuth(req, res);

View file

@ -1,7 +1,6 @@
import { ok, unauthorized, methodNotAllowed, badRequest, hashPassword } from 'next-basics';
import { getAccountById, getAccountByUsername, updateAccount, createAccount } from 'queries';
import { useAuth } from 'lib/middleware';
import { hashPassword } from 'lib/crypto';
import { ok, unauthorized, methodNotAllowed, badRequest } from 'lib/response';
export default async (req, res) => {
await useAuth(req, res);

View file

@ -1,7 +1,13 @@
import { getAccountById, updateAccount } from 'queries';
import { useAuth } from 'lib/middleware';
import { badRequest, methodNotAllowed, ok, unauthorized } from 'lib/response';
import { checkPassword, hashPassword } from 'lib/crypto';
import {
badRequest,
methodNotAllowed,
ok,
unauthorized,
checkPassword,
hashPassword,
} from 'next-basics';
export default async (req, res) => {
await useAuth(req, res);

View file

@ -1,6 +1,6 @@
import { getAccounts } from 'queries';
import { useAuth } from 'lib/middleware';
import { ok, unauthorized, methodNotAllowed } from 'lib/response';
import { ok, unauthorized, methodNotAllowed } from 'next-basics';
export default async (req, res) => {
await useAuth(req, res);

View file

@ -1,6 +1,6 @@
import { checkPassword, createSecureToken } from 'lib/crypto';
import { ok, unauthorized, badRequest, checkPassword, createSecureToken } from 'next-basics';
import { getAccountByUsername } from 'queries/admin/account/getAccountByUsername';
import { ok, unauthorized, badRequest } from 'lib/response';
import { secret } from 'lib/crypto';
export default async (req, res) => {
const { username, password } = req.body;
@ -11,10 +11,10 @@ export default async (req, res) => {
const account = await getAccountByUsername(username);
if (account && (await checkPassword(password, account.password))) {
if (account && checkPassword(password, account.password)) {
const { user_id, username, is_admin } = account;
const user = { user_id, username, is_admin };
const token = await createSecureToken(user);
const token = createSecureToken(user, secret());
return ok(res, { token, user });
}

View file

@ -1,5 +1,5 @@
import { useAuth } from 'lib/middleware';
import { ok, unauthorized } from 'lib/response';
import { ok, unauthorized } from 'next-basics';
export default async (req, res) => {
await useAuth(req, res);

View file

@ -1,12 +1,10 @@
const { Resolver } = require('dns').promises;
import isbot from 'isbot';
import ipaddr from 'ipaddr.js';
import { createToken, unauthorized, send, badRequest, forbidden } from 'next-basics';
import { savePageView, saveEvent } from 'queries';
import { useCors, useSession } from 'lib/middleware';
import { getJsonBody, getIpAddress } from 'lib/request';
import { unauthorized, send, badRequest, forbidden } from 'lib/response';
import { createToken } from 'lib/crypto';
import { removeTrailingSlash } from 'lib/url';
import { uuid } from 'lib/crypto';
export default async (req, res) => {
@ -69,7 +67,7 @@ export default async (req, res) => {
let { url, referrer, event_name, event_data } = payload;
if (process.env.REMOVE_TRAILING_SLASH) {
url = removeTrailingSlash(url);
url = url.replace(/\/$/, '');
}
const event_uuid = uuid();
@ -89,7 +87,7 @@ export default async (req, res) => {
return badRequest(res);
}
const token = await createToken({ website_id, session_id, session_uuid });
const token = createToken({ website_id, session_id, session_uuid });
return send(res, token);
};

View file

@ -1,4 +1,4 @@
import { ok, methodNotAllowed } from 'lib/response';
import { ok, methodNotAllowed } from 'next-basics';
export default async (req, res) => {
if (req.method === 'GET') {

View file

@ -1,4 +1,4 @@
import { ok } from 'lib/response';
import { ok } from 'next-basics';
export default async (req, res) => {
return ok(res, 'nice');

View file

@ -1,8 +1,8 @@
import { subMinutes } from 'date-fns';
import { ok, methodNotAllowed, createToken } from 'next-basics';
import { useAuth } from 'lib/middleware';
import { ok, methodNotAllowed } from 'lib/response';
import { getUserWebsites, getRealtimeData } from 'queries';
import { createToken } from 'lib/crypto';
import { secret } from 'lib/crypto';
export default async (req, res) => {
await useAuth(req, res);
@ -12,7 +12,7 @@ export default async (req, res) => {
const websites = await getUserWebsites(user_id);
const ids = websites.map(({ website_id }) => website_id);
const token = await createToken({ websites: ids });
const token = createToken({ websites: ids }, secret());
const data = await getRealtimeData(ids, subMinutes(new Date(), 30));
return ok(res, {

View file

@ -1,8 +1,8 @@
import { ok, methodNotAllowed, badRequest, parseToken } from 'next-basics';
import { useAuth } from 'lib/middleware';
import { ok, methodNotAllowed, badRequest } from 'lib/response';
import { getRealtimeData } from 'queries';
import { parseToken } from 'lib/crypto';
import { SHARE_TOKEN_HEADER } from 'lib/constants';
import { secret } from 'lib/crypto';
export default async (req, res) => {
await useAuth(req, res);
@ -16,7 +16,7 @@ export default async (req, res) => {
return badRequest(res);
}
const { websites } = await parseToken(token);
const { websites } = parseToken(token, secret());
const data = await getRealtimeData(websites, new Date(+start_at));

View file

@ -1,6 +1,6 @@
import { getWebsiteByShareId } from 'queries';
import { ok, notFound, methodNotAllowed } from 'lib/response';
import { createToken } from 'lib/crypto';
import { ok, notFound, methodNotAllowed, createToken } from 'next-basics';
import { secret } from 'lib/crypto';
export default async (req, res) => {
const { id } = req.query;
@ -10,7 +10,7 @@ export default async (req, res) => {
if (website) {
const websiteId = website.website_id;
const token = await createToken({ website_id: websiteId });
const token = createToken({ website_id: websiteId }, secret());
return ok(res, { websiteId, token });
}

View file

@ -1,4 +1,4 @@
import { methodNotAllowed, ok, unauthorized } from 'lib/response';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { allowQuery } from 'lib/auth';
import { useCors } from 'lib/middleware';
import { getActiveVisitors } from 'queries';

View file

@ -1,6 +1,6 @@
import moment from 'moment-timezone';
import { getEventMetrics } from 'queries';
import { ok, badRequest, methodNotAllowed, unauthorized } from 'lib/response';
import { ok, badRequest, methodNotAllowed, unauthorized } from 'next-basics';
import { allowQuery } from 'lib/auth';
import { useCors } from 'lib/middleware';

View file

@ -1,5 +1,5 @@
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { deleteWebsite, getWebsiteById } from 'queries';
import { methodNotAllowed, ok, unauthorized } from 'lib/response';
import { allowQuery } from 'lib/auth';
import { useCors } from 'lib/middleware';

View file

@ -1,5 +1,5 @@
import { getPageviewMetrics, getSessionMetrics, getWebsiteById } from 'queries';
import { ok, methodNotAllowed, unauthorized, badRequest } from 'lib/response';
import { ok, methodNotAllowed, unauthorized, badRequest } from 'next-basics';
import { allowQuery } from 'lib/auth';
import { useCors } from 'lib/middleware';
import { FILTER_IGNORED } from 'lib/constants';

View file

@ -1,6 +1,6 @@
import moment from 'moment-timezone';
import { getPageviewStats } from 'queries';
import { ok, badRequest, methodNotAllowed, unauthorized } from 'lib/response';
import { ok, badRequest, methodNotAllowed, unauthorized } from 'next-basics';
import { allowQuery } from 'lib/auth';
import { useCors } from 'lib/middleware';

View file

@ -1,5 +1,5 @@
import { resetWebsite } from 'queries';
import { methodNotAllowed, ok, unauthorized } from 'lib/response';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { allowQuery } from 'lib/auth';
export default async (req, res) => {

View file

@ -1,5 +1,5 @@
import { getWebsiteStats } from 'queries';
import { methodNotAllowed, ok, unauthorized } from 'lib/response';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { allowQuery } from 'lib/auth';
import { useCors } from 'lib/middleware';

View file

@ -1,7 +1,7 @@
import { ok, unauthorized, methodNotAllowed, getRandomChars } from 'next-basics';
import { updateWebsite, createWebsite, getWebsiteById } from 'queries';
import { useAuth } from 'lib/middleware';
import { uuid, getRandomChars } from 'lib/crypto';
import { ok, unauthorized, methodNotAllowed } from 'lib/response';
import { uuid } from 'lib/crypto';
export default async (req, res) => {
await useAuth(req, res);

View file

@ -1,6 +1,6 @@
import { getAllWebsites, getUserWebsites } from 'queries';
import { useAuth } from 'lib/middleware';
import { ok, methodNotAllowed, unauthorized } from 'lib/response';
import { ok, methodNotAllowed, unauthorized } from 'next-basics';
export default async (req, res) => {
await useAuth(req, res);