ref: remove comments, move authOptions, add somes Types
This commit is contained in:
parent
7af42ef5d6
commit
48e51a738b
@ -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 };
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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 (
|
||||
<>
|
||||
<h1>Home</h1>
|
||||
|
36
src/authOptions.ts
Normal file
36
src/authOptions.ts
Normal 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
14
src/types/next-auth.d.ts
vendored
Normal 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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user