KI-AGENT: Persistente App-Sessions mit Refresh-Tokens ergänzen

This commit is contained in:
2026-07-20 22:01:08 +02:00
parent d8273e794d
commit 657a4f9e85
11 changed files with 405 additions and 60 deletions

View File

@@ -14,6 +14,7 @@ import {
import {and, desc, eq, inArray} from "drizzle-orm"
import { enrichProfilesWithBranches } from "../utils/profileBranches"
import { enrichProfilesWithTeams } from "../utils/profileTeams"
import { rotateRefreshToken } from "../utils/authTokens"
export default async function tenantRoutes(server: FastifyInstance) {
@@ -65,7 +66,7 @@ export default async function tenantRoutes(server: FastifyInstance) {
return reply.code(401).send({ error: "Unauthorized" })
}
const { tenant_id } = req.body as { tenant_id: string }
const { tenant_id, refreshToken } = req.body as { tenant_id: string; refreshToken?: string }
if (!tenant_id) return reply.code(400).send({ error: "tenant_id required" })
// prüfen ob der User zu diesem Tenant gehört
@@ -81,7 +82,22 @@ export default async function tenantRoutes(server: FastifyInstance) {
return reply.code(403).send({ error: "Not a member of this tenant" })
}
// JWT neu erzeugen
if (refreshToken) {
const session = await rotateRefreshToken(
server,
refreshToken,
Number(tenant_id),
req.user.user_id
)
if (!session) {
return reply.code(401).send({ error: "Invalid or expired refresh token" })
}
return session
}
// Abwärtskompatibilität für Clients ohne persistente Session
const token = jwt.sign(
{
user_id: req.user.user_id,