Added Backend
This commit is contained in:
41
backend/src/plugins/tenant.ts
Normal file
41
backend/src/plugins/tenant.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { FastifyInstance, FastifyRequest } from "fastify";
|
||||
import fp from "fastify-plugin";
|
||||
|
||||
export default fp(async (server: FastifyInstance) => {
|
||||
server.addHook("preHandler", async (req, reply) => {
|
||||
const host = req.headers.host?.split(":")[0]; // Domain ohne Port
|
||||
if (!host) {
|
||||
reply.code(400).send({ error: "Missing host header" });
|
||||
return;
|
||||
}
|
||||
// Tenant aus DB laden
|
||||
const { data: tenant } = await server.supabase
|
||||
.from("tenants")
|
||||
.select("*")
|
||||
.eq("portalDomain", host)
|
||||
.single();
|
||||
|
||||
|
||||
if(!tenant) {
|
||||
// Multi Tenant Mode
|
||||
(req as any).tenant = null;
|
||||
}else {
|
||||
// Tenant ins Request-Objekt hängen
|
||||
(req as any).tenant = tenant;
|
||||
}
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
// Typ-Erweiterung
|
||||
declare module "fastify" {
|
||||
interface FastifyRequest {
|
||||
tenant?: {
|
||||
id: string;
|
||||
name: string;
|
||||
domain?: string;
|
||||
subdomain?: string;
|
||||
settings?: Record<string, any>;
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user