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).*)'], +};