Skip to content

Commit

Permalink
hotfix
Browse files Browse the repository at this point in the history
  • Loading branch information
Lee-Dongwook committed Oct 20, 2024
1 parent 7d0af38 commit 96202a5
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 69 deletions.
66 changes: 0 additions & 66 deletions client/src/app/api/[...nextauth]/route.ts

This file was deleted.

58 changes: 58 additions & 0 deletions client/src/app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
@@ -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 };
5 changes: 3 additions & 2 deletions client/src/types/next-auth.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ declare module "next-auth" {
id: string;
name: string;
email: string;
role: string;
role?: string;
token?: string;
}
interface Session {
user: {
id: string;
name?: string | null;
email?: string | null;
image?: string | null;
role?: string;
} & DefaultSession["user"];
accessToken?: string | null;
}
interface JWT {
id: string;
Expand Down
2 changes: 1 addition & 1 deletion server/src/routes/authRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ router.post("/login", async (req: Request, res: Response): Promise<void> => {
}
);

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 });
}
Expand Down

0 comments on commit 96202a5

Please sign in to comment.