diff --git a/backend/src/routes/auth/me.ts b/backend/src/routes/auth/me.ts index 24eac9d..3982474 100644 --- a/backend/src/routes/auth/me.ts +++ b/backend/src/routes/auth/me.ts @@ -52,6 +52,7 @@ export default async function meRoutes(server: FastifyInstance) { id: tenants.id, name: tenants.name, short: tenants.short, + calendarConfig: tenants.calendarConfig, hasActiveLicense: tenants.hasActiveLicense, locked: tenants.locked, features: tenants.features, diff --git a/backend/src/routes/tenant.ts b/backend/src/routes/tenant.ts index b65b2cf..0c85424 100644 --- a/backend/src/routes/tenant.ts +++ b/backend/src/routes/tenant.ts @@ -28,7 +28,21 @@ export default async function tenantRoutes(server: FastifyInstance) { // ------------------------------------------------------------- // GET CURRENT TENANT // ------------------------------------------------------------- - server.get("/tenant", async (req) => { + server.get("/tenant", async (req, reply) => { + if (req.user?.tenant_id) { + const tenantRows = await server.db + .select() + .from(tenants) + .where(eq(tenants.id, Number(req.user.tenant_id))) + .limit(1) + + if (!tenantRows.length) { + return reply.code(404).send({ error: "Tenant not found" }) + } + + return tenantRows[0] + } + if (req.tenant) { return { message: `Hallo vom Tenant ${req.tenant?.name}`, diff --git a/frontend/stores/auth.ts b/frontend/stores/auth.ts index 004f89d..d942b7f 100644 --- a/frontend/stores/auth.ts +++ b/frontend/stores/auth.ts @@ -355,8 +355,24 @@ export const useAuthStore = defineStore("auth", { if(this.profile?.temp_config) tempStore.setStoredTempConfig(this.profile.temp_config) if(me.activeTenant > 0) { - this.activeTenant = me.activeTenant - this.activeTenantData = me.tenants.find(i => i.id === me.activeTenant) + const normalizedActiveTenant = String(me.activeTenant) + this.activeTenant = normalizedActiveTenant + this.activeTenantData = me.tenants.find(i => String(i.id) === normalizedActiveTenant) || null + + try { + const tenant = await useNuxtApp().$api("/api/tenant", { + headers: { + Authorization: `Bearer ${tokenToUse}`, + context: { jwt: tokenToUse } + } + }) + + if (tenant?.id) { + this.activeTenantData = tenant + } + } catch (tenantError) { + console.error("fetch active tenant failed", tenantError) + } } this.scheduleSessionTimers(tokenToUse) diff --git a/frontend/stores/profile.js b/frontend/stores/profile.js index 58be12f..1341eff 100644 --- a/frontend/stores/profile.js +++ b/frontend/stores/profile.js @@ -77,6 +77,16 @@ export const useProfileStore = defineStore("profile", () => { async function fetchOwnTenant() { syncFromAuth() + try { + const tenant = await useNuxtApp().$api("/api/tenant") + if (tenant) { + ownTenant.value = tenant + auth.activeTenantData = tenant + return + } + } catch (e) { + } + ownTenant.value = auth.activeTenantData || ownTenant.value }