2025-06-23 17:18:28 -07:00
|
|
|
import { NextRequest, NextResponse } from "next/server";
|
2025-07-29 14:24:22 -07:00
|
|
|
import {
|
|
|
|
|
GITHUB_TOKEN_COOKIE,
|
|
|
|
|
GITHUB_INSTALLATION_ID_COOKIE,
|
2025-08-27 12:33:26 -07:00
|
|
|
} from "@openswe/shared/constants";
|
|
|
|
|
import { verifyGithubUser } from "@openswe/shared/github/verify-user";
|
2025-06-23 17:46:19 -07:00
|
|
|
|
|
|
|
|
export async function middleware(request: NextRequest) {
|
|
|
|
|
const token = request.cookies.get(GITHUB_TOKEN_COOKIE)?.value;
|
2025-07-29 14:24:22 -07:00
|
|
|
const installationId = request.cookies.get(
|
|
|
|
|
GITHUB_INSTALLATION_ID_COOKIE,
|
|
|
|
|
)?.value;
|
|
|
|
|
const user = token && installationId ? await verifyGithubUser(token) : null;
|
2025-06-23 17:18:28 -07:00
|
|
|
|
|
|
|
|
if (request.nextUrl.pathname === "/") {
|
2025-06-23 17:46:19 -07:00
|
|
|
if (user) {
|
2025-06-23 17:18:28 -07:00
|
|
|
const url = request.nextUrl.clone();
|
|
|
|
|
url.pathname = "/chat";
|
|
|
|
|
return NextResponse.redirect(url);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-06-23 17:46:19 -07:00
|
|
|
|
|
|
|
|
if (request.nextUrl.pathname.startsWith("/chat")) {
|
|
|
|
|
if (!user) {
|
|
|
|
|
const url = request.nextUrl.clone();
|
|
|
|
|
url.pathname = "/";
|
|
|
|
|
return NextResponse.redirect(url);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-23 17:18:28 -07:00
|
|
|
return NextResponse.next();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const config = {
|
2025-06-23 17:46:19 -07:00
|
|
|
matcher: ["/", "/chat/:path*"],
|
2025-06-23 17:18:28 -07:00
|
|
|
};
|