⚡️⚡️⚡️ Self-hosted collaborative bookmark manager to collect, read, annotate, and fully preserve what matters, all in one place.
TypeScript bookmarkbookmark-managercollaborationnextjsreact-nativeread-it-laterself-hostedtypescript
basic user listing
daniel31x137856e76b15c1
diff --git a/.env.sample b/.env.sampleindex 0e692b0d..a5796351 100644--- a/.env.sample+++ b/.env.sample@@ -22,6 +22,7 @@ BROWSER_TIMEOUT=IGNORE_UNAUTHORIZED_CA=IGNORE_HTTPS_ERRORS=IGNORE_URL_SIZE_LIMIT=+ADMINISTRATOR=# AWS S3 SettingsSPACES_KEY=diff --git a/lib/api/controllers/users/getUsers.ts b/lib/api/controllers/users/getUsers.tsnew file mode 100644index 00000000..dd2b1686--- /dev/null+++ b/lib/api/controllers/users/getUsers.ts@@ -0,0 +1,22 @@+import { prisma } from "@/lib/api/db";++export default async function getUsers() {+ // Get all users+ const users = await prisma.user.findMany({+ select: {+ id: true,+ username: true,+ email: true,+ emailVerified: true,+ subscriptions: {+ select: {+ active: true,+ },+ },+ createdAt: true,+ updatedAt: true,+ },+ });++ return { response: users, status: 200 };+}diff --git a/pages/admin.tsx b/pages/admin.tsxnew file mode 100644index 00000000..6624fe33--- /dev/null+++ b/pages/admin.tsx@@ -0,0 +1,77 @@+import { User as U } from "@prisma/client";+import Link from "next/link";+import { useEffect, useState } from "react";++interface User extends U {+ subscriptions: {+ active: boolean;+ };+}++export default function Admin() {+ const [users, setUsers] = useState<User[]>();++ useEffect(() => {+ // fetch users+ fetch("/api/v1/users")+ .then((res) => res.json())+ .then((data) => setUsers(data.response));+ }, []);++ return (+ <div className="max-w-5xl mx-auto mt-5 px-5">+ <div className="gap-2 inline-flex items-center">+ <Link+ href="/dashboard"+ className="text-neutral btn btn-square btn-sm btn-ghost"+ >+ <i className="bi-chevron-left text-xl"></i>+ </Link>+ <p className="capitalize text-3xl font-thin inline">+ User Administration+ </p>+ </div>++ <div className="divider my-3"></div>++ {users && users.length > 0 ? (+ <div className="overflow-x-auto whitespace-nowrap w-full">+ <table className="table table-zebra w-full">+ <thead>+ <tr>+ <th></th>+ <th>Username</th>+ {process.env.NEXT_PUBLIC_EMAIL_PROVIDER === "true" && (+ <th>Email</th>+ )}+ {process.env.NEXT_PUBLIC_STRIPE === "true" && (+ <th>Subscribed</th>+ )}+ <th>Created At</th>+ <th>Updated At</th>+ </tr>+ </thead>+ <tbody>+ {users.map((user, index) => (+ <tr key={user.id}>+ <td>{index + 1}</td>+ <td>{user.username}</td>+ {process.env.NEXT_PUBLIC_EMAIL_PROVIDER === "true" && (+ <td>{user.email}</td>+ )}+ {process.env.NEXT_PUBLIC_STRIPE === "true" && (+ <td>{user.subscriptions.active ? "Yes" : "No"}</td>+ )}+ <td>{new Date(user.createdAt).toLocaleString()}</td>+ <td>{new Date(user.updatedAt).toLocaleString()}</td>+ </tr>+ ))}+ </tbody>+ </table>+ </div>+ ) : (+ <p>No users found.</p>+ )}+ </div>+ );+}diff --git a/pages/api/v1/users/index.ts b/pages/api/v1/users/index.tsindex 3af7bf8e..cce6b4ea 100644--- a/pages/api/v1/users/index.ts+++ b/pages/api/v1/users/index.ts@@ -1,9 +1,17 @@import type { NextApiRequest, NextApiResponse } from "next";import postUser from "@/lib/api/controllers/users/postUser";+import getUsers from "@/lib/api/controllers/users/getUsers";+import verifyUser from "@/lib/api/verifyUser";export default async function users(req: NextApiRequest, res: NextApiResponse) {if (req.method === "POST") {const response = await postUser(req, res);return response;+ } else if (req.method === "GET") {+ const user = await verifyUser({ req, res });+ if (!user || process.env.ADMINISTRATOR !== user.username) return;++ const response = await getUsers();+ return res.status(response.status).json({ response: response.response });}}diff --git a/types/enviornment.d.ts b/types/enviornment.d.tsindex e8c5a7c6..5a94472f 100644--- a/types/enviornment.d.ts+++ b/types/enviornment.d.ts@@ -14,6 +14,7 @@ declare global {ARCHIVE_TAKE_COUNT?: string;IGNORE_UNAUTHORIZED_CA?: string;IGNORE_URL_SIZE_LIMIT?: string;+ ADMINISTRATOR?: string;SPACES_KEY?: string;SPACES_SECRET?: string;@@ -418,4 +419,4 @@ declare global {}}-export { };+export {};