subman-nextjs/auth.config.ts

24 lines
709 B
TypeScript
Raw Permalink Normal View History

import type { NextAuthConfig } from 'next-auth';
2024-09-10 10:29:26 +00:00
import Credentials from 'next-auth/providers/credentials';
export const authConfig = {
pages: {
signIn: '/login',
},
callbacks: {
authorized({ auth, request: { nextUrl } }) {
const isLoggedIn = !!auth?.user;
2024-09-10 10:29:26 +00:00
console.log(`isLoggedIn: ${isLoggedIn}`)
const isOnDashboard = nextUrl.pathname.startsWith('/');
if (isOnDashboard) {
if (isLoggedIn) return true;
return false; // Redirect unauthenticated users to login page
} else if (isLoggedIn) {
2024-09-10 10:29:26 +00:00
return Response.redirect(new URL('/', nextUrl));
}
return true;
},
},
2024-09-10 10:29:26 +00:00
providers: [Credentials({})], // Add providers with an empty array for now
} satisfies NextAuthConfig;