chatbot-template/app/(auth)/auth.config.ts
2024-10-11 18:00:22 +05:30

39 lines
1.1 KiB
TypeScript

import { NextAuthConfig } from "next-auth";
export const authConfig = {
pages: {
signIn: "/login",
newUser: "/",
},
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 } }) {
let isLoggedIn = !!auth?.user;
let isOnChat = nextUrl.pathname.startsWith("/");
let isOnRegister = nextUrl.pathname.startsWith("/register");
let isOnLogin = nextUrl.pathname.startsWith("/login");
if (isLoggedIn && (isOnLogin || isOnRegister)) {
return Response.redirect(new URL("/", nextUrl));
}
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) {
return Response.redirect(new URL("/", nextUrl));
}
return true;
},
},
} satisfies NextAuthConfig;