feat: verify-user middleware improvement (#307)

* improve middleware

* rm comments
This commit is contained in:
Dylan Boudro 2025-06-23 17:46:19 -07:00 • committed by GitHub
parent d778021da3
commit 52fbc6aba5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 27 additions and 14 deletions

View file

@ -1,18 +1,30 @@
import { NextRequest, NextResponse } from "next/server";
import { GITHUB_TOKEN_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 user = token ? await verifyGithubUser(token) : null;
export function middleware(request: NextRequest) {
if (request.nextUrl.pathname === "/") {
const token = request.cookies.get(GITHUB_TOKEN_COOKIE)?.value;
if (token && token.length > 0) {
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: ["/"],
matcher: ["/", "/chat/:path*"],
};

View file

@ -37,17 +37,18 @@ const StreamSession = ({ children }: { children: ReactNode }) => {
const [customEvents, setCustomEvents] = useState<CustomNodeEvent[]>([]);
const { refreshThreads } = useThreadsContext();
if (
!process.env.NEXT_PUBLIC_API_URL ||
!process.env.NEXT_PUBLIC_MANAGER_ASSISTANT_ID
) {
throw new Error(
"Both NEXT_PUBLIC_API_URL and NEXT_PUBLIC_MANAGER_ASSISTANT_ID environment variables must be defined.",
);
}
const streamValue = useTypedStream({
apiUrl:
process.env.NEXT_PUBLIC_API_URL ??
(() => {
throw new Error("NEXT_PUBLIC_API_URL is required");
})(),
assistantId:
process.env.NEXT_PUBLIC_MANAGER_ASSISTANT_ID ??
(() => {
throw new Error("NEXT_PUBLIC_MANAGER_ASSISTANT_ID is required");
})(),
apiUrl: process.env.NEXT_PUBLIC_API_URL,
assistantId: process.env.NEXT_PUBLIC_MANAGER_ASSISTANT_ID,
reconnectOnMount: true,
threadId: threadId ?? null,
onCustomEvent: (event, options) => {