mirror of
https://github.com/Sea-Haven-Industries/open-swe.git
synced 2026-10-01 02:43:13 +00:00
36 lines
994 B
TypeScript
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*"],
|
|
};
|