Skip to content

Commit

Permalink
feat : Next-auth client
Browse files Browse the repository at this point in the history
  • Loading branch information
Lee-Dongwook committed Oct 20, 2024
1 parent 6bc2b08 commit 70539fa
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 1 deletion.
3 changes: 3 additions & 0 deletions client/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
NEXT_PUBLIC_API_URL=
NEXTAUTH_SECRET=
JWT_SECRET=
19 changes: 19 additions & 0 deletions client/src/app/api/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down
3 changes: 2 additions & 1 deletion client/src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -30,7 +31,7 @@ export default function RootLayout({
<body
className={`${geistSans.variable} ${geistMono.variable} antialiased`}
>
<Toaster position="top-right" /> {children}
<Toaster position="top-right" /> <Provider>{children}</Provider>
</body>
</html>
);
Expand Down
12 changes: 12 additions & 0 deletions client/src/components/Provider/index.tsx
Original file line number Diff line number Diff line change
@@ -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 <SessionProvider>{children}</SessionProvider>;
}
24 changes: 24 additions & 0 deletions client/src/types/next-auth.d.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 70539fa

Please sign in to comment.