From dfb3d0173a687b35a11d6b4b239683efdb8ba9a9 Mon Sep 17 00:00:00 2001 From: Guillaume DEMAN Date: Tue, 25 Jun 2024 18:49:28 +0200 Subject: [PATCH] Get bearer token and push it in API request --- src/app/apiClient/registerUser.ts | 1 + src/middleware.ts | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/app/apiClient/registerUser.ts b/src/app/apiClient/registerUser.ts index 85edad1..ef2b3b3 100644 --- a/src/app/apiClient/registerUser.ts +++ b/src/app/apiClient/registerUser.ts @@ -1,6 +1,7 @@ export const registerUserApi = async (data: { email: string; password: string; + confirmPassword: string; username: string; name: string; lastName: string; diff --git a/src/middleware.ts b/src/middleware.ts index 6453be8..da9aa98 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -1,10 +1,22 @@ -import { withAuth } from "next-auth/middleware"; +import { NextRequestWithAuth, withAuth } from "next-auth/middleware"; import { NextResponse } from "next/server"; export default withAuth( - function middleware(req) { + async function middleware(req: NextRequestWithAuth) { // ajouter les informations de la session à la requête ? - return NextResponse.next(); + const bearerToken = req.headers.get("Authorization"); + const token = bearerToken?.split("Bearer ")[1]; + + console.log("middleware token", token); + + const requestHeaders = new Headers(req.headers); + requestHeaders.set("Authorization", `Bearer ${token}`); + + return NextResponse.next({ + request: { + headers: requestHeaders, + }, + }); }, { callbacks: {