ref: remove comments, move authOptions, add somes Types

This commit is contained in:
M1000 2024-11-24 18:22:28 +01:00
parent 7af42ef5d6
commit 48e51a738b
5 changed files with 55 additions and 47 deletions

View File

@ -1,40 +1,5 @@
import NextAuth from "next-auth"; import NextAuth from "next-auth";
import DiscordProvider from "next-auth/providers/discord"; import { authOptions } from "@/authOptions";
import jwt from "jsonwebtoken";
const handler = NextAuth({ const handler = NextAuth(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 }) {
console.log(token);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
session.accessToken = token.accessToken;
return session;
},
},
});
export { handler as GET, handler as POST }; export { handler as GET, handler as POST };

View File

@ -2,7 +2,7 @@ import { useSession } from "next-auth/react";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
const FetchWithSession = () => { const FetchWithSession = () => {
const { data: session, status } = useSession(); // Récupère la session utilisateur const { data: session, status } = useSession();
const [data, setData] = useState(null); const [data, setData] = useState(null);
const [error, setError] = useState(null); const [error, setError] = useState(null);
@ -16,9 +16,7 @@ const FetchWithSession = () => {
method: "GET", method: "GET",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
// eslint-disable-next-line @typescript-eslint/ban-ts-comment Authorization: `Bearer ${session.accessToken}`,
// @ts-ignore
Authorization: `Bearer ${session.accessToken}`, // Exemple : inclure un token JWT si nécessaire
}, },
} }
); );
@ -31,7 +29,7 @@ const FetchWithSession = () => {
setData(result); setData(result);
} catch (err) { } catch (err) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore // @ts-expect-error
setError(err.message); setError(err.message);
} }
} }

View File

@ -1,12 +1,7 @@
"use client"; "use client";
import { useSession } from "next-auth/react";
import FetchWithSession from "./components/FetchWithSession"; import FetchWithSession from "./components/FetchWithSession";
export default function Home() { export default function Home() {
const { data } = useSession(); // Récupère la session
console.log(data);
return ( return (
<> <>
<h1>Home</h1> <h1>Home</h1>

36
src/authOptions.ts Normal file
View File

@ -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;
},
},
};

14
src/types/next-auth.d.ts vendored Normal file
View File

@ -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;
}
}