From 48e51a738bae5e21408a7a3e35e6e9f1d358dad2 Mon Sep 17 00:00:00 2001 From: M1000 Date: Sun, 24 Nov 2024 18:22:28 +0100 Subject: [PATCH] ref: remove comments, move authOptions, add somes Types --- src/app/api/auth/[...nextauth]/route.ts | 39 ++----------------------- src/app/components/FetchWithSession.tsx | 8 ++--- src/app/page.tsx | 5 ---- src/authOptions.ts | 36 +++++++++++++++++++++++ src/types/next-auth.d.ts | 14 +++++++++ 5 files changed, 55 insertions(+), 47 deletions(-) create mode 100644 src/authOptions.ts create mode 100644 src/types/next-auth.d.ts diff --git a/src/app/api/auth/[...nextauth]/route.ts b/src/app/api/auth/[...nextauth]/route.ts index 2c7a966..df3a04c 100644 --- a/src/app/api/auth/[...nextauth]/route.ts +++ b/src/app/api/auth/[...nextauth]/route.ts @@ -1,40 +1,5 @@ import NextAuth from "next-auth"; -import DiscordProvider from "next-auth/providers/discord"; -import jwt from "jsonwebtoken"; +import { authOptions } from "@/authOptions"; -const handler = NextAuth({ - providers: [ - DiscordProvider({ - clientId: process.env.DISCORD_CLIENT_ID!, - clientSecret: process.env.DISCORD_CLIENT_SECRET!, - }), - ], - secret: process.env.NEXTAUTH_SECRET, - session: { - strategy: "jwt", - }, - callbacks: { - async jwt({ token, account, user }) { - if (account) { - token.accessToken = jwt.sign( - { - userId: user?.id, - provider: account.provider, - timestamp: Date.now(), - }, - process.env.NEXTAUTH_SECRET!, - { expiresIn: "1h" } - ); - } - return token; - }, - async session({ session, token }) { - console.log(token); - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - session.accessToken = token.accessToken; - return session; - }, - }, -}); +const handler = NextAuth(authOptions); export { handler as GET, handler as POST }; diff --git a/src/app/components/FetchWithSession.tsx b/src/app/components/FetchWithSession.tsx index d1ae591..d2a84c4 100644 --- a/src/app/components/FetchWithSession.tsx +++ b/src/app/components/FetchWithSession.tsx @@ -2,7 +2,7 @@ import { useSession } from "next-auth/react"; import { useEffect, useState } from "react"; const FetchWithSession = () => { - const { data: session, status } = useSession(); // Récupère la session utilisateur + const { data: session, status } = useSession(); const [data, setData] = useState(null); const [error, setError] = useState(null); @@ -16,9 +16,7 @@ const FetchWithSession = () => { method: "GET", headers: { "Content-Type": "application/json", - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - Authorization: `Bearer ${session.accessToken}`, // Exemple : inclure un token JWT si nécessaire + Authorization: `Bearer ${session.accessToken}`, }, } ); @@ -31,7 +29,7 @@ const FetchWithSession = () => { setData(result); } catch (err) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore + // @ts-expect-error setError(err.message); } } diff --git a/src/app/page.tsx b/src/app/page.tsx index 096fd84..e64e3f6 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -1,12 +1,7 @@ "use client"; -import { useSession } from "next-auth/react"; import FetchWithSession from "./components/FetchWithSession"; export default function Home() { - const { data } = useSession(); // Récupère la session - - console.log(data); - return ( <>

Home

diff --git a/src/authOptions.ts b/src/authOptions.ts new file mode 100644 index 0000000..17bfe2b --- /dev/null +++ b/src/authOptions.ts @@ -0,0 +1,36 @@ +import { AuthOptions } from "next-auth"; +import DiscordProvider from 'next-auth/providers/discord'; +import jwt from 'jsonwebtoken'; + +export const authOptions: AuthOptions = { + providers: [ + DiscordProvider({ + clientId: process.env.DISCORD_CLIENT_ID!, + clientSecret: process.env.DISCORD_CLIENT_SECRET!, + }), + ], + secret: process.env.NEXTAUTH_SECRET, + session: { + strategy: "jwt", + }, + callbacks: { + async jwt({ token, account, user }) { + if (account) { + token.accessToken = jwt.sign( + { + userId: user?.id, + provider: account.provider, + timestamp: Date.now(), + }, + process.env.NEXTAUTH_SECRET!, + { expiresIn: "1h" } + ); + } + return token; + }, + async session({ session, token }) { + session.accessToken = token.accessToken; + return session; + }, + }, +}; \ No newline at end of file diff --git a/src/types/next-auth.d.ts b/src/types/next-auth.d.ts new file mode 100644 index 0000000..863e852 --- /dev/null +++ b/src/types/next-auth.d.ts @@ -0,0 +1,14 @@ +// eslint-disable-next-line @typescript-eslint/no-unused-vars +import NextAuth, { DefaultSession } from "next-auth"; + +declare module "next-auth" { + interface Session { + accessToken: string; + } +} + +declare module "next-auth/jwt" { + interface JWT { + accessToken: string; + } +}