diff --git a/src/app/overview/page.tsx b/src/app/overview/page.tsx index 02a2b45..0bfa0d2 100644 --- a/src/app/overview/page.tsx +++ b/src/app/overview/page.tsx @@ -103,28 +103,6 @@ export default function OverviewPage() { fetchNodes(); const interval = setInterval(fetchNodes, 3000); - const test = async () => { - const sessionToken = await getSession(); - console.log(sessionToken); - // console.log( - // ( - // await axios.get("https://api.github.com/user/repos", { - // headers: { Authorization: `Bearer ${sessionToken}` }, - // }) - // ).data - // ); - - // console.log( - // ( - // await axios.get("https://api.github.com/orgs/orus-dev/repos", { - // headers: { Authorization: `Bearer ${sessionToken}` }, - // }) - // ).data - // ); - }; - - test(); - return () => clearInterval(interval); }, []); diff --git a/src/core/auth/session.ts b/src/core/auth/session.ts index f6aaaca..eb0eda5 100644 --- a/src/core/auth/session.ts +++ b/src/core/auth/session.ts @@ -1,8 +1,14 @@ "use server"; +import { read, write } from "@/app/api/file"; +import axios from "axios"; import { cookies } from "next/headers"; import { redirect } from "next/navigation"; -export async function getSession() { +type Users = Record; + +export async function getSession(): Promise< + undefined | { user: { id: number }; accessToken: string } +> { const c = await cookies(); const accessToken = c.get("github_access")?.value; @@ -10,5 +16,36 @@ export async function getSession() { redirect("/"); } - return accessToken; + // Test the token + try { + const user = ( + await axios.get("https://api.github.com/user", { + headers: { Authorization: `Bearer ${accessToken}` }, + }) + ).data; + + const users = Object.keys( + await read(() => ({}), "data", "users.json") + ); + + if (!users.includes(String(user.id))) { + if (users.length === 0) { + await write( + { [String(user.id)]: { roles: ["admin"] } } as Users, // First user is always an admin + "data", + "users.json" + ); + } else { + throw new Error("Invalid user"); + } + } + + return { user, accessToken }; + } catch { + redirect("/"); + } +} + +export async function verifySession(): Promise { + return !(await getSession())?.accessToken; } diff --git a/src/core/node.ts b/src/core/node.ts index 842fa79..a9cda4b 100644 --- a/src/core/node.ts +++ b/src/core/node.ts @@ -3,35 +3,42 @@ import { read, write } from "@/app/api/file"; import { Docker, Node, NodeLiveData } from "@/lib/types"; import axios from "axios"; +import { verifySession } from "./auth/session"; + +export async function getNodes(): Promise { + if (await verifySession()) return; -export async function getNodes(): Promise { return await read(() => [], "data", "nodes.json"); } export async function setNodes(nodes: Node[]) { + if (await verifySession()) return; + await write(nodes, "data", "nodes.json"); } -export async function getNode(node_id: string): Promise { - const nodes = await read(() => [], "data", "nodes.json"); - return nodes.find((v) => v.node_id === node_id); +export async function getLiveNodes(): Promise< + (NodeLiveData & Node)[] | undefined +> { + if (await verifySession()) return; + + const nodes: Node[] = await read(() => [], "data", "nodes.json"); + + return await Promise.all( + nodes.map(async (node) => ({ + ...node, + liveData: await ( + await axios.get(`http://${node.ip}:3000/api/status`, { + headers: { Authorization: `ApiKey ${node.key}` }, + }) + ).data.liveData, + })) + ); } -export async function getLiveNode(node: Node): Promise { - const data = await ( - await axios.get(`http://${node.ip}:3000/api/status`, { - headers: { Authorization: `ApiKey ${node.key}` }, - }) - ).data; - return { ...node, liveData: data.liveData }; -} +export async function getDockerNode(node: Node): Promise { + if (await verifySession()) return; -export async function getLiveNodes(): Promise<(NodeLiveData & Node)[]> { - const nodes = await read(() => [], "data", "nodes.json"); - return await Promise.all(nodes.map(getLiveNode)); -} - -export async function getDockerNode(node: Node): Promise { const data = await ( await axios.get(`http://${node.ip}:3000/api/get-docker`, { headers: { Authorization: `ApiKey ${node.key}` }, @@ -41,6 +48,8 @@ export async function getDockerNode(node: Node): Promise { } export async function authNode(ip: string, key: string) { + if (await verifySession()) return; + const data = await ( await axios.get(`http://${ip}:3000/api/auth`, { headers: { Authorization: `ApiKey ${key}` },