refactor: consolidate protected routes under /intern/*

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
mikl0s 2026-02-22 21:53:20 +00:00
parent 90407c4f8d
commit efe19f0cda
5 changed files with 12 additions and 20 deletions

View file

@ -40,7 +40,7 @@ export default function HistorikPage() {
const res = await fetch("/api/quotes") const res = await fetch("/api/quotes")
if (!res.ok) { if (!res.ok) {
if (res.status === 401) { if (res.status === 401) {
router.push("/login") router.push("/intern/login")
return return
} }
throw new Error("Failed to fetch quotes") throw new Error("Failed to fetch quotes")
@ -57,7 +57,7 @@ export default function HistorikPage() {
async function handleLogout() { async function handleLogout() {
try { try {
await fetch("/api/auth/logout", { method: "POST" }) await fetch("/api/auth/logout", { method: "POST" })
router.push("/login") router.push("/intern/login")
router.refresh() router.refresh()
} catch (error) { } catch (error) {
console.error("Logout failed:", error) console.error("Logout failed:", error)
@ -104,7 +104,7 @@ export default function HistorikPage() {
</div> </div>
<div className="flex items-center gap-2"> <div className="flex items-center gap-2">
<Link href="/dashboard"> <Link href="/intern">
<Button variant="ghost" size="sm"> <Button variant="ghost" size="sm">
<LayoutDashboard className="mr-2 h-4 w-4" /> <LayoutDashboard className="mr-2 h-4 w-4" />
<span className="hidden sm:inline">Dashboard</span> <span className="hidden sm:inline">Dashboard</span>

View file

@ -35,7 +35,7 @@ export default function LoginPage() {
return return
} }
router.push("/dashboard") router.push("/intern")
router.refresh() router.refresh()
} catch { } catch {
setError("Der opstod en fejl. Prøv igen.") setError("Der opstod en fejl. Prøv igen.")

View file

@ -38,7 +38,7 @@ export default function DashboardPage() {
const res = await fetch("/api/quotes") const res = await fetch("/api/quotes")
if (!res.ok) { if (!res.ok) {
if (res.status === 401) { if (res.status === 401) {
router.push("/login") router.push("/intern/login")
return return
} }
throw new Error("Failed to fetch quotes") throw new Error("Failed to fetch quotes")
@ -84,7 +84,7 @@ export default function DashboardPage() {
async function handleLogout() { async function handleLogout() {
try { try {
await fetch("/api/auth/logout", { method: "POST" }) await fetch("/api/auth/logout", { method: "POST" })
router.push("/login") router.push("/intern/login")
router.refresh() router.refresh()
} catch (error) { } catch (error) {
console.error("Logout failed:", error) console.error("Logout failed:", error)
@ -133,7 +133,7 @@ export default function DashboardPage() {
</div> </div>
<div className="flex items-center gap-2"> <div className="flex items-center gap-2">
<Link href="/historik"> <Link href="/intern/historik">
<Button variant="ghost" size="sm"> <Button variant="ghost" size="sm">
<List className="mr-2 h-4 w-4" /> <List className="mr-2 h-4 w-4" />
<span className="hidden sm:inline">Historik</span> <span className="hidden sm:inline">Historik</span>

View file

@ -1,34 +1,26 @@
import { NextResponse } from "next/server" import { NextResponse } from "next/server"
import type { NextRequest } from "next/server" import type { NextRequest } from "next/server"
// Routes that require authentication
const protectedPaths = ["/dashboard", "/historik", "/admin"]
export function middleware(request: NextRequest) { export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl const { pathname } = request.nextUrl
// Check if path requires authentication // Don't protect the login page
const isProtectedPath = protectedPaths.some((path) => pathname.startsWith(path)) if (pathname === "/intern/login") {
if (!isProtectedPath) {
return NextResponse.next() return NextResponse.next()
} }
// Check for session cookie // Check for session cookie on all /intern/* routes
const sessionCookie = request.cookies.get("session") const sessionCookie = request.cookies.get("session")
if (!sessionCookie?.value) { if (!sessionCookie?.value) {
// Redirect to login const loginUrl = new URL("/intern/login", request.url)
const loginUrl = new URL("/login", request.url)
loginUrl.searchParams.set("redirect", pathname) loginUrl.searchParams.set("redirect", pathname)
return NextResponse.redirect(loginUrl) return NextResponse.redirect(loginUrl)
} }
// Cookie exists - let the page validate the session
// (Session validation happens server-side in the page)
return NextResponse.next() return NextResponse.next()
} }
export const config = { export const config = {
matcher: ["/dashboard/:path*", "/historik/:path*", "/admin/:path*"], matcher: ["/intern/:path*"],
} }