Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix: enforce HTTPS redirect globally in middleware

Gener962b0c462867
diff --git a/src/web/proxy.ts b/src/web/proxy.ts
deleted file mode 100644
index 8eb7a7afa..000000000
--- a/src/web/proxy.ts
+++ /dev/null
@@ -1,16 +0,0 @@
-import { NextRequest, NextResponse } from "next/server";
-
-export function proxy(req: NextRequest) {
- const session = req.cookies.get("alook_session");
- if (!session) {
- return NextResponse.redirect(new URL("/login", req.url));
- }
- return NextResponse.next();
-}
-
-export const config = {
- matcher: [
- // Protect all (app) group routes but not /login, /api, or static assets
- "/((?!login|auth|health|api|_next|favicon\\.ico|.*\\.).*)",
- ],
-};
diff --git a/src/web/src/middleware.ts b/src/web/src/middleware.ts
index 0c4a19d36..2eece61c9 100644
--- a/src/web/src/middleware.ts
+++ b/src/web/src/middleware.ts
@@ -3,16 +3,29 @@ import { getCloudflareContext } from "@opennextjs/cloudflare"
import { createAuth } from "@/lib/auth"
export async function middleware(request: NextRequest) {
- const { env } = await getCloudflareContext({ async: true })
- const auth = createAuth(env as Env)
- const session = await auth.api.getSession({ headers: request.headers })
+ if (
+ request.headers.get("x-forwarded-proto") === "http" &&
+ !request.nextUrl.hostname.startsWith("localhost") &&
+ !request.nextUrl.hostname.startsWith("127.")
+ ) {
+ const httpsUrl = new URL(request.url)
+ httpsUrl.protocol = "https:"
+ return NextResponse.redirect(httpsUrl, 301)
+ }
+
+ if (request.nextUrl.pathname.startsWith("/dashboard")) {
+ const { env } = await getCloudflareContext({ async: true })
+ const auth = createAuth(env as Env)
+ const session = await auth.api.getSession({ headers: request.headers })
- if (!session) {
- return NextResponse.redirect(new URL("/sign-in", request.url))
+ if (!session) {
+ return NextResponse.redirect(new URL("/sign-in", request.url))
+ }
}
+
return NextResponse.next()
}
export const config = {
- matcher: ["/dashboard/:path*"],
+ matcher: ["/((?!_next|favicon\\.ico|.*\\..*).*)"],
}