mirror of
https://github.com/Sea-Haven-Industries/open-swe.git
synced 2026-09-30 19:43:15 +00:00
36 lines
996 B
TypeScript
36 lines
996 B
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import {
|
|
GITHUB_TOKEN_COOKIE,
|
|
GITHUB_INSTALLATION_ID_COOKIE,
|
|
} from "@open-swe/shared/constants";
|
|
import { verifyGithubUser } from "@open-swe/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*"],
|
|
};
|