Initial working commit
Some checks failed
Automated Container Build / build-and-push (push) Failing after 7s
Some checks failed
Automated Container Build / build-and-push (push) Failing after 7s
This commit is contained in:
parent
666ceb7325
commit
b7ce314f01
105 changed files with 35510 additions and 11 deletions
49
src/middleware.ts
Normal file
49
src/middleware.ts
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { getIronSession } from "iron-session";
|
||||
import type { SessionData } from "@/lib/auth";
|
||||
|
||||
const sessionOptions = {
|
||||
password:
|
||||
process.env.SESSION_SECRET ||
|
||||
"dev-session-secret-change-in-production-must-be-32-chars",
|
||||
cookieName: "study-app-session",
|
||||
};
|
||||
|
||||
// Routes that don't require authentication
|
||||
const publicPaths = ["/login", "/api/auth", "/shared"];
|
||||
|
||||
export async function middleware(request: NextRequest) {
|
||||
const { pathname } = request.nextUrl;
|
||||
|
||||
// Allow public routes
|
||||
if (publicPaths.some((path) => pathname.startsWith(path))) {
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
// Allow static assets and Next.js internals
|
||||
if (
|
||||
pathname.startsWith("/_next") ||
|
||||
pathname.startsWith("/favicon") ||
|
||||
pathname.includes(".")
|
||||
) {
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
// Check session
|
||||
const response = NextResponse.next();
|
||||
const session = await getIronSession<SessionData>(
|
||||
request,
|
||||
response,
|
||||
sessionOptions
|
||||
);
|
||||
|
||||
if (!session.isAuthenticated) {
|
||||
return NextResponse.redirect(new URL("/login", request.url));
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
export const config = {
|
||||
matcher: ["/((?!_next/static|_next/image|favicon.ico).*)"],
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue