From 1f0cf68c52f4e80dae525d2b1bdb1f7f18c0e272 Mon Sep 17 00:00:00 2001 From: adb-sh Date: Thu, 26 Sep 2024 19:30:50 +0200 Subject: [PATCH] Create user ns on register #2 --- src/lib/auth.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/lib/auth.ts b/src/lib/auth.ts index 3d17b43..b848e33 100644 --- a/src/lib/auth.ts +++ b/src/lib/auth.ts @@ -9,6 +9,7 @@ import { redirect } from "@solidjs/router"; import { z } from "zod"; import { db } from "./db"; import { PrismaAdapter } from "@auth/prisma-adapter"; +import { k8sCore } from "./k8s"; declare module "@auth/core/types" { export interface Session { @@ -42,6 +43,35 @@ const authOptions: SolidAuthConfig = { debug: false, trustHost: true, basePath: "/api/auth", + callbacks: { + signIn: async (user) => { + if (!user.user.name) return false; + let existingNamespace; + try { + existingNamespace = await k8sCore.readNamespace(user.user.name); + } catch (e) {} + // disallow register when namespace exists + if ( + !(await db.user.findUnique({ where: { id: user.user.id } })) && + existingNamespace + ) + return false; + + if (existingNamespace) return true; + + await k8sCore.createNamespace({ + apiVersion: "v1", + kind: "Namespace", + metadata: { + name: user.user.name, + labels: { + "app.kubernetes.io/managed-by": "deploycat", + }, + }, + }); + return true; + }, + }, }; export const getAuthOptions = () => authOptions;