2024-11-15 10:37:01 -05:00
|
|
|
import type { NextAuthConfig } from 'next-auth';
|
2024-10-11 18:00:22 +05:30
|
|
|
|
|
|
|
|
export const authConfig = {
|
|
|
|
|
pages: {
|
2024-11-05 09:35:04 -05:00
|
|
|
signIn: '/login',
|
|
|
|
|
newUser: '/',
|
2024-10-11 18:00:22 +05:30
|
|
|
},
|
|
|
|
|
providers: [
|
|
|
|
|
// added later in auth.ts since it requires bcrypt which is only compatible with Node.js
|
|
|
|
|
// while this file is also used in non-Node.js environments
|
|
|
|
|
],
|
|
|
|
|
callbacks: {
|
|
|
|
|
authorized({ auth, request: { nextUrl } }) {
|
2024-11-15 10:37:01 -05:00
|
|
|
const isLoggedIn = !!auth?.user;
|
|
|
|
|
const isOnChat = nextUrl.pathname.startsWith('/');
|
|
|
|
|
const isOnRegister = nextUrl.pathname.startsWith('/register');
|
|
|
|
|
const isOnLogin = nextUrl.pathname.startsWith('/login');
|
2024-10-11 18:00:22 +05:30
|
|
|
|
|
|
|
|
if (isLoggedIn && (isOnLogin || isOnRegister)) {
|
2024-11-05 09:35:04 -05:00
|
|
|
return Response.redirect(new URL('/', nextUrl as unknown as URL));
|
2024-10-11 18:00:22 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isOnRegister || isOnLogin) {
|
|
|
|
|
return true; // Always allow access to register and login pages
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isOnChat) {
|
|
|
|
|
if (isLoggedIn) return true;
|
|
|
|
|
return false; // Redirect unauthenticated users to login page
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isLoggedIn) {
|
2024-11-05 09:35:04 -05:00
|
|
|
return Response.redirect(new URL('/', nextUrl as unknown as URL));
|
2024-10-11 18:00:22 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
} satisfies NextAuthConfig;
|