open-swe/apps/web/src/middleware.ts
Brace Sproul 504be7c563
fix: Rename from open-swe to openswe (#809)
* fix: Rename from open-swe to openswe

* del tmp file, format and lint
2025-08-27 19:33:26 +00:00

36 lines
994 B
TypeScript

import { NextRequest, NextResponse } from "next/server";
import {
GITHUB_TOKEN_COOKIE,
GITHUB_INSTALLATION_ID_COOKIE,
} from "@openswe/shared/constants";
import { verifyGithubUser } from "@openswe/shared/github/verify-user";
export async function middleware(request: NextRequest) {
const token = request.cookies.get(GITHUB_TOKEN_COOKIE)?.value;
const installationId = request.cookies.get(
GITHUB_INSTALLATION_ID_COOKIE,
)?.value;
const user = token && installationId ? await verifyGithubUser(token) : null;
if (request.nextUrl.pathname === "/") {
if (user) {
const url = request.nextUrl.clone();
url.pathname = "/chat";
return NextResponse.redirect(url);
}
}
if (request.nextUrl.pathname.startsWith("/chat")) {
if (!user) {
const url = request.nextUrl.clone();
url.pathname = "/";
return NextResponse.redirect(url);
}
}
return NextResponse.next();
}
export const config = {
matcher: ["/", "/chat/:path*"],
};