This repository has been archived on 2026-08-10. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
onlypets/frontend/middleware.ts
T

43 lines
1.2 KiB
TypeScript

import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
// 🔐 Simulierter Login-Check
// später: Cookie / Session / JWT
const isLoggedIn = request.cookies.get('onlypets-auth')?.value === 'true';
// 🚪 Gast auf Startseite lassen
if (!isLoggedIn && pathname === '/') {
return NextResponse.next();
}
// 🏠 Eingeloggt? Startseite überspringen → Feed
if (isLoggedIn && pathname === '/') {
return NextResponse.redirect(new URL('/feed', request.url));
}
// 🔒 Geschützte Seiten
const protectedRoutes = [
'/feed',
'/post',
'/pets',
'/explore', // 🔍 Entdecken / Neue Beiträge
'/profile', // 👤 Eigenes Profil
'/user', // 👥 fremde Profile (z. B. /user/123)
];
if (!isLoggedIn && protectedRoutes.some((route) => pathname.startsWith(route))) {
return NextResponse.redirect(new URL('/login', request.url));
}
// 🔄 Login-Seite für eingeloggte User blocken
if (isLoggedIn && pathname === '/login') {
return NextResponse.redirect(new URL('/feed', request.url));
}
return NextResponse.next();
}