diff --git a/client/src/app/api/[...nextauth]/route.ts b/client/src/app/api/[...nextauth]/route.ts deleted file mode 100644 index 7c2f780..0000000 --- a/client/src/app/api/[...nextauth]/route.ts +++ /dev/null @@ -1,66 +0,0 @@ -import NextAuth, { type NextAuthOptions } from "next-auth"; -import CredentialsProvider from "next-auth/providers/credentials"; - -const authOptions: NextAuthOptions = { - providers: [ - CredentialsProvider({ - name: "Credentials", - credentials: { - email: { label: "Email", type: "email" }, - password: { label: "Password", type: "password" }, - }, - async authorize(credentials) { - const res = await fetch( - `${process.env.NEXT_PUBLIC_API_URL}/api/auth/login`, - { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify(credentials), - } - ); - - const user = await res.json(); - - if (res.ok && user) { - return { - id: user.id, - name: user.name, - email: user.email, - role: user.role, - }; - } - - return null; - }, - }), - ], - session: { - strategy: "jwt", - maxAge: 3 * 24 * 60 * 60, - }, - jwt: { - secret: process.env.JWT_SECRET, - }, - callbacks: { - async jwt({ token, user }) { - if (user) { - token.id = user.id; - token.role = user.role; - } - return token; - }, - - async session({ session, token }) { - session.user.id = token.id as string; - session.user.role = token.role as string; - - return session; - }, - }, - secret: process.env.NEXTAUTH_SECRET, -}; - -const handler = NextAuth(authOptions); -export { handler as GET, handler as POST }; diff --git a/client/src/app/api/auth/[...nextauth]/route.ts b/client/src/app/api/auth/[...nextauth]/route.ts new file mode 100644 index 0000000..844521e --- /dev/null +++ b/client/src/app/api/auth/[...nextauth]/route.ts @@ -0,0 +1,58 @@ +import NextAuth, { NextAuthOptions } from "next-auth"; +import CredentialsProvider from "next-auth/providers/credentials"; +import axios from "axios"; + +export const authOptions: NextAuthOptions = { + providers: [ + CredentialsProvider({ + name: "Credentials", + credentials: { + email: { label: "Email", type: "text" }, + password: { label: "Password", type: "password" }, + }, + async authorize(credentials) { + const { email, password } = credentials ?? {}; + + try { + const response = await axios.post( + `${process.env.NEXT_PUBLIC_API_URL}/login`, + { email, password } + ); + + const { token, user } = response.data; + + return { id: user.id, name: user.name, email: user.email, token }; + } catch (error) { + console.error("Login failed:", error); + return null; + } + }, + }), + ], + session: { + strategy: "jwt", + }, + callbacks: { + async jwt({ token, user }) { + if (user) { + token.id = user.id; + token.name = user.name; + token.email = user.email; + token.accessToken = user.token; + } + return token; + }, + async session({ session, token }) { + session.user.id = token.id; + session.user.name = token.name; + session.user.email = token.email; + session.accessToken = token.accessToken as string; + return session; + }, + }, + secret: process.env.NEXTAUTH_SECRET, +}; + +const handler = NextAuth(authOptions); + +export { handler as GET, handler as POST }; diff --git a/client/src/types/next-auth.d.ts b/client/src/types/next-auth.d.ts index 9eacd82..cfec1a3 100644 --- a/client/src/types/next-auth.d.ts +++ b/client/src/types/next-auth.d.ts @@ -6,7 +6,8 @@ declare module "next-auth" { id: string; name: string; email: string; - role: string; + role?: string; + token?: string; } interface Session { user: { @@ -14,8 +15,8 @@ declare module "next-auth" { name?: string | null; email?: string | null; image?: string | null; - role?: string; } & DefaultSession["user"]; + accessToken?: string | null; } interface JWT { id: string; diff --git a/server/src/routes/authRoutes.ts b/server/src/routes/authRoutes.ts index ee37ff7..f409088 100644 --- a/server/src/routes/authRoutes.ts +++ b/server/src/routes/authRoutes.ts @@ -51,7 +51,7 @@ router.post("/login", async (req: Request, res: Response): Promise => { } ); - res.json({ token, userId: user._id, username: user.username }); + res.status(200).json({ token, userId: user._id, username: user.username }); } catch (error) { res.status(500).json({ message: "Login failed", error }); }