48 lines
1.3 KiB
TypeScript
48 lines
1.3 KiB
TypeScript
// proxy.ts
|
|
import { NextResponse, type NextRequest } from 'next/server';
|
|
import { getToken } from 'next-auth/jwt';
|
|
|
|
export default async function proxy(request: NextRequest) {
|
|
const { pathname, search } = request.nextUrl;
|
|
|
|
// Sachen, die NIE geschützt werden sollen:
|
|
if (
|
|
pathname.startsWith('/api/auth') ||
|
|
pathname.startsWith('/login') ||
|
|
pathname.startsWith('/_next') ||
|
|
pathname === '/favicon.ico'
|
|
) {
|
|
return NextResponse.next();
|
|
}
|
|
|
|
// Token aus NextAuth lesen
|
|
const token = await getToken({
|
|
req: request,
|
|
secret: process.env.NEXTAUTH_SECRET,
|
|
});
|
|
|
|
// nicht eingeloggt -> auf /login umleiten
|
|
if (!token) {
|
|
const loginUrl = new URL('/login', request.url);
|
|
// nach Login wieder zurück zur ursprünglichen Seite
|
|
loginUrl.searchParams.set('callbackUrl', pathname + search);
|
|
return NextResponse.redirect(loginUrl);
|
|
}
|
|
|
|
// eingeloggt und auf Root "/" -> nach /dashboard umleiten
|
|
if (pathname === '/') {
|
|
const dashboardUrl = new URL('/dashboard', request.url);
|
|
return NextResponse.redirect(dashboardUrl);
|
|
}
|
|
|
|
// alles andere normal durchlassen
|
|
return NextResponse.next();
|
|
}
|
|
|
|
export const config = {
|
|
matcher: [
|
|
// alles abfangen, außer reinen Static-Kram
|
|
'/((?!_next/static|_next/image|favicon.ico).*)',
|
|
],
|
|
};
|