import { createMiddleware } from "@tanstack/react-start";
import { getRequestHeaders } from "@tanstack/react-start/server";
import { authClient } from "../lib/auth-client";

export const authMiddleware = createMiddleware().server(async ({ next }) => {
	const auth = await authClient.getSession({
		fetchOptions: {
			headers: getRequestHeaders(),
		},
	});
	const isAuthenticated = typeof auth.data?.user.id === "string";

	return await next({
		context: {
			user: auth.data?.user,
			isAuthenticated,
			isUserBanned: auth.data?.user.banned ?? false,
			banReason: auth.data?.user.banReason,
		},
	});
});
