From 17fae3eea46c4e808efe14c3215249e8b4fadd50 Mon Sep 17 00:00:00 2001 From: MartinBraquet Date: Sun, 27 Jul 2025 21:43:36 +0200 Subject: [PATCH] Fix router --- app/api/auth/[...nextauth]/route.ts | 98 ++++++++++++++++++++++++----- auth.ts | 61 ------------------ package-lock.json | 2 +- package.json | 2 +- 4 files changed, 85 insertions(+), 78 deletions(-) delete mode 100644 auth.ts diff --git a/app/api/auth/[...nextauth]/route.ts b/app/api/auth/[...nextauth]/route.ts index cbfd5162..7b8a823a 100644 --- a/app/api/auth/[...nextauth]/route.ts +++ b/app/api/auth/[...nextauth]/route.ts @@ -1,18 +1,86 @@ -import NextAuth from "next-auth"; -import { authOptions } from "@/auth"; +import NextAuth, {type NextAuthOptions} from "next-auth"; +import GoogleProvider from "next-auth/providers/google"; +import CredentialsProvider from "next-auth/providers/credentials"; +import {PrismaAdapter} from "@next-auth/prisma-adapter"; +import {prisma} from "@/lib/prisma"; +import bcrypt from "bcryptjs"; -const handler = NextAuth(authOptions); -export { handler as GET, handler as POST }; +export const authOptions = { + providers: [ + GoogleProvider({ + clientId: process.env.GOOGLE_CLIENT_ID!, + clientSecret: process.env.GOOGLE_CLIENT_SECRET!, + }), + CredentialsProvider({ + name: "credentials", + credentials: { + email: {label: "Email", type: "email"}, + name: {label: "Name", type: "name"}, + password: {label: "Password", type: "password"}, + }, + async authorize(credentials) { + if (!credentials?.email || !credentials?.password) { + throw new Error("Email and password are required"); + } -declare module "next-auth" { - interface Session { - user: { id: string; name: string; email: string }; - // user: { id: string }; - } -} + const user = await prisma.user.findUnique({ + where: {email: credentials.email}, + }); -declare module "next-auth/jwt" { - interface JWT { - id: string; - } -} + if (!user || !user.password) { + throw new Error("Invalid email or password"); + } + + const isCorrectPassword = await bcrypt.compare( + credentials.password, + user.password + ); + + if (!isCorrectPassword) { + throw new Error("Invalid email or password"); + } + console.log(user); + return user; + }, + }), + ], + pages: { + signIn: "/login", + }, + session: { + strategy: "database", + }, + adapter: PrismaAdapter(prisma), + debug: process.env.NODE_ENV === "development", + secret: process.env.NEXTAUTH_SECRET, + // callbacks: { + // async jwt({ token, user }) { + // return { ...token, id: token.id ?? user?.id }; + // }, + // async session({ session, token }) { + // return { ...session, user: { ...session.user, id: token.id } }; + // }, + // }, +} satisfies NextAuthOptions; + +// Export **named** HTTP handlers (required by App Router) +console.log("Auth route hit"); + +// export const { handlers, auth, signIn, signOut } = NextAuth(authOptions); +const authHandler = NextAuth(authOptions); +// export const GET = handlers.GET; +// export const POST = handlers.POST; +export { authHandler as GET, authHandler as POST }; + +// declare module "next-auth" { +// interface Session { +// user: { id: string; name: string; email: string }; +// // user: { id: string }; +// } +// } +// +// declare module "next-auth/jwt" { +// interface JWT { +// id: string; +// } +// } diff --git a/auth.ts b/auth.ts deleted file mode 100644 index 39a97c6a..00000000 --- a/auth.ts +++ /dev/null @@ -1,61 +0,0 @@ -import CredentialsProvider from "next-auth/providers/credentials"; -import GoogleProvider from "next-auth/providers/google"; -import {type NextAuthOptions} from "next-auth"; -import bcrypt from "bcryptjs"; -import { PrismaAdapter } from "@next-auth/prisma-adapter"; -import {prisma} from "@/lib/prisma" - -export const authOptions = { - providers: [ - GoogleProvider({ - clientId: process.env.GOOGLE_CLIENT_ID!, - clientSecret: process.env.GOOGLE_CLIENT_SECRET!, - }), - CredentialsProvider({ - name: "credentials", - credentials: { - email: {label: "Email", type: "email"}, - name: {label: "Name", type: "name"}, - password: {label: "Password", type: "password"}, - }, - async authorize(credentials) { - if (!credentials?.email || !credentials?.password) { - throw new Error("Email and password are required"); - } - - const user = await prisma.user.findUnique({ - where: {email: credentials.email}, - }); - - if (!user || !user.password) { - throw new Error("Invalid email or password"); - } - - const isCorrectPassword = await bcrypt.compare( - credentials.password, - user.password - ); - - if (!isCorrectPassword) { - throw new Error("Invalid email or password"); - } - - return user; - }, - }), - ], - pages: { - signIn: "/login", - }, - adapter: PrismaAdapter(prisma), - debug: process.env.NODE_ENV === "development", - secret: process.env.NEXTAUTH_SECRET, - // callbacks: { - // async jwt({ token, user }) { - // return { ...token, id: token.id ?? user?.id }; - // }, - // async session({ session, token }) { - // return { ...session, user: { ...session.user, id: token.id } }; - // }, - // }, -} satisfies NextAuthOptions; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 997af1a0..244e8810 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,7 +16,7 @@ "bcryptjs": "^3.0.2", "lucide-react": "^0.503.0", "next": "^15.4.4", - "next-auth": "^4.24.7", + "next-auth": "^4.24.11", "react": "^19.1.0", "react-dom": "^19.0.0", "react-icons": "^5.5.0" diff --git a/package.json b/package.json index 398e37a9..021a5aa2 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "bcryptjs": "^3.0.2", "lucide-react": "^0.503.0", "next": "^15.4.4", - "next-auth": "^4.24.7", + "next-auth": "^4.24.11", "react": "^19.1.0", "react-dom": "^19.0.0", "react-icons": "^5.5.0"