diff --git a/components/Login.js b/components/Login.js
index a2579aec..f4e12a37 100644
--- a/components/Login.js
+++ b/components/Login.js
@@ -23,7 +23,7 @@ export default function Login() {
const response = await post('/api/auth', { username, password });
if (response?.token) {
- await Router.push('/admin');
+ await Router.push('/');
} else {
setMessage('Incorrect username/password.');
}
diff --git a/lib/db.js b/lib/db.js
index 6fa92ce8..29eaf809 100644
--- a/lib/db.js
+++ b/lib/db.js
@@ -112,3 +112,13 @@ export async function getAccount(username = '') {
}),
);
}
+
+export async function getPageviews(website_id) {
+ return runQuery(
+ prisma.pageview.findMany({
+ where: {
+ website_id,
+ },
+ }),
+ );
+}
diff --git a/lib/middleware.js b/lib/middleware.js
index e2144e74..3c644629 100644
--- a/lib/middleware.js
+++ b/lib/middleware.js
@@ -12,9 +12,4 @@ export function use(middleware) {
});
}
-export const allowPost = use(
- cors({
- origin: '*',
- methods: ['POST', 'OPTIONS'],
- }),
-);
+export const useCors = use(cors());
diff --git a/pages/admin.js b/pages/admin.js
deleted file mode 100644
index 4ed33b4d..00000000
--- a/pages/admin.js
+++ /dev/null
@@ -1,36 +0,0 @@
-import React from 'react';
-import cookies from 'next-cookies';
-import Layout from 'components/Layout';
-import { verifySecureToken } from 'lib/crypto';
-
-export default function Admin({ username }) {
- return (
-
-
- You've successfully logged in as {username}.
-
-
- );
-}
-
-export async function getServerSideProps(context) {
- const token = cookies(context)['umami.auth'];
-
- try {
- const payload = await verifySecureToken(token);
-
- return {
- props: {
- username: payload.username,
- },
- };
- } catch {
- const { res } = context;
-
- res.statusCode = 303;
- res.setHeader('Location', '/');
- res.end();
- }
-
- return { props: {} };
-}
diff --git a/pages/api/auth.js b/pages/api/auth.js
index 55b5e3c1..bd50a801 100644
--- a/pages/api/auth.js
+++ b/pages/api/auth.js
@@ -1,11 +1,8 @@
import { serialize } from 'cookie';
import { checkPassword, createSecureToken } from 'lib/crypto';
import { getAccount } from 'lib/db';
-import { allowPost } from 'lib/middleware';
export default async (req, res) => {
- await allowPost(req, res);
-
const { username, password } = req.body;
const account = await getAccount(username);
@@ -21,7 +18,7 @@ export default async (req, res) => {
res.setHeader('Set-Cookie', [cookie]);
- res.status(200).send({ token });
+ res.status(200).json({ token });
} else {
res.status(401).end();
}
diff --git a/pages/api/collect.js b/pages/api/collect.js
index cb3c15c5..9ad8bc95 100644
--- a/pages/api/collect.js
+++ b/pages/api/collect.js
@@ -1,10 +1,10 @@
import { savePageView, saveEvent } from 'lib/db';
-import { allowPost } from 'lib/middleware';
+import { useCors } from 'lib/middleware';
import checkSession from 'lib/session';
import { createToken } from 'lib/crypto';
export default async (req, res) => {
- await allowPost(req, res);
+ await useCors(req, res);
const session = await checkSession(req);
diff --git a/pages/api/verify.js b/pages/api/verify.js
index 8c0bd532..4088172e 100644
--- a/pages/api/verify.js
+++ b/pages/api/verify.js
@@ -5,7 +5,7 @@ export default async (req, res) => {
try {
const payload = await verifySecureToken(token);
- res.status(200).send(payload);
+ res.status(200).json(payload);
} catch {
res.status(400).end();
}
diff --git a/pages/index.js b/pages/index.js
index 7bc8402f..c9a58dfd 100644
--- a/pages/index.js
+++ b/pages/index.js
@@ -1,17 +1,40 @@
import React from 'react';
import Link from 'next/link';
+import cookies from 'next-cookies';
import Layout from 'components/Layout';
-import Login from 'components/Login';
+import { verifySecureToken } from 'lib/crypto';
-export default function Home() {
+export default function HomePage({ username }) {
return (
-
-
-
- Test page 🡒
-
-
+
+ You've successfully logged in as {username}.
+
+
+ Logout 🡒
+
);
}
+
+export async function getServerSideProps(context) {
+ const token = cookies(context)['umami.auth'];
+
+ try {
+ const payload = await verifySecureToken(token);
+
+ return {
+ props: {
+ username: payload.username,
+ },
+ };
+ } catch {
+ const { res } = context;
+
+ res.statusCode = 303;
+ res.setHeader('Location', '/login');
+ res.end();
+ }
+
+ return { props: {} };
+}
diff --git a/pages/login.js b/pages/login.js
new file mode 100644
index 00000000..87c6c9af
--- /dev/null
+++ b/pages/login.js
@@ -0,0 +1,17 @@
+import React from 'react';
+import Link from 'next/link';
+import Layout from 'components/Layout';
+import Login from 'components/Login';
+
+export default function LoginPage() {
+ return (
+
+
+
+
+ Test page 🡒
+
+
+
+ );
+}
diff --git a/pages/logout.js b/pages/logout.js
new file mode 100644
index 00000000..d31dd48a
--- /dev/null
+++ b/pages/logout.js
@@ -0,0 +1,27 @@
+import React from 'react';
+import { serialize } from 'cookie';
+import Layout from 'components/Layout';
+
+export default function LogoutPage() {
+ return (
+
+ You've successfully logged out..
+
+ );
+}
+
+export async function getServerSideProps({ res }) {
+ const cookie = serialize('umami.auth', '', {
+ path: '/',
+ httpOnly: true,
+ maxAge: 0,
+ });
+
+ res.statusCode = 303;
+ res.setHeader('Set-Cookie', [cookie]);
+ res.setHeader('Location', '/login');
+
+ res.end();
+
+ return { props: {} };
+}