Skip to content

Commit

Permalink
Create user ns on register #2
Browse files Browse the repository at this point in the history
  • Loading branch information
adb-sh committed Sep 26, 2024
1 parent f4070d2 commit 1f0cf68
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 1f0cf68

Please sign in to comment.