From 44c19d28132231351caa199562a57576caea1587 Mon Sep 17 00:00:00 2001 From: "Orace.A" Date: Tue, 25 Mar 2025 15:31:54 +0100 Subject: [PATCH] feat: add nextauth config --- auth.d.ts | 12 ++++ src/app/api/auth/[...nextauth]/route.ts | 79 ++++++++++++++++--------- 2 files changed, 62 insertions(+), 29 deletions(-) create mode 100644 auth.d.ts diff --git a/auth.d.ts b/auth.d.ts new file mode 100644 index 0000000..9d14c6a --- /dev/null +++ b/auth.d.ts @@ -0,0 +1,12 @@ +import { DefaultSession } from "next-auth"; + +declare module "next-auth" { + export interface User extends Partial> { + access_token: string; + refresh_token: string; + } + + export interface Session { + user: User; + } +} \ No newline at end of file diff --git a/src/app/api/auth/[...nextauth]/route.ts b/src/app/api/auth/[...nextauth]/route.ts index 80aa1f1..2e22206 100644 --- a/src/app/api/auth/[...nextauth]/route.ts +++ b/src/app/api/auth/[...nextauth]/route.ts @@ -1,6 +1,6 @@ import NextAuth, { User } from "next-auth"; import Credentials from "next-auth/providers/credentials"; -import axios, { AxiosError } from "axios"; +import axios from "axios"; import { jwtDecode } from "jwt-decode"; const handler = NextAuth({ @@ -11,37 +11,58 @@ const handler = NextAuth({ password: {}, }, async authorize(credentials) { - let user: User | null = null; - - const response = axios({ - method: 'post', - url: 'private-docs-api.intside.co/users/login/', - data: { - email: credentials?.email, - password: credentials?.password, - } - }) - .then(function (response: any) { - const { user_id } = jwtDecode(response.access_token) as { - user_id: string; - }; - - - - }) - .catch(function (error) { - if (error instanceof AxiosError) { - if (error.status === 401) { - throw new Error("Email ou mot de passe incorrect"); - } else { - throw new Error(error.message, error); + try { + const response = await axios.post( + 'private-docs-api.intside.co/users/login/', + { + email: credentials?.email, + password: credentials?.password, } - } - throw new Error("Une erreur est survenue"); - }); + ) + + const { access_token, refresh_token } = response.data; + const { id } = jwtDecode(access_token) as { id: string }; + + return { + id: id, + email: credentials?.email, + access_token: access_token, + refresh_token: refresh_token + } as User; + } catch (error) { + if (axios.isAxiosError(error)) { + if (error.response?.status === 401) { + throw new Error("Email ou mot de passe incorrect"); + } + throw new Error(error.response?.data?.message || error.message); + } + throw new Error("Une erreur est survenue"); + } }, }) - ] + ], + session: { + strategy: "jwt", + }, + callbacks: { + async jwt({ token, user }) { + if (user) { + token.access_token = user.access_token; + token.refresh_token = user.refresh_token; + } + return token; + }, + async session({ session, token }) { + return { + ...session, + user: { + ...session.user, + ...token, + }, + }; + }, + }, + secret: process.env.AUTH_SECRET ?? "", }); export { handler as GET, handler as POST };