Files
DeepTutor/web/proxy.ts
Bingxi Zhao (Frank) 452de66729 release: v1.4.3 — TutorBot→Partners, single-loop Chat, real multi-user isolation
The biggest cut since v1.4.0:

- Partners: TutorBot renamed and rebuilt on the product chat agent loop —
  production IM pipeline (retry/dedup/coalescing, per-channel delivery),
  live streaming on Telegram/Discord/Feishu, new WeCom AI Bot channel
  (15 connectors), web-chat attachments, soul template library + editor.
  Automatic non-destructive migration from data/tutorbot/; HTTP API moved
  to /api/v1/partners.
- Chat: single exploring agent loop with native tool calling; Activity
  header with collapsible thinking/tool trace.
- Multi-user: per-user grants v2 (tools/MCP/exec), data/users + data/system
  layout with automatic migration, runner mounts only the user workspace.
- Visualize: inline themed SVG, structured prompts, local validate+repair
  replacing the LLM review round.
- Co-writer Mermaid diagrams + selection RAG/web; file viewer docx/xlsx
  previews; MinerU cloud parsing backend; office skills on by default;
  CLI chat renderer rewrite.
- Docs rebuilt at deeptutor.info: per-channel setup guides (en+zh),
  refreshed screenshots.
- Pre-commit clean sweep: defusedxml for SVG validation, usedforsecurity
  flags on content hashes, mypy fixes, types-croniter stub.

Release notes: assets/releases/ver1-4-3.md

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 17:17:43 +08:00

74 lines
2.2 KiB
TypeScript

import { NextRequest, NextResponse } from "next/server";
const AUTH_ENABLED = process.env.NEXT_PUBLIC_AUTH_ENABLED === "true";
const LOGIN_PATH = "/login";
const COOKIE_NAME = "dt_token";
export function proxy(req: NextRequest) {
// Auth is disabled (default) — let everything through
if (!AUTH_ENABLED) return NextResponse.next();
const { pathname } = req.nextUrl;
// Always allow auth pages and Next.js internals
if (
pathname.startsWith(LOGIN_PATH) ||
pathname.startsWith("/register") ||
pathname.startsWith("/_next") ||
pathname.startsWith("/favicon")
) {
return NextResponse.next();
}
const token = req.cookies.get(COOKIE_NAME)?.value;
// No token — redirect to login, preserving the intended destination
if (!token) {
const loginUrl = req.nextUrl.clone();
loginUrl.pathname = LOGIN_PATH;
loginUrl.searchParams.set("next", pathname);
return NextResponse.redirect(loginUrl);
}
// Verify token structure (JWT: header.payload.signature)
const parts = token.split(".");
if (parts.length !== 3) {
const loginUrl = req.nextUrl.clone();
loginUrl.pathname = LOGIN_PATH;
loginUrl.searchParams.set("next", pathname);
const response = NextResponse.redirect(loginUrl);
response.cookies.delete(COOKIE_NAME);
return response;
}
// Check expiry from payload (base64url-decoded JSON)
try {
const payload = JSON.parse(
Buffer.from(parts[1], "base64url").toString("utf-8"),
);
if (payload.exp && Date.now() >= payload.exp * 1000) {
const loginUrl = req.nextUrl.clone();
loginUrl.pathname = LOGIN_PATH;
loginUrl.searchParams.set("next", pathname);
const response = NextResponse.redirect(loginUrl);
response.cookies.delete(COOKIE_NAME);
return response;
}
} catch {
// Malformed payload — treat as invalid
const loginUrl = req.nextUrl.clone();
loginUrl.pathname = LOGIN_PATH;
loginUrl.searchParams.set("next", pathname);
const response = NextResponse.redirect(loginUrl);
response.cookies.delete(COOKIE_NAME);
return response;
}
return NextResponse.next();
}
export const config = {
// Run on all page routes, skip API and static assets
matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
};