feature/auth #2

Merged
m1000 merged 14 commits from feature/auth into main 2024-12-13 12:55:23 +00:00
5 changed files with 55 additions and 47 deletions
Showing only changes of commit 48e51a738b - Show all commits

View File

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

View File

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

View File

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