From 70539fa4bd3d6385f3da67a799ea653f0341e51d Mon Sep 17 00:00:00 2001 From: Lee-Dongwook Date: Sun, 20 Oct 2024 12:07:14 +0900 Subject: [PATCH] feat : Next-auth client --- client/.env.example | 3 +++ client/src/app/api/[...nextauth]/route.ts | 19 ++++++++++++++++++ client/src/app/layout.tsx | 3 ++- client/src/components/Provider/index.tsx | 12 ++++++++++++ client/src/types/next-auth.d.ts | 24 +++++++++++++++++++++++ 5 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 client/.env.example create mode 100644 client/src/components/Provider/index.tsx create mode 100644 client/src/types/next-auth.d.ts diff --git a/client/.env.example b/client/.env.example new file mode 100644 index 0000000..3e134a0 --- /dev/null +++ b/client/.env.example @@ -0,0 +1,3 @@ +NEXT_PUBLIC_API_URL= +NEXTAUTH_SECRET= +JWT_SECRET= \ No newline at end of file diff --git a/client/src/app/api/[...nextauth]/route.ts b/client/src/app/api/[...nextauth]/route.ts index 9efbcee..7c2f780 100644 --- a/client/src/app/api/[...nextauth]/route.ts +++ b/client/src/app/api/[...nextauth]/route.ts @@ -40,6 +40,25 @@ const authOptions: NextAuthOptions = { 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, }; diff --git a/client/src/app/layout.tsx b/client/src/app/layout.tsx index 1ba3309..e0ed154 100644 --- a/client/src/app/layout.tsx +++ b/client/src/app/layout.tsx @@ -3,6 +3,7 @@ import localFont from "next/font/local"; import "./globals.css"; import { Toaster } from "react-hot-toast"; +import Provider from "@/components/Provider"; const geistSans = localFont({ src: "./fonts/GeistVF.woff", @@ -30,7 +31,7 @@ export default function RootLayout({ - {children} + {children} ); diff --git a/client/src/components/Provider/index.tsx b/client/src/components/Provider/index.tsx new file mode 100644 index 0000000..cadb23d --- /dev/null +++ b/client/src/components/Provider/index.tsx @@ -0,0 +1,12 @@ +"use client"; + +import { SessionProvider } from "next-auth/react"; +import { ReactNode } from "react"; + +interface ProviderProps { + children: ReactNode; +} + +export default function Provider({ children }: ProviderProps) { + return {children}; +} diff --git a/client/src/types/next-auth.d.ts b/client/src/types/next-auth.d.ts new file mode 100644 index 0000000..9eacd82 --- /dev/null +++ b/client/src/types/next-auth.d.ts @@ -0,0 +1,24 @@ +// eslint-disable-next-line @typescript-eslint/no-unused-vars +import NextAuth from "next-auth"; + +declare module "next-auth" { + interface User { + id: string; + name: string; + email: string; + role: string; + } + interface Session { + user: { + id: string; + name?: string | null; + email?: string | null; + image?: string | null; + role?: string; + } & DefaultSession["user"]; + } + interface JWT { + id: string; + role: string; + } +}