From d43c0f78b4a2e1b074c5d838c8e50d5b250a7008 Mon Sep 17 00:00:00 2001 From: Dominik Stahl Date: Sat, 19 Apr 2025 01:25:23 +0200 Subject: [PATCH] feat: setup protected routes --- src/auth.ts | 5 +++++ src/middleware.ts | 13 ++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/auth.ts b/src/auth.ts index e97fa92..1d91260 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -3,4 +3,9 @@ import Authentik from 'next-auth/providers/authentik'; export const { handlers, signIn, signOut, auth } = NextAuth({ providers: [Authentik], + callbacks: { + authorized: async ({ auth }) => { + return !!auth; + }, + }, }); diff --git a/src/middleware.ts b/src/middleware.ts index c979e88..e03128a 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -1 +1,12 @@ -export { auth as middleware } from '@/auth'; +import { auth } from '@/auth'; + +export default auth((req) => { + if (!req.auth && req.nextUrl.pathname !== '/login') { + const newUrl = new URL('/login', req.nextUrl.origin); + return Response.redirect(newUrl); + } +}); + +export const config = { + matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)'], +};