Skip to content

Commit 1f0cf68

Browse files
committed
Create user ns on register #2
1 parent f4070d2 commit 1f0cf68

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/lib/auth.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { redirect } from "@solidjs/router";
99
import { z } from "zod";
1010
import { db } from "./db";
1111
import { PrismaAdapter } from "@auth/prisma-adapter";
12+
import { k8sCore } from "./k8s";
1213

1314
declare module "@auth/core/types" {
1415
export interface Session {
@@ -42,6 +43,35 @@ const authOptions: SolidAuthConfig = {
4243
debug: false,
4344
trustHost: true,
4445
basePath: "/api/auth",
46+
callbacks: {
47+
signIn: async (user) => {
48+
if (!user.user.name) return false;
49+
let existingNamespace;
50+
try {
51+
existingNamespace = await k8sCore.readNamespace(user.user.name);
52+
} catch (e) {}
53+
// disallow register when namespace exists
54+
if (
55+
!(await db.user.findUnique({ where: { id: user.user.id } })) &&
56+
existingNamespace
57+
)
58+
return false;
59+
60+
if (existingNamespace) return true;
61+
62+
await k8sCore.createNamespace({
63+
apiVersion: "v1",
64+
kind: "Namespace",
65+
metadata: {
66+
name: user.user.name,
67+
labels: {
68+
"app.kubernetes.io/managed-by": "deploycat",
69+
},
70+
},
71+
});
72+
return true;
73+
},
74+
},
4575
};
4676

4777
export const getAuthOptions = () => authOptions;

0 commit comments

Comments
 (0)