how to authenticate 3rd api along with nextauth's JWT session

Viewed 98

I have two components:

  • mydomain runs a nextjs along with next-auth.js
  • api.mydomain runs a back-end API that mydomain's client points to make some request

Mydomain is authenticated via the Next.js's Github provider, and as long as I get next-auth.js creates a JWT session which is sent to the client as a form of a cookie (correct me if I'm wrong). Then, if you need some info about your authentication, you can use the hook useSession on front side in order to access your JWT session.

My frontend is not using the api nextjs' functionality though: it communicates with an API through axios HTTP calls, which is deployed separately. What I would like to do is passing the Nextjs' JWT to my backend in order to verify authentication and authorization, but I'm not sure if it's possible, safe and recommended.

So: how could I accomplish that? The only alternative I see is generating a separated JWT token within the JWT callback, pass back to the front and use it for my calls, but I see it like a redundant thing actually, like embedding a JWT within a JWT.

Here is what I'd like to do in summary:

enter image description here

0 Answers
Related