From d167cb7265fe499d3fa639a5cdf303fe351732ae Mon Sep 17 00:00:00 2001 From: brianling Date: Sat, 6 Jul 2024 18:31:26 +0800 Subject: [PATCH] feat(Auth): Compete auth-middleware --- composables/user.ts | 11 +++ middleware/admin.ts | 6 ++ middleware/auth.ts | 12 +-- pages/admin/index.vue | 47 +++-------- pages/apply/index.vue | 77 +++++++++++++++--- pages/index.vue | 6 +- pages/user/index.vue | 56 ++++++++++++- prisma/test.prisma | 113 ++++++++++++++++++++++++++ server/api/admin/applications.ts | 51 ++++++++++++ server/api/application/create.post.ts | 6 ++ server/api/application/index.ts | 15 ++++ server/api/test/index.ts | 65 ++++++++++++++- server/api/user/application.post.ts | 55 +++++++++++++ server/api/user/auth.get.ts | 23 ++++++ server/api/user/auth.ts | 2 +- types/Application/Application.ts | 18 ++-- types/Application/idnex.ts | 0 types/Application/index.d.ts | 1 + 18 files changed, 495 insertions(+), 69 deletions(-) create mode 100644 composables/user.ts create mode 100644 prisma/test.prisma create mode 100644 server/api/admin/applications.ts create mode 100644 server/api/application/create.post.ts create mode 100644 server/api/application/index.ts create mode 100644 server/api/user/application.post.ts create mode 100644 server/api/user/auth.get.ts delete mode 100644 types/Application/idnex.ts create mode 100644 types/Application/index.d.ts diff --git a/composables/user.ts b/composables/user.ts new file mode 100644 index 0000000..6b6c44d --- /dev/null +++ b/composables/user.ts @@ -0,0 +1,11 @@ +export async function getUserId() { + const auth = useCookie("auth"); + return await $fetch("/api/user/auth", { + method: "GET", + query: { + auth: auth.value, + }, + }).then((res: number) => { + return res; + }); +} diff --git a/middleware/admin.ts b/middleware/admin.ts index e69de29..f27d094 100644 --- a/middleware/admin.ts +++ b/middleware/admin.ts @@ -0,0 +1,6 @@ +export default defineNuxtRouteMiddleware(async (to, from) => { + if ((await getUserId()) != 1) { + ElMessage("禁止访问"); + return navigateTo("/", { replace: true }); + } +}); diff --git a/middleware/auth.ts b/middleware/auth.ts index 1697741..2de3f8b 100644 --- a/middleware/auth.ts +++ b/middleware/auth.ts @@ -4,24 +4,24 @@ export default defineNuxtRouteMiddleware(async (to, from) => { ElMessage("未登录或cookie未开启"); return navigateTo("/user/login", { replace: true }); } else { - const { data: result } = await useFetch("/api/user/auth", { + const result = await $fetch("/api/user/auth", { method: "post", body: { auth: auth.value, }, }); - if (!result.value?.login && to.path !== "/user/test") { - if (result.value?.code == 0) { + if (!result.login && to.path !== "/user/test") { + if (result.code == 0) { ElMessage("未登录"); - } else if (result.value?.code == 2) { + } else if (result.code == 2) { ElMessage("登录超时,请重新登录"); auth.value = undefined; } else { - ElMessage(result.value?.code); + ElMessage("error" + result.code); } return navigateTo("/user/login"); } else { - console.log(auth.value); + //console.log(auth.value); } } }); diff --git a/pages/admin/index.vue b/pages/admin/index.vue index 9ad99b2..589f089 100644 --- a/pages/admin/index.vue +++ b/pages/admin/index.vue @@ -5,26 +5,26 @@ - - - + + + - + @@ -49,30 +49,7 @@ diff --git a/pages/apply/index.vue b/pages/apply/index.vue index 20c4f2b..a28f4af 100644 --- a/pages/apply/index.vue +++ b/pages/apply/index.vue @@ -12,6 +12,7 @@ > - - - + + + + + + diff --git a/pages/index.vue b/pages/index.vue index e381d48..17351a1 100644 --- a/pages/index.vue +++ b/pages/index.vue @@ -4,10 +4,12 @@ FreePotato Server - - + diff --git a/pages/user/index.vue b/pages/user/index.vue index b449f35..25372c6 100644 --- a/pages/user/index.vue +++ b/pages/user/index.vue @@ -1,11 +1,63 @@ - diff --git a/prisma/test.prisma b/prisma/test.prisma new file mode 100644 index 0000000..0994f3a --- /dev/null +++ b/prisma/test.prisma @@ -0,0 +1,113 @@ +// This is your Prisma schema file, +// learn more about it in the docs: https://pris.ly/d/prisma-schema + +generator client { + provider = "prisma-client-js" + binaryTargets = ["native", "debian-openssl-3.0.x"] +} + +datasource db { + provider = "sqlite" + url = env("DATABASE_URL") +} + +model User { + id Int @id @default(autoincrement()) + phone String @unique + username String? + password String + applications Application[] + loginlogs Loginlogs[] + Vm Vm[] +} + +model Adminer { + id Int @id @default(autoincrement()) + adminId Int +} + +model Application { + id Int @id @default(autoincrement()) + name String + area String + cpu Int + ram Int + disk Int + desc String + deploy Boolean @default(false) + applicant User @relation(fields: [applicantId], references: [id]) + applicantId Int +} +model Register { + id Int @id @default(autoincrement()) + phone String + deadline DateTime + code String +} +model Loginlogs { + id Int @id @default(autoincrement()) + outtime DateTime + ip String + loginer User @relation(fields: [userid], references: [id]) + userid Int + token String @unique +} +//Config +model Web { + ConfigId Int @id + ConfigName String + ConfigValue String +} +model Cluster { + ClusterId Int @id + Name String + Ip String + Username String + Password String + Gateway String + Resource String + Status String + Nodes Node[] +} +model Node { + NodeId Int @id + Cluster Cluster @relation(fields: [ClusterId], references: [ClusterId]) + ClusterId Int + Resource String + Status String + Vms Vm[] +} +model Template { + TemplateId Int @id @default(autoincrement()) + OS String + Type String + Path String + Cpu String + Ram String + Disk String + Ports String + Vm Vm[] +} +//datasource +model Ip { + Adress String @id + Vmid Int + Vm Vm @relation(fields: [Vmid], references: [Vmid]) +} +model Port { + Port Int @id + Vmid Int + Vm Vm @relation(fields: [Vmid], references: [Vmid]) +} +model Vm { + Vmid Int @id @default(autoincrement()) + NodeId Int + Node Node @relation(fields: [NodeId], references: [NodeId]) + TemplateId Int + Template Template @relation(fields: [TemplateId], references: [TemplateId]) + UserId Int + User User @relation(fields: [UserId], references: [id]) + Ip Ip[] + SshPort Int + Ports Port[] +} \ No newline at end of file diff --git a/server/api/admin/applications.ts b/server/api/admin/applications.ts new file mode 100644 index 0000000..7cb0f4d --- /dev/null +++ b/server/api/admin/applications.ts @@ -0,0 +1,51 @@ +import { PrismaClient } from "@prisma/client"; +import type { Application } from "~/types/Application"; +const db = new PrismaClient(); +type app = { + applicant: { + username: string | null; + }; +} & { + id: number; + name: string; + area: string; + cpu: number; + ram: number; + disk: number; + desc: string; + deploy: boolean; + applicantId: number; +}; +function formatApplication(raw: app[]): Application[] { + var logs: Application[] = []; + raw.forEach((element: app) => { + logs.push({ + id: element.id, + area: element.area, + name: element.name, + applicant: element.applicant.username || "none", + cpu: element.cpu, + ram: element.ram, + disk: element.disk, + desc: element.desc, + deploy: element.deploy, + }); + }); + return logs; +} +export default defineEventHandler(async (event) => { + const applications = await db.application.findMany({ + orderBy: { + id: "desc", // 'asc' 表示升序,'desc' 表示降序 + }, + include: { + applicant: { + select: { + username: true, + }, + }, + }, + }); + await db.$disconnect(); + return formatApplication(applications); +}); diff --git a/server/api/application/create.post.ts b/server/api/application/create.post.ts new file mode 100644 index 0000000..b27a6f2 --- /dev/null +++ b/server/api/application/create.post.ts @@ -0,0 +1,6 @@ +import { PrismaClient } from "@prisma/client"; +const db = new PrismaClient(); +export default defineEventHandler(async (event) => { + const body = await readBody(event); + return body; +}); diff --git a/server/api/application/index.ts b/server/api/application/index.ts new file mode 100644 index 0000000..8c6e573 --- /dev/null +++ b/server/api/application/index.ts @@ -0,0 +1,15 @@ +import { PrismaClient } from "@prisma/client"; +const db = new PrismaClient(); +export default defineEventHandler(async (event) => { + const body = await readBody(event); + const res = { + name: + (await db.application.findFirst({ + where: { + name: body.name, + }, + })) == null, + }; + await db.$disconnect(); + return res; +}); diff --git a/server/api/test/index.ts b/server/api/test/index.ts index 78272f2..6348e21 100644 --- a/server/api/test/index.ts +++ b/server/api/test/index.ts @@ -1,7 +1,66 @@ import { PrismaClient } from "@prisma/client"; - const db = new PrismaClient(); +export default defineEventHandler(async (event) => { + const body: { + name: string; + area: string; + cpu: string; + ram: string; + disk: string; + desc: string; + uid: string; + auth: string; + } = await readBody(event); + const isAuth: { login: boolean; code: number } = await $fetch( + "/api/user/auth", + { + method: "POST", + body: { + auth: body.auth, + }, + } + ); + if (isAuth.login) { + await db.application.create({ + data: { + name: body.name, + area: body.area, + cpu: parseInt(body.cpu), + ram: parseInt(body.ram), + disk: parseInt(body.disk), + desc: body.desc, + applicantId: parseInt(body.uid), + }, + }); -export default defineEventHandler((event) => { - return event; + await db.$disconnect(); + return { + code: 1, + msg: "申请提交成功", + }; + } else { + if (isAuth.code == 0) { + console.error(isAuth); + console.error(JSON.stringify(body)); + return { + code: 0, + msg: "未登录", + }; + } else { + return { + code: 0, + msg: "登陆超时", + }; + } + } + return body; }); + +/* { name: '1231', + area: '1', + cpu: '2', + ram: '2', + disk: '5', + desc: '
12313
', + uid: '1', + auth: 'a19705902b2ba12fbe52930b34802ab1' } */ diff --git a/server/api/user/application.post.ts b/server/api/user/application.post.ts new file mode 100644 index 0000000..43a9119 --- /dev/null +++ b/server/api/user/application.post.ts @@ -0,0 +1,55 @@ +import { PrismaClient } from "@prisma/client"; +import type { Application } from "~/types/Application"; +const db = new PrismaClient(); +type app = { + applicant: { + username: string | null; + }; +} & { + id: number; + name: string; + area: string; + cpu: number; + ram: number; + disk: number; + desc: string; + deploy: boolean; + applicantId: number; +}; +function formatApplication(raw: app[]): Application[] { + var logs: Application[] = []; + raw.forEach((element: app) => { + logs.push({ + id: element.id, + area: element.area, + name: element.name, + applicant: element.applicant.username || "none", + cpu: element.cpu, + ram: element.ram, + disk: element.disk, + desc: element.desc, + deploy: element.deploy, + }); + }); + return logs; +} +export default defineEventHandler(async (event) => { + const body = await readBody(event); + const applications = await db.application.findMany({ + where: { + applicantId: body.uid, + }, + orderBy: { + id: "desc", // 'asc' 表示升序,'desc' 表示降序 + }, + include: { + applicant: { + select: { + username: true, + }, + }, + }, + }); + await db.$disconnect(); + return formatApplication(applications); +}); diff --git a/server/api/user/auth.get.ts b/server/api/user/auth.get.ts new file mode 100644 index 0000000..88f610d --- /dev/null +++ b/server/api/user/auth.get.ts @@ -0,0 +1,23 @@ +import { PrismaClient } from "@prisma/client"; + +const db = new PrismaClient(); +export default defineEventHandler(async (event) => { + const query: { auth: string } = getQuery(event); + let userid: number = 0; + userid = await db.loginlogs + .findFirst({ + where: { token: query.auth }, + select: { + userid: true, + }, + }) + .then((res) => { + if (res != null) { + return res.userid; + } else { + return 0; + } + }); + //console.info(userid); + return userid; +}); diff --git a/server/api/user/auth.ts b/server/api/user/auth.ts index 05a6129..3ac168c 100644 --- a/server/api/user/auth.ts +++ b/server/api/user/auth.ts @@ -4,7 +4,7 @@ const db = new PrismaClient(); async function auth(auth: string) { const res = await db.loginlogs.findFirst({ - where: { token: auth.toString() }, + where: { token: auth }, }); //return JSON.stringify((await res).values) if (res == null) { diff --git a/types/Application/Application.ts b/types/Application/Application.ts index f839334..27d1896 100644 --- a/types/Application/Application.ts +++ b/types/Application/Application.ts @@ -1,9 +1,11 @@ export type Application = { - name: string - area: string - cpu: number - ram: number - disk: number - usage: string - applicantId: number -} \ No newline at end of file + id: number; + name: string; + area: string; + cpu: number; + ram: number; + disk: number; + desc: string; + applicant: string; + deploy: boolean; +}; diff --git a/types/Application/idnex.ts b/types/Application/idnex.ts deleted file mode 100644 index e69de29..0000000 diff --git a/types/Application/index.d.ts b/types/Application/index.d.ts new file mode 100644 index 0000000..79b1a5d --- /dev/null +++ b/types/Application/index.d.ts @@ -0,0 +1 @@ +export { Application } from "./Application.ts";