Proper auth
This commit is contained in:
@@ -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);
|
||||
}, []);
|
||||
|
||||
|
||||
@@ -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<string, { roles: string[] }>;
|
||||
|
||||
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<Users>(() => ({}), "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<boolean> {
|
||||
return !(await getSession())?.accessToken;
|
||||
}
|
||||
|
||||
+27
-18
@@ -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<Node[] | undefined> {
|
||||
if (await verifySession()) return;
|
||||
|
||||
export async function getNodes(): Promise<Node[]> {
|
||||
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<Node | undefined> {
|
||||
const nodes = await read<Node[]>(() => [], "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<NodeLiveData & Node> {
|
||||
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<Docker | undefined> {
|
||||
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<Docker> {
|
||||
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<Docker> {
|
||||
}
|
||||
|
||||
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}` },
|
||||
|
||||
Reference in New Issue
Block a user